Skip to content
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.

Related

Login screen (Compose)FreePageEmail and password screen in Material 3. It checks both fields when you tap Sign in, shows the error your server returns, and can add Forgot password and fingerprint buttons. You call BiometricPrompt yourself.Contact form (HTML + CSS)FreePageA contact form in one HTML file: name, email, an optional topic and the message. Every field has a label, and a red border appears only after someone types in it or tries to send. Set the form's action to your endpoint. Dark mode follows the system.Settings screen (Compose)FreePageMaterial 3 settings screen with switches, a theme choice and a text size slider. It holds no state: you pass a SettingsState in and get the changed copy back. Each row is one touch target of 56 dp or more that TalkBack reads as a switch or radio button.Sign-up form (Vue)FreePageA sign-up form for Vue 3 as a single-file component in TypeScript, with name, email and password. When you leave a field, it says what is wrong, and screen readers announce the same message. It uses no libraries, and the colours come from CSS variables.Dart and Flutter snippetsFreeSnippets20 Dart and Flutter snippets: initState and dispose, Scaffold, FutureBuilder and StreamBuilder, ListView.builder, navigation, dialogs and SnackBars, a JSON model with copyWith, and unit and widget tests.Landing hero (Next.js + Tailwind)FreePageHero section for the Next.js App Router: a title, a subtitle and two links over a soft gradient. It uses dark colours when the system is in dark mode.