- Flutter 3.x 项目初始化(pubspec.yaml + 目录结构) - 核心依赖:dio/go_router/provider/shared_preferences/hive/geolocator - 统一登录页 + Token管理(TokenService + AuthInterceptor) - 底部Tab三合一导航(供水/巡检/营收) - 供水管理Tab:监测数据列表(MonitorListPage) - 巡检Tab:任务列表+状态筛选+进度展示(PatrolTaskListPage) - 营收Tab:抄表页面+账单列表(MeterReadingPage/BillListPage) - 消息推送服务(PushService) - GPS定位服务(LocationService) - 拍照/相册服务(CameraService) - 离线缓存服务(CacheService) - Android/iOS打包配置+权限声明 - GoRouter路由+Auth认证守卫 - Provider状态管理
174 lines
5.8 KiB
Dart
174 lines
5.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../services/auth_provider.dart';
|
|
|
|
/// 登录页面
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _obscurePassword = true;
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _handleLogin() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
final authProvider = Provider.of<AuthProvider>(context, listen: false);
|
|
final success = await authProvider.login(
|
|
username: _usernameController.text.trim(),
|
|
password: _passwordController.text,
|
|
);
|
|
|
|
if (success && mounted) {
|
|
// 登录成功后路由由 AuthGuard 自动处理
|
|
Navigator.of(context).pushReplacementNamed('/main');
|
|
} else if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(authProvider.errorMessage ?? '登录失败'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
// Logo 和应用名
|
|
const Icon(
|
|
Icons.water_drop,
|
|
size: 80,
|
|
color: Color(0xFF1976D2),
|
|
),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'供水管理系统',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Color(0xFF1976D2),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'供水 · 巡检 · 营收',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
color: Colors.grey[600],
|
|
),
|
|
),
|
|
const SizedBox(height: 48),
|
|
|
|
// 用户名输入
|
|
TextFormField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(
|
|
labelText: '用户名',
|
|
prefixIcon: Icon(Icons.person),
|
|
hintText: '请输入用户名',
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return '请输入用户名';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 密码输入
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: InputDecoration(
|
|
labelText: '密码',
|
|
prefixIcon: const Icon(Icons.lock),
|
|
hintText: '请输入密码',
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscurePassword ? Icons.visibility_off : Icons.visibility,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
obscureText: _obscurePassword,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入密码';
|
|
}
|
|
if (value.length < 6) {
|
|
return '密码长度不能少于6位';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 32),
|
|
|
|
// 登录按钮
|
|
Consumer<AuthProvider>(
|
|
builder: (context, auth, child) {
|
|
return ElevatedButton(
|
|
onPressed: auth.isLoading ? null : _handleLogin,
|
|
child: auth.isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
color: Colors.white,
|
|
),
|
|
)
|
|
: const Text(
|
|
'登 录',
|
|
style: TextStyle(fontSize: 16),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 版本信息
|
|
Text(
|
|
'v1.0.0',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.grey[400], fontSize: 12),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|