【问题标题】:Pagination with ListView.bulider in Flutter在 Flutter 中使用 ListView.builder 进行分页
【发布时间】:2022-08-05 14:15:53
【问题描述】:

我是 Flutter 的新手,我一直在寻找分页方面的好结果。

Flutter listview 中的分页是一种在到达列表末尾时加载数据的方法。 分页用于部分加载数据。 Flutter listview中的分页,将数据按照page 1和page的方式进行分页。

每页需要加载10项数据列表 输入: 实现分页

输出:

<1 2 3 ... > 每页 10 项 ^

    标签: flutter


    【解决方案1】:

    您可以使用loadmore 包。

      body:  RefreshIndicator(
              child: LoadMore(
                isFinish: count >= 10,
                onLoadMore: _loadMore,
                child: ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                      child: Text(list[index].toString()),
                      alignment: Alignment.center,
                    );
                  },
                  itemCount: count,
                ),
                whenEmptyLoad: false,
                delegate: DefaultLoadMoreDelegate(),
                textBuilder: DefaultLoadMoreTextBuilder.chinese,
              ),
              onRefresh: _refresh,
            ),
    

    【讨论】:

      【解决方案2】:

      您可以使用 Pull To Refresh 包在 Flutter 中进行分页。

      【讨论】:

        【解决方案3】:

        你可以使用 Flutter 的 NotificationListener&lt;ScrollNotification&gt;(...) 小部件。

        样本:

        import 'package:flutter/material.dart';
        
        class PaginatedListView extends StatefulWidget {
          const PaginatedListView({
            required this.onNext,
            this.nextPageRatio = 1,
            this.hasNextPage = false,
            required this.child,
            this.loadingIndicator,
            super.key,
          }) : assert(
                  nextPageRatio >= 0 && nextPageRatio <= 1,
                  '[nextPageRatio] should be between 0...1',
                );
        
          final VoidCallback onNext;
          final double nextPageRatio;
          final bool hasNextPage;
          final SliverList child;
          final Widget? loadingIndicator;
        
          @override
          State<PaginatedListView> createState() => _PaginatedListViewState();
        }
        
        class _PaginatedListViewState extends State<PaginatedListView> {
          final ScrollController _controller = ScrollController();
        
          double _currentMaxScrollExtent = 0;
        
          @override
          void initState() {
            super.initState();
        
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (_controller.position.maxScrollExtent == 0 && widget.hasNextPage) {
                widget.onNext();
              }
            });
          }
        
          bool _onNotification(ScrollNotification notification) {
            if (!widget.hasNextPage) {
              return false;
            }
        
            final double maxScrollExtent =
                notification.metrics.maxScrollExtent * widget.nextPageRatio;
        
            if (notification.metrics.pixels >= maxScrollExtent &&
                _currentMaxScrollExtent < maxScrollExtent) {
              _currentMaxScrollExtent = maxScrollExtent;
              widget.onNext();
            }
        
            return false;
          }
        
          @override
          Widget build(BuildContext context) {
            return NotificationListener<ScrollNotification>(
              onNotification: _onNotification,
              child: CustomScrollView(
                controller: _controller,
                physics: const AlwaysScrollableScrollPhysics(),
                slivers: <Widget>[
                  widget.child,
                  if (widget.hasNextPage)
                    SliverToBoxAdapter(
                      child: Center(
                        child: widget.loadingIndicator ??
                            const CircularProgressIndicator(),
                      ),
                    ),
                ],
              ),
            );
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-04-15
          • 1970-01-01
          • 2021-01-13
          • 1970-01-01
          • 2021-04-30
          • 1970-01-01
          • 2019-06-25
          • 2021-04-16
          • 2021-06-24
          相关资源
          最近更新 更多