Files
sweet_chat_app/lib/pages/login.dart
2026-06-20 16:11:57 +08:00

129 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
import 'package:sweet_chat_app/apis/auth_api.dart';
import 'package:sweet_chat_app/services/notify_service.dart';
import 'package:sweet_chat_app/utils/sp_utils.dart';
import 'package:sweet_chat_app/utils/toast_utils.dart';
import 'home_page.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
// final NotifyService notifyService = NotifyService();
void _login() async {
try {
// notifyService.showInstantNotification(title: '你好', body: '测试消息');
final auth = await loginApi(
_usernameController.text,
_passwordController.text,
);
await SpUtil.setInt('userId', auth.user.id);
await SpUtil.setString('nickname', auth.user.nickname);
await SpUtil.setString('avatarUrl', auth.user.avatarUrl);
Navigator.of(
context,
).push(MaterialPageRoute(builder: (_) => const HomePage()));
ToastUtils.success("登录成功");
} catch (e) {
print(e.toString());
ToastUtils.error("登录失败");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
Center(
child: GlassCard(
width: 340,
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Sweet Chat',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 24),
// 用户名
TextField(
controller: _usernameController,
style: const TextStyle(color: Colors.white),
cursorColor: Colors.white,
decoration: InputDecoration(
hintText: '请输入用户名',
hintStyle: TextStyle(color: Colors.white70),
filled: true,
fillColor: Colors.white.withAlpha(50),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none,
),
),
),
const SizedBox(height: 16),
// 密码
TextField(
controller: _passwordController,
obscureText: true,
style: const TextStyle(color: Colors.white),
cursorColor: Colors.white,
decoration: InputDecoration(
hintText: '请输入密码',
hintStyle: TextStyle(color: Colors.white70),
filled: true,
fillColor: Colors.white.withAlpha(50),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none,
),
),
),
const SizedBox(height: 28),
// 登录按钮
ElevatedButton(
onPressed: _login,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white.withAlpha(50),
foregroundColor: Colors.white,
minimumSize: const Size.fromHeight(50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 0,
),
child: const Text('登录', style: TextStyle(fontSize: 18)),
),
],
),
),
),
],
),
);
}
}