Best practice for assigning key for different widgets?

vij

I have been looking for controlling each widget in a page using a separate key. Can anyone suggest me best way for that? I tried in Form like

final formKey = GlobalKey<FormState>();
Form(
key: formKey,
),

But I am confused while working with Container, Card, and others widget. Can any suggest what should I follow and where?

Esen Mehmet

You don't need formKey for Container, Card or any other widget which is not related with .

If you create Form Widget, you need to give it a TextFormField child, which inherits FormState.

The thing is here, probably you'll have multiple TextFormField widgets, so instead of creating and handling them one by one keys for each TextFormField, you create Form Widget to group your TextFormFields, then assign formKey once and use it at all.

Your formKey has no business with other type of widgets.

Explainer code:

  Widget buildFormTree() {
    final formKey = GlobalKey<FormState>();
    String text1;
    String text2;
    return Column(
      children: <Widget>[
        Form(
          key: formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
//            key: asd, //No need
                onSaved: (text) {
                  text1 = text;
                },
              ),
              TextFormField(
//            key: qwe, // No need
                onSaved: (text) {
                  text2 = text;
                },
              ),
            ],
          ),
        ),
        RaisedButton(
          child: Text('Save Forms'),
          onPressed: () {
            ///to trigger onSaved callback
            formKey.currentState.save();
          },
        )
      ],
    );
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best practice for using ssh key pair with different groups?

From Dev

Best practice when assigning reference type properties

From Dev

Creating your own flutter widgets best practice

From Dev

which is best coding practice, assigning to the variable or fetch from database

From Dev

Re-Assigning values on a pandas DataFrame best practice

From Dev

What is the best practice for using id as primary key?

From Java

Is using the Class instance as a Map key a best practice?

From Dev

MySQL table without primary key (best practice)

From Dev

Best practice to store App Key in Laravel

From Dev

Store.putRight(..) and best practice for key selection

From Dev

Cassandra best practice to ORDER BY using PRIMARY KEY

From Dev

Best practice for handling api key in Play framework

From Dev

Ngrx effect dispatch different actions - best practice

From Java

Best practice to run Linux service as a different user

From Dev

Best practice for calling two different endpoints for a widget?

From Dev

What is best practice to implement SQS on different environments?

From Dev

Best practice for dynamically translating content into different languages

From Dev

Kubernetes best practice: different config for local or remote

From Dev

Assigning values to Flutter widgets

From Dev

What is best practice for instantiating a variable for use outside of a loop if you are only assigning to it within the loop?

From Dev

What is a best practice naming convention for DOJO widgets that can be initiated only once?

From Dev

Best practice to update the Application Insights instrumentation key for classic Desktop apps

From Dev

Best practice to implement key-value pair in android Spinner

From Dev

In MVC whats best practice for placing primary key in table row

From Dev

Best Practice to check for multiple array_key_exists in PHP

From Dev

Laravel best practice method for querying using an index key in table

From Dev

Best practice for storing and retrieving a list of strings in a single JSON key?

From

Best practice when using an API key in Node.js

From Dev

What to use (best/good practice) for the secret key in HMAC solution?

Related Related

  1. 1

    Best practice for using ssh key pair with different groups?

  2. 2

    Best practice when assigning reference type properties

  3. 3

    Creating your own flutter widgets best practice

  4. 4

    which is best coding practice, assigning to the variable or fetch from database

  5. 5

    Re-Assigning values on a pandas DataFrame best practice

  6. 6

    What is the best practice for using id as primary key?

  7. 7

    Is using the Class instance as a Map key a best practice?

  8. 8

    MySQL table without primary key (best practice)

  9. 9

    Best practice to store App Key in Laravel

  10. 10

    Store.putRight(..) and best practice for key selection

  11. 11

    Cassandra best practice to ORDER BY using PRIMARY KEY

  12. 12

    Best practice for handling api key in Play framework

  13. 13

    Ngrx effect dispatch different actions - best practice

  14. 14

    Best practice to run Linux service as a different user

  15. 15

    Best practice for calling two different endpoints for a widget?

  16. 16

    What is best practice to implement SQS on different environments?

  17. 17

    Best practice for dynamically translating content into different languages

  18. 18

    Kubernetes best practice: different config for local or remote

  19. 19

    Assigning values to Flutter widgets

  20. 20

    What is best practice for instantiating a variable for use outside of a loop if you are only assigning to it within the loop?

  21. 21

    What is a best practice naming convention for DOJO widgets that can be initiated only once?

  22. 22

    Best practice to update the Application Insights instrumentation key for classic Desktop apps

  23. 23

    Best practice to implement key-value pair in android Spinner

  24. 24

    In MVC whats best practice for placing primary key in table row

  25. 25

    Best Practice to check for multiple array_key_exists in PHP

  26. 26

    Laravel best practice method for querying using an index key in table

  27. 27

    Best practice for storing and retrieving a list of strings in a single JSON key?

  28. 28

    Best practice when using an API key in Node.js

  29. 29

    What to use (best/good practice) for the secret key in HMAC solution?

HotTag

Archive