|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+import 'package:flutter/material.dart';
|
|
|
2
|
+import 'package:provider/provider.dart';
|
|
|
3
|
+import '../../services/auth_service.dart';
|
|
|
4
|
+import '../home/home_page.dart';
|
|
|
5
|
+
|
|
|
6
|
+class LoginPage extends StatefulWidget {
|
|
|
7
|
+ const LoginPage({super.key});
|
|
|
8
|
+ @override State<LoginPage> createState() => _LoginPageState();
|
|
|
9
|
+}
|
|
|
10
|
+
|
|
|
11
|
+class _LoginPageState extends State<LoginPage> {
|
|
|
12
|
+ final _userCtrl = TextEditingController(text: 'admin');
|
|
|
13
|
+ final _passCtrl = TextEditingController(text: 'admin123');
|
|
|
14
|
+ bool _loading = false;
|
|
|
15
|
+
|
|
|
16
|
+ Future<void> _login() async {
|
|
|
17
|
+ setState(() => _loading = true);
|
|
|
18
|
+ final ok = await context.read<AuthService>().login(_userCtrl.text, _passCtrl.text);
|
|
|
19
|
+ if (!mounted) return;
|
|
|
20
|
+ setState(() => _loading = false);
|
|
|
21
|
+ if (ok) {
|
|
|
22
|
+ Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => const HomePage()));
|
|
|
23
|
+ } else {
|
|
|
24
|
+ ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('登录失败')));
|
|
|
25
|
+ }
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ @override
|
|
|
29
|
+ Widget build(BuildContext context) {
|
|
|
30
|
+ return Scaffold(
|
|
|
31
|
+ body: Center(
|
|
|
32
|
+ child: Padding(padding: const EdgeInsets.all(32), child: Column(mainAxisSize: MainAxisSize.min, children: [
|
|
|
33
|
+ const Text('智慧水务', style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.blue)),
|
|
|
34
|
+ const SizedBox(height: 32),
|
|
|
35
|
+ TextField(controller: _userCtrl, decoration: const InputDecoration(labelText: '用户名', prefixIcon: Icon(Icons.person))),
|
|
|
36
|
+ const SizedBox(height: 16),
|
|
|
37
|
+ TextField(controller: _passCtrl, obscureText: true, decoration: const InputDecoration(labelText: '密码', prefixIcon: Icon(Icons.lock))),
|
|
|
38
|
+ const SizedBox(height: 24),
|
|
|
39
|
+ SizedBox(width: double.infinity, height: 48, child: ElevatedButton(onPressed: _loading ? null : _login, child: Text(_loading ? '登录中...' : '登录'))),
|
|
|
40
|
+ ])),
|
|
|
41
|
+ ),
|
|
|
42
|
+ );
|
|
|
43
|
+ }
|
|
|
44
|
+}
|