Home > Blockchain >  I want to update the text inside a TextFormField when I change the item in a DropDownButton in Flutt
I want to update the text inside a TextFormField when I change the item in a DropDownButton in Flutt

Time:02-11

I have a dropDownButton populated with a query from my database here I have the users, when I select an item I want to show all the user info in the TextFields below.

Since some of the information should not be edited by hand, I wanted to make some of the fields as containers, so I can't write in them, but some fields should be a TextField so I can edit them. I'm new to flutter so I still don't understand this at full, and I can't find a way to load the info in the TextFields when I change the dropDownButton.

here is the code below:

class UtilizadorPage extends StatefulWidget {
  @override
  _UtilizadorPageState createState() => new _UtilizadorPageState();
}

class _UtilizadorPageState extends State<UtilizadorPage> {
  Utilizador user = Utilizador();
  BDLocal db = BDLocal.instance;
  Utilizador? _currentUser;
  var txt = TextEditingController();


  @override
  void initState(){
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Utilizadores",
        ),
      ),
      body: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              FutureBuilder<List<Utilizador>>(
                  future: user.getUtilizadores(),
                  builder: (BuildContext context,
                      AsyncSnapshot<List<Utilizador>> snapshot){
                        if (!snapshot.hasData) return CircularProgressIndicator();
                        return DropdownButton<Utilizador>(
                          alignment: Alignment.center,
                          items: snapshot.data?.map((util) => DropdownMenuItem<Utilizador>(
                            child: Text(util.nome),
                            value: util,
                          )).toList(),
                          onChanged:(Utilizador? value) {
                          setState(() {
                            _currentUser = value;
                          });
                        },
                    isExpanded: false,
                    //value: _currentUser,
                    hint: Text('Select User'),
                    );
                  }),
            ]
          ),
          SizedBox(height: 20.0),
          Row(
            children: [
              _currentUser != null
                  ? Expanded(
                child: Column(
                  children: [
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              child: InputDecorator(
                                decoration: InputDecoration(
                                labelText: "Nome",
                                floatingLabelBehavior:FloatingLabelBehavior.always,
                                border: OutlineInputBorder(),
                              ),
                                  child: Text(_currentUser!.nome)
                              ),
                            ),
                          ),
                          SizedBox(width: 10.0),
                          Expanded(
                            flex: 1,
                            child: Container(
                              child: InputDecorator(
                                  decoration: InputDecoration(
                                    labelText: "ID na BDNuvem",
                                    floatingLabelBehavior:FloatingLabelBehavior.always,
                                    border: OutlineInputBorder(),
                                  ),
                                  child: Text(_currentUser!.id_BDNuvem.toString())
                              ),
                            ),
                          ),
                        ]),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Expanded(
                          flex: 1,
                          child: TextFormField(
                            initialValue: _currentUser!.perfil.toString(),
                            decoration: InputDecoration(
                              label: Text("Perfil"),
                              floatingLabelBehavior:FloatingLabelBehavior.always,
                              border: OutlineInputBorder(),
                            ),
                          ),
                        ),
                        SizedBox(width: 10.0),
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Numero de Colaborador",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.numero.toString())
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Nome do Funcionario",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.nomeFuncionario)
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Email",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.email)
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Senha",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.senha)
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Ultima Atualizacao",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.ultimaAtualizacao)
                            ),
                          ),
                        ),
                        SizedBox(width: 10.0),
                        Expanded(
                          flex: 1,
                          child: Container(
                            child: InputDecorator(
                                decoration: InputDecoration(
                                  labelText: "Atualizado Por",
                                  floatingLabelBehavior:FloatingLabelBehavior.always,
                                  border: OutlineInputBorder(),
                                ),
                                child: Text(_currentUser!.atualizadoPor)
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              )
                  : Text("No User selected"),
            ],
          )
        ],
      ),
    );
  }
}

I only have one textField now because I'm still experimenting things. what I wanted to do is update the value of the text with the info I retrieved from the query for example: _currentUser!.perfil.toString(), which holds a string that represent the type of user

CodePudding user response:

If you've assigned a TextEditingController to Textfield you can simply manipulate value in by

myTextEditingController.text = "new value";
  • Related