Forgot Password
Loading preview
Starting the Flutter app
Email input, success state, back to login
import 'package:flutter/material.dart';
final _emailPattern = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$",
);
class ForgotPasswordPage extends StatefulWidget {
/// Called by 'Back to Login'. Without it the demo shows a SnackBar.
final VoidCallback? onBackToLogin;
const ForgotPasswordPage({super.key, this.onBackToLogin});
@override
State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}
class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
final _formKey = GlobalKey<FormState>();
var _sent = false;
void _backToLogin() {
final onBackToLogin = widget.onBackToLogin;
if (onBackToLogin != null) {
onBackToLogin();
return;
}
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(content: Text('Back to login (demo)')));
}
void _submit() {
if (!(_formKey.currentState?.validate() ?? false)) return;
// Await your password-reset request here.
setState(() => _sent = true);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Forgot Password')),
body: _sent ? _buildSuccessState() : _buildForm(),
);
}
Widget _buildForm() {
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 32),
Text(
'Enter your email to receive a reset link.',
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
TextFormField(
decoration: const InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
validator: (v) {
if (v == null || v.trim().isEmpty) return 'Required';
if (!_emailPattern.hasMatch(v.trim())) {
return 'Enter a valid email';
}
return null;
},
),
const SizedBox(height: 24),
FilledButton(
onPressed: _submit,
child: const Text('Send Reset Link'),
),
const SizedBox(height: 16),
TextButton(
onPressed: _backToLogin,
child: const Text('Back to Login'),
),
],
),
),
);
}
Widget _buildSuccessState() {
return Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.check_circle,
size: 80,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 24),
Text(
'Check your email',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
'We\'ve sent a password reset link to your email address.',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
FilledButton(
onPressed: _backToLogin,
child: const Text('Back to Login'),
),
],
),
),
);
}
}
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
A single boolean, _sent, switches the body between _buildForm and _buildSuccessState. _submit runs the TextFormField validator, which rejects empty and malformed addresses, before setting _sent. No request is made, so the success screen is only a demonstration. Both Back to Login buttons call onBackToLogin, or show a demo SnackBar without it.
Adapting it
- Await your reset call in
_submitbefore setting_sent. - Track a loading flag next to
_sentto disable the button while the request runs. - Soften the copy in
_buildSuccessStateto “if an account exists” so the page can’t be used to discover registered emails.