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
23ffd7b4
提交
23ffd7b4
authored
11月 08, 2024
作者:
songchuancai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加动态加载应用的功能
上级
28ae4749
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
77 行增加
和
4 行删除
+77
-4
apps_page.dart
lib/pages/apps_page.dart
+75
-2
apps_service.dart
lib/services/apps_service.dart
+2
-2
没有找到文件。
lib/pages/apps_page.dart
浏览文件 @
23ffd7b4
...
...
@@ -18,10 +18,60 @@ class _AppsPageState extends State<AppsPage> {
bool
isLoading
=
true
;
String
?
error
;
bool
_isLoadingMore
=
false
;
int
_currentPage
=
1
;
bool
_hasMoreData
=
true
;
final
ScrollController
_scrollController
=
ScrollController
();
static
const
int
_pageSize
=
4
;
@override
void
initState
()
{
super
.
initState
();
_initializeService
();
_scrollController
.
addListener
(
_onScroll
);
}
@override
void
dispose
()
{
_scrollController
.
dispose
();
super
.
dispose
();
}
void
_onScroll
()
{
if
(
_scrollController
.
position
.
pixels
>=
_scrollController
.
position
.
maxScrollExtent
*
0.8
)
{
_loadMoreData
();
}
}
Future
<
void
>
_loadMoreData
()
async
{
if
(
_isLoadingMore
||
!
_hasMoreData
)
return
;
setState
(()
{
_isLoadingMore
=
true
;
});
try
{
final
newApps
=
await
_appsService
.
getApps
(
page:
_currentPage
+
1
,
pageSize:
_pageSize
,
);
if
(
newApps
.
isEmpty
)
{
_hasMoreData
=
false
;
}
else
{
setState
(()
{
apps
.
addAll
(
newApps
);
_currentPage
++;
});
}
}
catch
(
e
)
{
// 处理错误,可以显示一个 snackbar 或 toast
}
finally
{
setState
(()
{
_isLoadingMore
=
false
;
});
}
}
Future
<
void
>
_initializeService
()
async
{
...
...
@@ -32,10 +82,15 @@ class _AppsPageState extends State<AppsPage> {
Future
<
void
>
fetchApps
()
async
{
try
{
final
appsList
=
await
_appsService
.
getApps
();
final
appsList
=
await
_appsService
.
getApps
(
page:
1
,
pageSize:
_pageSize
,
);
setState
(()
{
apps
=
appsList
;
isLoading
=
false
;
_currentPage
=
1
;
_hasMoreData
=
appsList
.
length
>=
_pageSize
;
});
}
catch
(
e
)
{
setState
(()
{
...
...
@@ -56,9 +111,14 @@ class _AppsPageState extends State<AppsPage> {
}
return
ListView
.
builder
(
controller:
_scrollController
,
padding:
const
EdgeInsets
.
all
(
16
),
itemCount:
apps
.
length
,
itemCount:
apps
.
length
+
(
_hasMoreData
?
1
:
0
)
,
itemBuilder:
(
context
,
index
)
{
if
(
index
==
apps
.
length
)
{
return
_buildLoadingIndicator
();
}
final
app
=
apps
[
index
];
return
Card
(
margin:
const
EdgeInsets
.
only
(
bottom:
16
),
...
...
@@ -113,4 +173,17 @@ class _AppsPageState extends State<AppsPage> {
},
);
}
Widget
_buildLoadingIndicator
()
{
return
Padding
(
padding:
const
EdgeInsets
.
all
(
16.0
),
child:
Center
(
child:
_isLoadingMore
?
const
CircularProgressIndicator
()
:
_hasMoreData
?
const
SizedBox
()
:
const
Text
(
'没有更多数据了'
),
),
);
}
}
lib/services/apps_service.dart
浏览文件 @
23ffd7b4
...
...
@@ -8,9 +8,9 @@ class AppsService {
AppsService
({
this
.
token
});
Future
<
List
<
AppItem
>>
getApps
({
int
page
=
1
,
int
limit
=
12
})
async
{
Future
<
List
<
AppItem
>>
getApps
({
int
page
=
1
,
int
pageSize
=
4
})
async
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
'
$baseUrl
/apps/?page=
$page
&
limit=
$limit
'
),
Uri
.
parse
(
'
$baseUrl
/apps/?page=
$page
&
pageSize=
$pageSize
'
),
headers:
{
'Content-Type'
:
'application/json'
,
'Accept'
:
'application/json'
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论