OTP Verification

Loading preview

Starting the Flutter app

Individual digit boxes, auto-focus, resend timer

Get the source

Add it to your project

Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.

First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.

How it works

Six one-character TextFields share the _controllers and _focusNodes lists, each Flexible up to 44px wide, and onChanged moves focus forward after a digit and back after a deletion. FilteringTextInputFormatter.digitsOnly rejects anything else, and _startResendTimer runs the 30-second countdown on a Timer.periodic cancelled in dispose. Backspace on an already empty box fires no onChanged, so focus stays put. Verify shows an error until all six boxes are filled, then passes the code to onVerify, or a demo SnackBar without it. Resend only restarts the countdown.

Adapting it

  • Send the code to your auth API from onVerify.
  • maxLength: 1 truncates pasted codes, so spread pasted digits across _controllers if you need paste.