Home > Software engineering >  flutter : Textfield duplicates first data entry
flutter : Textfield duplicates first data entry

Time:07-31

I am creating an application where user can add their triggers. I am not sure what happens but everytime that I entry any data to the first textfield its duplicate to the next one. Users can add multiples textfields with a button.

This problem appear when I added a controller to the textfield.

This is the code -

children: List.generate(
                        totalTextField,
                        (index) => Row(children: [
                              SizedBox(
                                  width: 90,
                                  child: TextField(
                                    controller: _triggersController,
                                    decoration: InputDecoration(
                                      isDense: true,
                                      contentPadding: EdgeInsets.zero,
                                      hintText: 'Add...',
                                      hintStyle: TextStyle(
                                          fontWeight: FontWeight.normal,
                                          color: Color.fromARGB(
                                              255, 194, 193, 193),
                                          fontSize: 13),
                                      filled: true,
                                      fillColor: Colors.white,
                                    ),
                                  )),
                              SizedBox(width: 5),
                              if (index == totalTextField - 1)
                                ElevatedButton(
                                  style: ElevatedButton.styleFrom(
                                    primary: Color.fromARGB(255, 255, 255, 255),
                                    elevation: 2,
                                    shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.zero),
                                    minimumSize: Size(60, 24),
                                  ),

Image of the problem

thank you for your help guys

CodePudding user response:

propably because you have TextField generated always with the same controller.

CodePudding user response:

This is Because all the TextFields are using just one controller

_triggersController

So when you change the text in one field all the fields will change their value too.

Generate a variable having list of controllers as well and then you can pass that controllers list inside your TextField

List<TextEditingController> allControllers =
          List.generate(totalTextField, (index) => TextEditingController());

And now pass the controller as well.

TextField(
         controller: allControllers[index],
         decoration: InputDecoration(
         isDense: true,
         contentPadding: EdgeInsets.zero,
         hintText: 'Add...',
         hintStyle: TextStyle(
         fontWeight: FontWeight.normal,
         color: Color.fromARGB(
         255, 194, 193, 193),
         fontSize: 13),
         filled: true,
         fillColor: Colors.white,
         ),
       )),

To use the controller to get the text back you can use

allControllers[1].text;
  • Related