Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
F
flutter-chat
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
songchuancai
flutter-chat
Commits
cdb7b31f
提交
cdb7b31f
authored
11月 07, 2024
作者:
songchuancai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加登录功能(登录校验逻辑未增加,默认输入任何信息都会登录成功)
上级
13b88ee1
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
253 行增加
和
4 行删除
+253
-4
home_page.dart
lib/home_page.dart
+125
-2
main.dart
lib/main.dart
+19
-2
user.dart
lib/models/user.dart
+20
-0
login_page.dart
lib/pages/login_page.dart
+69
-0
storage_service.dart
lib/services/storage_service.dart
+20
-0
没有找到文件。
lib/home_page.dart
浏览文件 @
cdb7b31f
...
@@ -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
(
...
...
lib/main.dart
浏览文件 @
cdb7b31f
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
(),
},
);
);
}
}
}
}
lib/models/user.dart
0 → 100644
浏览文件 @
cdb7b31f
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'
],
);
}
}
lib/pages/login_page.dart
0 → 100644
浏览文件 @
cdb7b31f
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
(
'登录'
),
),
],
),
),
);
}
}
lib/services/storage_service.dart
浏览文件 @
cdb7b31f
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论