Pre-inserted text from the database

Asked

Viewed 157 times

0

In my app it would be interesting this text come from the bank not in formed of placeholder but in a text where he could change only a comma if he wanted, but I can’t find a way to do that because I’ve been reading the documentation of the flutter and I couldn’t find anything that would help in this regard.


class CommentStyle extends StatelessWidget {


  //Parametros que precisam ser passados para o CommentStyle
  const CommentStyle(this.index, this.model, {this.comentario});
  final int index;
  final UserModel model;
  final String comentario;



  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.all(15.0),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Center(
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(89.0, 0.0, 0.0, 0.0),
                          child: StyleTextTitle("Perfil do contato"),
                        ),
                      ),
                    ],
                  ),

                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.person_solid),
                      StyleTextTitle("Nome:"),
                      StyleTextInfo(model.contactsList[index]["fullname"]),
                    ],
                  ),

                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.mail),
                      StyleTextTitle("E-mail:"),
                      StyleTextInfo(model.contactsList[index]["email"]),
                    ],
                  ),

                  //Titulo acima do TextField.
                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.pen),
                      StyleTextTitle("Comentário: "),
                    ],
                  ),
                 //Aqui que eu quero retornar o texto editavel
                  CupertinoTextField(
                    controller: controllerComent,
                    maxLines: 6,
                    decoration: BoxDecoration(
                        border: Border.all(width: 1.0, color: Colors.grey),
                        borderRadius: BorderRadius.circular(15)),
                    placeholder: model.contactsList[index]["comentario"],
                    keyboardType: TextInputType.text,
                    maxLength: 255,
                  ),
                ],
              )),
        ],
      ),
    );
  }
}
  • Feed the controllerComent with the database data on initState( If you’re using a Statefullwidget) or before return within the method Build (If you are using Statelesswidget). Once you have time, I will create a more detailed answer.

  • Thanks, I’ve been testing here and I couldn’t. Your example would be of great help.

  • The parameter he needs to receive is model.contactsList[index]["id"] however it is only possible to take these parameters inside my widget StateLess that within it I pass the parameters that will pull from the seat but if I put the controller inside it, it accuses as if it were a necessary parameter too and that’s where I brake.

  • In this case then, put the complete code from where this will be used TextField, provide a more complete example helps us help you hahaha

  • Okay, I added rsrs

  • I saw that you opened another question at the SOP, this one... If the main question of this question here has already been resolved, please mark one of the answers as ACCEPTED!

  • I forgot to dial. Thank you for remembering

Show 2 more comments

2 answers

0


It even looks like you providing complete example still lacked creation of TextController, but come on, I’ll give you an example and you adjust your need.

class CommentStyle extends StatelessWidget {


  //Parametros que precisam ser passados para o CommentStyle
  const CommentStyle(this.index, this.model, {this.comentario});
  final int index;
  final UserModel model;
  final String comentario;
  TextController controllerComent = TextController();    

  @override
  Widget build(BuildContext context) {
    controllerComent.Text = "Texto que veio do banco";
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.all(15.0),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Center(
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(89.0, 0.0, 0.0, 0.0),
                          child: StyleTextTitle("Perfil do contato"),
                        ),
                      ),
                    ],
                  ),

                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.person_solid),
                      StyleTextTitle("Nome:"),
                      StyleTextInfo(model.contactsList[index]["fullname"]),
                    ],
                  ),

                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.mail),
                      StyleTextTitle("E-mail:"),
                      StyleTextInfo(model.contactsList[index]["email"]),
                    ],
                  ),

                  //Titulo acima do TextField.
                  Row(
                    children: <Widget>[
                      Icon(CupertinoIcons.pen),
                      StyleTextTitle("Comentário: "),
                    ],
                  ),
                 //Aqui que eu quero retornar o texto editavel
                  CupertinoTextField(
                    controller: controllerComent,
                    maxLines: 6,
                    decoration: BoxDecoration(
                        border: Border.all(width: 1.0, color: Colors.grey),
                        borderRadius: BorderRadius.circular(15)),
                    placeholder: model.contactsList[index]["comentario"],
                    keyboardType: TextInputType.text,
                    maxLength: 255,
                  ),
                ],
              )),
        ],
      ),
    );
  }
}

Because it is a StatelessWidget you can feed your controller directly on Build.

This way you will have the variable fed when the screen is created, already with the database data and then can use it again to recover the edited text.

  • 1

    I didn’t put the rest of the code because it’s just the creation of the screen as: app bar and calling the Stateless inside the Scaffold so I didn’t see much need, but the way you put it worked here, except for the controller can’t be declared there. The only problem in this way that you have gone through is that it is not possible to edit the text within Textfield but it should not be anything too difficult to solve and once again thank you for your help.

  • For nothing! I forgot to initialize the TextController but I have already modified in my reply.

0

You didn’t put in the full code, so it’s hard to guess where things come from, like yours controllerComent for example.

The error is in it, you have to create it by passing parameter the text you want to be initially filled the CupertinoTextField. Change your code snippet to this:

CupertinoTextField(
  controller: TextEditingController(text: model.contactsList[index]["comentario"]),
  maxLines: 6,
  decoration: BoxDecoration(
      border: Border.all(width: 1.0, color: Colors.grey),
      borderRadius: BorderRadius.circular(15)),
  keyboardType: TextInputType.text,
  maxLength: 255,
),

And you will already see the text presented on TextField. Now, you have to see the implications with the controller being created directly there, probably not the best option for you to create it right now. But since I don’t know where you’re raising him.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.