Home > OS >  How to generate uuid every time when user typing in a textfield?
How to generate uuid every time when user typing in a textfield?

Time:08-03

How to generate uuid every time when user typing in a textfield and show the result in another textfield?

CodePudding user response:

in order to generate an uuid you can use this package: https://pub.dev/packages/uuid

For generate unique id in Texfield use, the onChange event.

For update the second TextField set - TextFieldController and update the .text from the on Change event.

// Declare a text controller
var _myController = TextEditingController()..text = '';

...

// The first TextField
TextField(
        textInputAction: TextInputAction.next,
        onChanged: (value) {
            var uuid = Uuid();
            _myController.text = uuid;
          }
        },
        keyboardType: TextInputType.text,
),
// Second TextField
TextField(
        controller: _myController,
        textInputAction: TextInputAction.next,
        keyboardType: TextInputType.text,
) 

This will generate a new unique id every time the user type a new character.

If you want to do the same on field validation juste change onChanged to onSubmitted.

CodePudding user response:

You can generate uuid evey time on onChanged

    TextField(
            onChanged: (value) {
            setState(() {
               var uuid = Uuid();
            }); 
         },
    )
  • Related