Sub topic 4
Configuring the Keyboard and Obscured Text
Assume you know basic Dart, nullable types, closures, StatefulWidget, setState, and layouts such as Column and Scaffold. This chapter focuses on how a text field behaves while the user enters data. It does not replace validation. Choosing a number keyboard, for example, does not guarantee that the value is numeric.
Choosing the keyboard and action
keyboardType is a hint to the platform about which keyboard to display. Common choices include:
| Setting | Typical use |
|---|---|
| `TextInputType.emailAddress` | Email addresses |
| `TextInputType.number` | Numeric values |
| `TextInputType.phone` | Phone numbers |
| `TextInputType.url` | Web addresses |
| `TextInputType.text` | General text |
textInputAction controls the action button on the software keyboard. Use next when another field follows, and done when the user has finished. The action button does not submit anything automatically. Handle it with onSubmitted.
Hiding sensitive text
Set obscureText: true for passwords, PINs, and similar values. Flutter replaces the displayed characters with obscuring symbols, but the underlying value remains available to the controller or callback.
Obscuring text is a display feature. It does not encrypt the value, protect an insecure network request, or prevent screenshots on every platform. Use HTTPS and appropriate platform security when sending sensitive data.
autofillHints gives the operating system context for saved credentials and autofill services. Place related fields inside AutofillGroup when they belong to the same form.
A complete example
This example moves focus from the email field to the password field when the user presses the keyboard's next action.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: LoginPage()));
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _passwordFocus = FocusNode();
@override
void dispose() {
_passwordFocus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Sign in')),
body: Padding(
padding: const EdgeInsets.all(16),
child: AutofillGroup(
child: Column(
children: [
TextField(
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
autofillHints: const [AutofillHints.username],
decoration: const InputDecoration(
labelText: 'Email',
hintText: '[email protected]',
),
onSubmitted: (_) => _passwordFocus.requestFocus(),
),
TextField(
focusNode: _passwordFocus,
obscureText: true,
textInputAction: TextInputAction.done,
autofillHints: const [AutofillHints.password],
decoration: const InputDecoration(labelText: 'Password'),
onSubmitted: (_) => _passwordFocus.unfocus(),
),
],
),
),
),
);
}
}FocusNode represents the keyboard focus of a field. Because this widget owns _passwordFocus, it creates the node once and disposes of it when the widget is removed. Calling requestFocus moves focus without requiring the user to tap the next field.
How the settings work together
Exercise
Which setting hides a password on screen, and which callback can react when the user presses the keyboard action button?
Show answer
Set obscureText: true to hide the password. Use onSubmitted to react to the keyboard action.