提交 cdb7b31f authored 作者: songchuancai's avatar songchuancai

增加登录功能(登录校验逻辑未增加,默认输入任何信息都会登录成功)

上级 13b88ee1
...@@ -11,6 +11,7 @@ import 'models/conversation.dart'; ...@@ -11,6 +11,7 @@ import 'models/conversation.dart';
import 'services/storage_service.dart'; import 'services/storage_service.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'models/chat_message.dart'; import 'models/chat_message.dart';
import 'models/user.dart';
class HomePage extends StatefulWidget { class HomePage extends StatefulWidget {
const HomePage({super.key}); const HomePage({super.key});
...@@ -350,7 +351,7 @@ class _HomePageState extends State<HomePage> { ...@@ -350,7 +351,7 @@ class _HomePageState extends State<HomePage> {
); );
} }
Widget _buildDrawer() { Widget _buildDrawer(BuildContext context) {
return Drawer( return Drawer(
child: Column( child: Column(
children: [ children: [
...@@ -420,6 +421,128 @@ class _HomePageState extends State<HomePage> { ...@@ -420,6 +421,128 @@ class _HomePageState extends State<HomePage> {
}, },
), ),
), ),
const Spacer(),
const Divider(height: 1),
FutureBuilder<User?>(
future: StorageService.getUser(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const SizedBox();
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.grey[50],
),
child: Column(
children: [
Row(
children: [
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Pallete.firstSuggestionBoxColor,
shape: BoxShape.circle,
),
child: Center(
child: Text(
snapshot.data!.username[0].toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
snapshot.data!.username,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
'在线',
style: TextStyle(
fontSize: 14,
color: Colors.green[600],
),
),
],
),
),
],
),
const SizedBox(height: 16),
InkWell(
onTap: () async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('确认退出'),
content: const Text('您确定要退出登录吗?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('取消'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text(
'退出',
style: TextStyle(color: Colors.red),
),
),
],
),
);
if (confirmed == true && context.mounted) {
await StorageService.clearUser();
Navigator.of(context).pushReplacementNamed('/login');
}
},
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 16,
),
decoration: BoxDecoration(
color: Colors.red[50],
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.logout,
color: Colors.red[700],
size: 20,
),
const SizedBox(width: 8),
Text(
'退出登录',
style: TextStyle(
color: Colors.red[700],
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
],
),
),
),
],
),
);
},
),
], ],
), ),
); );
...@@ -429,7 +552,7 @@ class _HomePageState extends State<HomePage> { ...@@ -429,7 +552,7 @@ class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: _buildAppBar(), appBar: _buildAppBar(),
drawer: _buildDrawer(), drawer: _buildDrawer(context),
body: Column( body: Column(
children: [ children: [
Expanded( Expanded(
......
import 'package:allen/home_page.dart'; import 'package:allen/home_page.dart';
import 'package:allen/pallete.dart'; import 'package:allen/pallete.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:allen/pages/login_page.dart';
import 'package:allen/services/storage_service.dart';
import 'package:allen/models/user.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
...@@ -20,7 +25,19 @@ class MyApp extends StatelessWidget { ...@@ -20,7 +25,19 @@ class MyApp extends StatelessWidget {
backgroundColor: Pallete.whiteColor, backgroundColor: Pallete.whiteColor,
), ),
), ),
home: const HomePage(), home: FutureBuilder<User?>(
future: StorageService.getUser(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
return snapshot.hasData ? const HomePage() : const LoginPage();
},
),
routes: {
'/home': (context) => const HomePage(),
'/login': (context) => const LoginPage(),
},
); );
} }
} }
class User {
final String username;
final String password;
User({required this.username, required this.password});
Map<String, dynamic> toJson() {
return {
'username': username,
'password': password,
};
}
factory User.fromJson(Map<String, dynamic> json) {
return User(
username: json['username'],
password: json['password'],
);
}
}
import 'package:flutter/material.dart';
import 'package:allen/models/user.dart';
import 'package:allen/services/storage_service.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 _handleLogin() async {
final username = _usernameController.text;
final password = _passwordController.text;
if (username.isEmpty || password.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('请输入用户名和密码')),
);
return;
}
final user = User(username: username, password: password);
await StorageService.saveUser(user);
if (mounted) {
Navigator.of(context).pushReplacementNamed('/home');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: '用户名',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: '密码',
border: OutlineInputBorder(),
),
obscureText: true,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _handleLogin,
child: const Text('登录'),
),
],
),
),
);
}
}
import 'dart:convert'; import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import '../models/conversation.dart'; import '../models/conversation.dart';
import 'package:allen/models/user.dart';
class StorageService { class StorageService {
static const String _conversationsKey = 'conversations'; static const String _conversationsKey = 'conversations';
...@@ -44,4 +45,23 @@ class StorageService { ...@@ -44,4 +45,23 @@ class StorageService {
conversations.removeWhere((conv) => conv.id == id); conversations.removeWhere((conv) => conv.id == id);
await saveConversations(conversations); await saveConversations(conversations);
} }
static const String _userKey = 'user';
static Future<void> saveUser(User user) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_userKey, jsonEncode(user.toJson()));
}
static Future<User?> getUser() async {
final prefs = await SharedPreferences.getInstance();
final userJson = prefs.getString(_userKey);
if (userJson == null) return null;
return User.fromJson(jsonDecode(userJson));
}
static Future<void> clearUser() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_userKey);
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论