Page·Free
Sign-in screen (Flutter)
A Material 3 sign-in screen for Flutter. It checks the email and password, can show or hide the password, supports autofill and shows a spinner while it signs in. Colours come from your app's theme, and it needs only the Flutter SDK.
Free to install.
If PocketCode is installed on your Android device, this opens the page in the app.
Preview
login_screen.dart · 190 lines
// Sign-in screen for Flutter with Material 3, using only the Flutter SDK.
//
// Email and password with checks, a show/hide button for the password, autofill from the
// password manager, a spinner while it signs in and a message if it fails. It takes its
// colours from your app's Theme, so it follows light and dark mode.
//
// LoginScreen(
// onSignIn: (email, password) => auth.signIn(email, password),
// onForgotPassword: () => Navigator.pushNamed(context, '/reset'),
// onCreateAccount: () => Navigator.pushNamed(context, '/signup'),
// )
//
// onSignIn should throw if the sign-in fails; the screen then shows a message and stays.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({
super.key,
required this.onSignIn,
this.onForgotPassword,
this.onCreateAccount,
});
/// Called with the trimmed email and the password once both pass the checks.
final Future<void> Function(String email, String password) onSignIn;
/// Shows a "Forgot password?" link when set.
final VoidCallback? onForgotPassword;
/// Shows "No account yet? Create one" when set.
final VoidCallback? onCreateAccount;
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
static final _emailPattern = RegExp(r'^[^\s@]+@[^\s@]+\.[^\s@]+$');
final _formKey = GlobalKey<FormState>();
final _email = TextEditingController();
final _password = TextEditingController();
final _passwordFocus = FocusNode();
bool _obscure = true;
bool _busy = false;
String? _error;
@override
void dispose() {
_email.dispose();
_password.dispose();
_passwordFocus.dispose();
super.dispose();
}
Future<void> _submit() async {
if (_busy) return;
setState(() => _error = null);
if (!(_formKey.currentState?.validate() ?? false)) return;
setState(() => _busy = true);
try {
await widget.onSignIn(_email.text.trim(), _password.text);
// Lets the password manager offer to save what was typed.
TextInput.finishAutofillContext();
} catch (_) {
if (mounted) {
setState(() => _error = 'Could not sign in. Check your email and password and try again.');
}
} finally {
if (mounted) setState(() => _busy = false);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final muted = theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSurfaceVariant);
This preview is shortened. You get the full content when you install it.
Changes in 1.0.0
First version.