智慧水务管理系统 - 精河县供水工程综合管理平台

login_page.dart 1.8KB

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