在不覆盖旧项目的情况下获取更多项目来列表视图
我想使用Hacker News API对我的应用程序进行分页,我希望在向下滚动并将新项目添加到首页中的旧项目时加载更多数据。
这就是项目页面的代码,我使用一个bloc模式我wend变量<代码> size to the the the event ant the bloc中的事件i在第一个加载中仅获取6个项目,当我向下滚动时,将6个项目添加到我的循环...
class _TopPageState extends State<TopPage> {
@override
void initState() {
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
size = size + 6;
context.read<ItemBloc>().add(GetTopEvent(size));
}
});
super.initState();
}
@override
void dispose() {
super.dispose();
_scrollController.dispose();
}
ScrollController _scrollController = new ScrollController();
int size = 6;
bool isLoading = false;`enter code here`
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: BlocBuilder<ItemBloc, ItemState>(
builder: (context, state) {
if (state.topState == RequestState.LOADING) {
return const CircularProgressIndicator();
} else if (state.topState == RequestState.ERROR) {
print(state.errorMsg);
return Container();
}else if (state.topState == RequestState.NONE) {
context.read<ItemBloc>().add(GetTopEvent(size));
return Container();
}
else if (state.topState == RequestState.LOADED) {
return NotificationListener<ScrollNotification>(
onNotification: (notification) {
if (notification is ScrollUpdateNotification) {
_getMoreData(notification.metrics);
}
return true;
},
child: ListView.builder(
controller: _scrollController,
padding: const EdgeInsets.symmetric(vertical: 8.0),
itemCount: state.reposTop.length,
itemBuilder: (context, index) {
return GestureDetector(
child: Item(state.reposTop[index]),
onTap: () {
if (state.reposTop[index].url != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
WebViewPage(state.reposTop[index])),
);
} else if (state.reposTop[index].text != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ItemDetails(state.reposTop[index])),
);
}
});
}),
);
} else {
return Container();
}
},
),
),
);
}
void _getMoreData(ScrollMetrics metrics) {
if (_scrollController.position.pixels ==
_scrollController.position.minScrollExtent) {
//&& size != 5
if( size - 6 <=0){
context.read<ItemBloc>().add(GetTopEvent(size));
}
else{
size = size - 6;
}
context.read<ItemBloc>().add(GetTopEvent(size));
}
}
}
I want to make a pagination to my application using hacker news api, I want load more data when I scroll down and add new items to old items I have in the first page.
That's the code of the item page, I use a bloc pattern I wend variable named size
to the bloc withe the event ant in the bloc I fetch only 6 items in the first load and when I scroll down I add 6 item to my loop ...
class _TopPageState extends State<TopPage> {
@override
void initState() {
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
size = size + 6;
context.read<ItemBloc>().add(GetTopEvent(size));
}
});
super.initState();
}
@override
void dispose() {
super.dispose();
_scrollController.dispose();
}
ScrollController _scrollController = new ScrollController();
int size = 6;
bool isLoading = false;`enter code here`
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: BlocBuilder<ItemBloc, ItemState>(
builder: (context, state) {
if (state.topState == RequestState.LOADING) {
return const CircularProgressIndicator();
} else if (state.topState == RequestState.ERROR) {
print(state.errorMsg);
return Container();
}else if (state.topState == RequestState.NONE) {
context.read<ItemBloc>().add(GetTopEvent(size));
return Container();
}
else if (state.topState == RequestState.LOADED) {
return NotificationListener<ScrollNotification>(
onNotification: (notification) {
if (notification is ScrollUpdateNotification) {
_getMoreData(notification.metrics);
}
return true;
},
child: ListView.builder(
controller: _scrollController,
padding: const EdgeInsets.symmetric(vertical: 8.0),
itemCount: state.reposTop.length,
itemBuilder: (context, index) {
return GestureDetector(
child: Item(state.reposTop[index]),
onTap: () {
if (state.reposTop[index].url != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
WebViewPage(state.reposTop[index])),
);
} else if (state.reposTop[index].text != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ItemDetails(state.reposTop[index])),
);
}
});
}),
);
} else {
return Container();
}
},
),
),
);
}
void _getMoreData(ScrollMetrics metrics) {
if (_scrollController.position.pixels ==
_scrollController.position.minScrollExtent) {
//&& size != 5
if( size - 6 <=0){
context.read<ItemBloc>().add(GetTopEvent(size));
}
else{
size = size - 6;
}
context.read<ItemBloc>().add(GetTopEvent(size));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论