Files
water-management-system/mobile/lib/pages/login/login_page.dart
T

45 lines
1.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../services/auth_service.dart';
import '../home/home_page.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _userCtrl = TextEditingController(text: 'admin');
final _passCtrl = TextEditingController(text: 'admin123');
bool _loading = false;
Future<void> _login() async {
setState(() => _loading = true);
final ok = await context.read<AuthService>().login(_userCtrl.text, _passCtrl.text);
if (!mounted) return;
setState(() => _loading = false);
if (ok) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => const HomePage()));
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('登录失败')));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(padding: const EdgeInsets.all(32), child: Column(mainAxisSize: MainAxisSize.min, children: [
const Text('智慧水务', style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.blue)),
const SizedBox(height: 32),
TextField(controller: _userCtrl, decoration: const InputDecoration(labelText: '用户名', prefixIcon: Icon(Icons.person))),
const SizedBox(height: 16),
TextField(controller: _passCtrl, obscureText: true, decoration: const InputDecoration(labelText: '密码', prefixIcon: Icon(Icons.lock))),
const SizedBox(height: 24),
SizedBox(width: double.infinity, height: 48, child: ElevatedButton(onPressed: _loading ? null : _login, child: Text(_loading ? '登录中...' : '登录'))),
])),
),
);
}
}