Flutter 轻松构建加载更多(loading more),十位互联网一线高工手写Android高级知识

本文详细介绍了如何在Flutter中构建加载更多功能,通过示例代码展示了如何使用ListView和GridView实现,同时还涉及SliverList和SliverGrid的用法。文章强调了数据源的管理和UI组件的选择,提供了一个完整的ListView Demo和SliverList Demo。
摘要由CSDN通过智能技术生成

// TODO: implement onRefresh
pageindex = 1;
return loadMore();
}

@override
Future loadData() async {
// TODO: implement getData
String url = “”;
if (this.length == 0) {
url = “https://api.tuchong.com/feed-app”;
} else {
int lastPostId = this[this.length - 1].post_id;
url =
“https://api.tuchong.com/feed-app?post_id=KaTeX parse error: Expected 'EOF', got '&' at position 13: {lastPostId}&̲page={pageindex}&type=loadmore”;
}
bool isSuccess = false;
try {
//to show loading more clearly, in your app,remove this
await Future.delayed(Duration(milliseconds: 500, seconds: 1));

var result = await HttpFactory.getInstance().getHttpClient().get(url);

var source = TuChongSource.fromJson(json.decode(result.body));
if (pageindex == 1) {
this.clear();
}

source.feedList.forEach((item) {
if (item.hasImage && !this.contains(item) && hasMore) {
this.add(item);
}
});

_hasMore = source.feedList.length != 0;
pageindex++;
isSuccess = true;
} catch (exception) {
isSuccess = false;
print(exception);
}
return isSuccess;
}
}

将你请求列表的代码加到getData方法里面,这样数据源的准备就好了。

下面说说UI组件 这一部分分为ListView/GridView 和SliverList/SliverGrid

ListView/GridView

LoadingMoreList 里面的部分代码,StreamBuilder为更新UI,NotificationListener为了监听滑动状态

class LoadingMoreList extends StatelessWidget {
final ListConfig listConfig;

LoadingMoreList(this.listConfig,{Key key})
super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder(
builder: (d, s) {
return NotificationListener(
//key: _key,
onNotification: _handleScrollNotification,
child: NotificationListener(
onNotification: _handleGlowNotification,
child: listConfig.buildContent(context, s.data)),
);
},
stream: listConfig.sourceList?.rebuild,
);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值