One-time code
A one-time code field collects a short code as one value, even when it draws separate places for each digit. Use it when the code length is known in advance.
Enter a code
#Enter or paste 1234. The feedback changes when all four places are filled. Delete one digit to return to the partial state.
components/one-time-code/appliance/enter_code.dart
import 'package:flutter/widgets.dart';
import 'package:appliance_ui/appliance_ui.dart';
class EnterCode extends StatefulWidget {
const EnterCode({super.key});
@override
State<EnterCode> createState() => _EnterCodeState();
}
class _EnterCodeState extends State<EnterCode> {
int digits = 0;
@override
Widget build(BuildContext context) => ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 360),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ApplianceOtpField(
semanticLabel: 'Verification code',
length: 4,
onChanged: (value) => setState(() => digits = value.length),
),
const SizedBox(height: 12),
Text(digits == 4 ? 'Code complete' : 'Digits entered: $digits of 4'),
],
),
);
}
Parameters
ApplianceOtpField API ↗semanticLabel- Names the single code entry.
length- Number of digits expected.
onChanged- Receives the whole value after each edit.
Shared behaviour
- Keyboard and focus. Tab to the code input. Type the digits together and use Backspace to edit.
- Screen readers. The input is named “Verification code”. Read the status beneath it to check how many digits are present.
- Direction and type. The code is a sequence with a fixed order. Compare its display in both directions, especially after enlarging the text.