| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../../providers/auth_provider.dart';
- import '../../../core/constants/route_constants.dart';
- import '../../widgets/common/app_button.dart';
- import '../../widgets/common/app_text_field.dart';
- import '../../widgets/common/loading_indicator.dart';
-
- class LoginScreen extends StatefulWidget {
- final VoidCallback? onSuccess;
-
- const LoginScreen({super.key, this.onSuccess});
-
- @override
- State<LoginScreen> createState() => _LoginScreenState();
- }
-
- class _LoginScreenState extends State<LoginScreen> {
- final _formKey = GlobalKey<FormState>();
- final _emailController = TextEditingController(text: 'test@example.com');
- final _passwordController = TextEditingController(text: '123456');
-
- @override
- void dispose() {
- _emailController.dispose();
- _passwordController.dispose();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- final authProvider = Provider.of<AuthProvider>(context);
-
- return Scaffold(
- appBar: AppBar(
- title: const Text('登录'),
- ),
- body: SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.all(24.0),
- child: Form(
- key: _formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const SizedBox(height: 40),
- const Center(
- child: Text(
- '欢迎回来',
- style: TextStyle(
- fontSize: 28,
- fontWeight: FontWeight.bold,
- ),
- ),
- ),
- const SizedBox(height: 8),
- const Center(
- child: Text(
- '请输入您的账号信息',
- style: TextStyle(
- fontSize: 16,
- color: Colors.grey,
- ),
- ),
- ),
- const SizedBox(height: 40),
- AppTextField(
- controller: _emailController,
- labelText: '邮箱地址',
- hintText: '请输入邮箱',
- keyboardType: TextInputType.emailAddress,
- prefixIcon: const Icon(Icons.email_outlined),
- validator: (value) {
- if (value == null || value.isEmpty) {
- return '请输入邮箱地址';
- }
- if (!value.contains('@')) {
- return '请输入有效的邮箱地址';
- }
- return null;
- },
- ),
- const SizedBox(height: 20),
- AppTextField(
- controller: _passwordController,
- labelText: '密码',
- hintText: '请输入密码',
- obscureText: true,
- prefixIcon: const Icon(Icons.lock_outline),
- validator: (value) {
- if (value == null || value.isEmpty) {
- return '请输入密码';
- }
- if (value.length < 6) {
- return '密码至少需要6位字符';
- }
- return null;
- },
- ),
- const SizedBox(height: 30),
- if (authProvider.error != null)
- Padding(
- padding: const EdgeInsets.only(bottom: 16),
- child: Text(
- authProvider.error!,
- style: const TextStyle(
- color: Colors.red,
- fontSize: 14,
- ),
- ),
- ),
- if (authProvider.isLoading)
- const LoadingIndicator()
- else
- AppButton(
- text: '登录',
- onPressed: () async {
- if (_formKey.currentState!.validate()) {
- await authProvider.login(
- _emailController.text.trim(),
- _passwordController.text,
- );
- if (authProvider.isAuthenticated) {
- widget.onSuccess?.call();
- Navigator.of(context).pushReplacementNamed(RouteConstants.home);
- }
- }
- },
- ),
- const SizedBox(height: 20),
- Center(
- child: TextButton(
- onPressed: () {
- Navigator.of(context).pushNamed(RouteConstants.register);
- },
- child: const Text(
- '还没有账号?立即注册',
- style: TextStyle(
- fontSize: 14,
- color: Colors.blue,
- ),
- ),
- ),
- ),
- const SizedBox(height: 30),
- const Row(
- children: [
- Expanded(child: Divider()),
- Padding(
- padding: EdgeInsets.symmetric(horizontal: 16),
- child: Text('或'),
- ),
- Expanded(child: Divider()),
- ],
- ),
- const SizedBox(height: 20),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- IconButton(
- onPressed: () {},
- icon: Container(
- padding: const EdgeInsets.all(10),
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(50),
- ),
- child: const Icon(Icons.wechat, color: Colors.green),
- ),
- ),
- IconButton(
- onPressed: () {},
- icon: Container(
- padding: const EdgeInsets.all(10),
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(50),
- ),
- child: const Icon(Icons.wechat, color: Colors.blue),
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- }
|