flutter上拉加载更多下拉刷新

在pubspec.yaml中
  pull_to_refresh: 1.6.3 
 
页面中
 RefreshController _refreshController;
 int pageNo = 1;
  @override
  void initState() {
    super.initState();
    _refreshController = RefreshController(initialRefresh: false);
  }

  // 下拉刷新
  void _onRefresh() async {
    pageNo = 1;
  _refreshController.loadComplete();
  }

  // 上拉加载更多
  void _onLoading() async {
    pageNo++;
  _refreshController.loadComplete();
  }
RefreshConfiguration.copyAncestor(
        context: context,
        // Viewport不满一屏时,禁用上拉加载更多功能,应该配置更灵活一些,比如说一页条数大于等于总条数的时候设置或者总条数等于0
        hideFooterWhenNotFull: true,
        child: SmartRefresher(
          enablePullDown: true,
          enablePullUp: true,
          header: WaterDropHeader(),
          controller: _refreshController,
          onRefresh: _onRefresh,
          onLoading: _onLoading,
          child: CustomScrollView(
            slivers: [
              SliverList(
                delegate: SliverChildBuilderDelegate((BuildContext context, int i) {
                  return BListItem(
                    height: 50,
                    enableChecked: true,
                    title: data[i].name,
                    onTap: () => onTap(data[i]),
                    titleStyle: TextStyle(fontSize: 15, color: Colors.black),
                    isChecked: _activeCode == data[i].code,
                  );
                }, childCount: data?.length ?? 0),
              ),
            ],
          ),
        ),
      ),
 
需要注意的就是SmartRefresher的child需要直接加这种CustomScrollView或者listview等可滚动的组件
posted @ 2021-05-08 16:14  文磊啊~  阅读(948)  评论(0编辑  收藏  举报