Sub topic 5
Restricting and Shaping Input with Formatters
This chapter assumes you know basic Dart, Flutter widgets, StatefulWidget, and layouts such as Column. An input formatter changes the text edit before Flutter places it in a TextField or TextFormField. This lets you restrict characters, limit length, or normalize input while the user types.
Keyboard type is not validation
keyboardType chooses the keyboard layout suggested by the platform. It does not prevent invalid input. A numeric keyboard can still allow pasted letters or platform-specific characters.
inputFormatters enforce editing rules:
Use both settings when they serve different purposes:
- Use
keyboardTypeto improve the typing experience. - Use
inputFormattersto enforce the allowed shape. - Use a validator to check whether the completed value is acceptable.
Built-in formatters
Flutter provides common formatters in package:flutter/services.dart. This example accepts only digits and limits the value to five characters:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MaterialApp(home: ZipCodePage()));
}
class ZipCodePage extends StatelessWidget {
const ZipCodePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('ZIP code')),
body: Padding(
padding: const EdgeInsets.all(16),
child: TextField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(5),
],
decoration: const InputDecoration(
labelText: 'ZIP code',
hintText: '12345',
),
),
),
);
}
}FilteringTextInputFormatter.digitsOnly removes non-digit characters. LengthLimitingTextInputFormatter prevents the text from exceeding five characters. The order matters when you combine formatters. Each formatter receives the result produced by the previous one.
Other useful formatters include:
FilteringTextInputFormatter.allow(RegExp(...))to allow matching characters.FilteringTextInputFormatter.deny(RegExp(...))to remove matching characters.LengthLimitingTextInputFormatterto cap the length.TextInputFormatter.withFunctionfor a rule that accepts or rejects the complete edit.
Common errors
Do not treat a keyboard type as security or data validation. Always validate before saving or sending data.
Do not use a formatter as a replacement for a validator. A formatter controls editing, while a validator can show a meaningful error after the field is complete.
FilteringTextInputFormatter is best for filtering parts of a string. For a rule about the entire value, such as a strict pattern, use withFunction and return oldValue when the proposed edit is invalid. Also remember that formatters handle user edits. Setting controller.text in code can bypass the formatter, so normalize or validate programmatic values separately.
Exercise
Why should a ZIP-code field use both keyboardType and inputFormatters?
Show answer
keyboardType suggests a numeric keyboard, while inputFormatters enforce the digit-only and length rules even for pasted or unexpected input.