'Detect changes in TextField in Flutter

I have created a function which return 4 textfields in column and I have called that function from 4 different containers, now how do I know from which container textfields are being changed because all the textfields are common to different containers. Thanks in advance.



Solution 1:[1]

in general you can handle the changes in Textfield in two way

1- Using onChanged() callback

TextField(
  onChanged: (text) {
    print('First text field: $text');
  },
),

2- Using a TextEditingController

you create a controller

 final myController = TextEditingController();

Connect the TextEditingController to a text field.

TextField(
  controller: myController,
  ),

Create a function to print the latest value.

void _printLatestValue() {
  print('Second text field: ${myController.text}');
 }

Listen to the controller for changes.

@override
void initState() {
  super.initState();

  // Start listening to changes.
  myController.addListener(_printLatestValue);
}

in your case you can pass controllers to your function and listen to it

Solution 2:[2]

What about using keys. You can choose unique keys for each of the different textfields(maybe you can generate keys when you programmatically generate the textfields). Alternatively, you can assign unique keys to the containers as well.

Solution 3:[3]

flutter textfield has onChanged property, you can do inside onChanged

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdulkarim
Solution 2 Vipin Kumar Kashyap
Solution 3 anggadaz