133 lines
4.3 KiB
Dart
133 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
|
import 'package:sweet_chat_app/apis/auth_api.dart';
|
|
import 'package:sweet_chat_app/utils/SpUtil.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();
|
|
|
|
void _login() async {
|
|
try {
|
|
final auth = await loginApi(_usernameController.text, _passwordController.text);
|
|
await SpUtil.setInt('userId', auth.userId);
|
|
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (_) => const HomePage(),
|
|
),
|
|
);
|
|
|
|
Fluttertoast.showToast(
|
|
msg: "登录成功",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.TOP,
|
|
backgroundColor: Colors.white,
|
|
textColor: Colors.black,
|
|
);
|
|
} catch (e) {
|
|
Fluttertoast.showToast(
|
|
msg: "登录失败",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.TOP,
|
|
backgroundColor: Colors.white,
|
|
textColor: Colors.black,
|
|
);
|
|
}
|
|
}
|
|
|
|
@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)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|