简体   繁体   中英

Change text in a TextFormField in Flutter

I have a form with many TextFormField that are dynamically loaded inside a ListView.builder , the problem is that I want to change the text of only one TextFromField but I change the text of all the TextFromField using TextEditingController and it is because all my TextFormField are using the same controller, is there any way to change the text of just one TextFormField no matter exactly what TextFormField is?

I would greatly appreciate your help.

Here is the ListView.builder with the TextFormField :

 class ListBuilder extends StatefulWidget { final Preguntas preguntas; final formKey; ListBuilder({@required this.preguntas, this.formKey}); Preguntas preguntasGlobal = Preguntas(); @override _ListBuilderState createState() => _ListBuilderState(); } class _ListBuilderState extends State<ListBuilder> { var textController = new TextEditingController(); @override Widget build(BuildContext context) { return Column( children: <Widget>[ SizedBox(height: 15.0), ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: widget.preguntas.secciones[0].preguntas.length, itemBuilder: (BuildContext context, int i){ return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ TextFormField( controller: textController, decoration: InputDecoration( errorStyle: TextStyle(fontFamily: "OpenSans-Regular", color: Color(0xffe81935)), hintMaxLines: 500, hintText: widget.preguntas.secciones[0].preguntas[i].respuestas[0].pregunta.descripcion, hintStyle: TextStyle(fontFamily: "OpenSans-Regular", fontSize: 14.0), ), onChanged: (value){} ), ], ); } ), ], ); } }

It is incorrect to use one TextEditingController for multiple text fields. You should create one controller per text field.

I would do like this:

class _ListBuilderState extends State<ListBuilder> {
  List<TextEditingController> _controllers;
  @override
  void initState() { 
    super.initState();
    // Pre-create controllers:
    _controllers = List<TextEditingController>.generate(
        widget.preguntas.secciones[0].preguntas.length, (index) => TextEditingController());
  }


  @override
  Widget build(BuildContext context) {
    return Column(
       children: <Widget>[
        SizedBox(height: 15.0),
        ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true, 
          itemCount: widget.preguntas.secciones[0].preguntas.length,
          itemBuilder: (BuildContext context, int i){
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                TextFormField(
                  // Access a controller by index.
                  controller: _controllers[i],
                  decoration: InputDecoration(
                    errorStyle: TextStyle(fontFamily: "OpenSans-Regular", color: Color(0xffe81935)),
                    hintMaxLines: 500,
                    hintText: widget.preguntas.secciones[0].preguntas[i].respuestas[0].pregunta.descripcion,
                    hintStyle: TextStyle(fontFamily: "OpenSans-Regular", fontSize: 14.0),
                  ),
                  onChanged: (value){}
                ),
              ],
            ); 
          }
        ),
      ],
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM