在Flutter中更改TextFormField中的文本

时间:2020-05-15 16:56:07

标签: flutter dart

我有一个包含许多TextFormField的表单,这些表单是在ListView.builder内部动态加载的,问题是我只想更改一个TextFromField的文本,但是我要更改使用TextEditingController的所有TextFromField,这是因为我所有的TextFormField都使用同一控制器,是否有任何方法可以更改TextFormField的文本,而无论TextFormField是什么?< / p>

非常感谢您的帮助。

这是ListView.builderTextFormField

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){}
                ),
              ],
            ); 
          }
        ),
      ],
    );
  }
}

1 个答案:

答案 0 :(得分:1)

对于多个文本字段使用一个TextEditingController是不正确的。您应该为每个文本字段创建一个控制器。

我会这样:

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){}
                ),
              ],
            ); 
          }
        ),
      ],
    );
  }
}