Search code examples
flutterflutter-layout

How to know when user delete the input in VerificationCodeInput Flutter


I want to make a verification code input in my app. I'm using this widget to do that. But I don't know how to detect if the user delete the number that he/she inputted after he fully input in the widget so I can disable my submit button. I did the following. How can I know whether the user delete the number he/she inputted after he/she fully input in the field? Thanks!

VerificationCodeInput(
 keyboardType: TextInputType.number,
 length: 4,
 onCompleted: (String value) {
  setState(() {
   full = true;
  });
 },
),

Solution

  • You can go to the source https://github.com/tiny-express/flutter_verification_code_input and see in code developers made - logic that you want is not provided.

    So, you have possibility to fork this package and implement what you need by yourself.

    Just an example I made the fork https://github.com/awaik/flutter_verification_code where implemented one more check for editing.

    It works like on attached screen.

    For implementing it in your code you should change your pubspec.yaml with this new package

    dependencies:
      flutter:
        sdk: flutter
      flutter_verification_code: ^0.1.4
    

    And in code you can use it like this

      body: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Center(
              child: Text(
                'Enter your code',
                style: TextStyle(fontSize: 20.0),
              ),
            ),
          ),
          VerificationCodeInput(
            keyboardType: TextInputType.number,
            length: 4,
            autofocus: false,
            onCompleted: (String value) {
              print(value);
              setState(() {
                _code = value;
              });
            },
            onEditing: (bool value) {
              setState(() {
                _onEditing = value;
              });
            },
          ),
          (_onEditing != true)
              ? Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Center(
                    child: Text(
                      'Your code: $_code',
                    ),
                  ),
                )
              : Container(
                  child: Text(
                    'Please enter full code',
                  ),
                ),
        ],
    

    Full example here https://pub.dev/packages/flutter_verification_code

    (and of course great thanks for original developers)

    enter image description here