Home > Blockchain >  Detect changes in TextField in Flutter
Detect changes in TextField in Flutter

Time:05-11

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

flutter textfield has onChanged property, you can do inside onChanged

  • Related