在不覆盖旧项目的情况下获取更多项目来列表视图

发布于 2025-01-22 15:47:24 字数 3564 浏览 1 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文