david-legend / otp_textfield

BSD 3-Clause "New" or "Revised" License
63 stars 40 forks source link

Clear the form and start again #27

Open MobiNeil opened 3 months ago

MobiNeil commented 3 months ago

How do you clear all the fields and allow the user to start again?

Even is I use 'handleControllers' to access the individual fields and clear them all, then start entering digits from the first field, the 'onSubmit()' function still gets called with the old values.

The intention is that if the user enters an incorrect 4 digit code, that the form clears and they start again.

Nijeekeel commented 1 month ago

I can think of 2 ways to do this: 1) use the clearText Properties provided. You can put the following setState(() {clearText = true; }) in a button to clear the text. You can refer to the implementation here: https://github.com/david-legend/otp_textfield/blob/master/lib/flutter_otp_text_field.dart#L110 2) Rebuild the whole OtpTextField widget. Can be slightly more expensive. Can refer here on how to rebuild stateful widget: https://stackoverflow.com/questions/69034391/force-rebuild-of-a-stateful-child-widget-in-flutter