我有一个包含许多TextFormField的表单,这些表单是在ListView.builder内部动态加载的,问题是我只想更改一个TextFromField的文本,但是我使用TextEditingController更改了所有TextFromField的文本,这是因为我所有的TextFormField都在使用相同的 Controller ,是否有一种方法可以更改一个TextFormField的文本,而无论TextFormField到底是什么?

非常感谢您的帮助。

这是带有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){}
                ),
              ],
            );
          }
        ),
      ],
    );
  }
}

最佳答案

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

我会这样:

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

关于flutter - 在Flutter中更改TextFormField中的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61824537/

10-11 14:57