Nur Haslina
Nur Haslina

Reputation: 203

How to automatically set value for a text field based on value from another text field in Flutter?

Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Postal Code"),
        TextField(
          controller: controllerPostalCode,
        ),

        // if controllerPostalCode not null
        Container(
            child: FutureBuilder<AddressCtrl>(   //Future Builder that hold data on State and Country
                future: getDataCtrl(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (snapshot.data.result == null) {
                      return Container();
                    } else {
                      return Container(
                        child: Column(
                          children: <Widget>[
                            Text("State"),
                            TextField(
                              decoration: InputDecoration(
                                  hintText: snapshot.data.result[index].State // value will be display after user input Postal Code text field
                                  ),
                            ),
                            Text("Country"),
                            TextField(
                              decoration: InputDecoration(
                                  hintText: snapshot.data.result[index].Country // value will be display after user input Postal Code text field
                                  ),
                            )
                          ],
                        ),
                      );
                    }
                  }
                })),
      ],
    ),
  ),

I want to automatically input values to two text field (in this case: State and Country) based on value of user input on Postal Code text field without having to click on any button. I found many question regarding this issue but I couldn't find in Dart/Flutter.

Additional notes: The value of State and Country are from REST API.

Upvotes: 1

Views: 6289

Answers (2)

user14191814
user14191814

Reputation:

use in text

var textController = new TextEditingController();

Upvotes: 1

Shady Boshra
Shady Boshra

Reputation: 2911

I think you can use onTextChanged method in postal code and setState to change the variable that holding the text from postal code text field. Then use it (the variable) where you want.

And this doc will help you more and more.

Hope I could help you.

Upvotes: 0

Related Questions