In Flutter, How do you link multiple text editing controllers to one button?

31 Views Asked by At

I want to clear text from three different text fields using one button but I can't figure out how. Thanks.

1

There are 1 best solutions below

0
Will Johnson On

you can link multiple text editing controllers to one button in Flutter . just create a TextEditingController for each text field and connect them using the controller property. When the button is pressed, you can call the clear() method of each controller to clear the text in the associated text field

here is a code example

// Create text editing controllers
final TextEditingController controller1 = TextEditingController();
final TextEditingController controller2 = TextEditingController();
final TextEditingController controller3 = TextEditingController();

// Connect each controller to the respective text field
TextField(
  controller: controller1,
),
TextField(
  controller: controller2,
),
TextField(
  controller: controller3,
),

// Link the button to clear the text in all three text fields
ElevatedButton(
  onPressed: () {
    controller1.clear();
    controller2.clear();
    controller3.clear();
  },
  child: Text('Clear All'),
)