页面·免费
登录界面(Flutter)
Flutter 的 Material 3 登录界面:校验邮箱和密码,可显示或隐藏密码,支持自动填充,登录时显示加载动画。颜色取自你应用的主题,只需要 Flutter SDK。
免费安装。
如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。
预览
login_screen.dart · 190 行
// 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);
此预览已截短。安装后可获得完整内容。
1.0.0 的变更
首个版本。