Flutter:如何在SliverAppBar中使用RefreshIndicator

Rob*_*yan 4 android ios dart flutter

我正在尝试向CustomScrollView添加刷新指示器。有可能吗?这是代码示例:

CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        title: Text('POSTAPP'),
        centerTitle: true,
        floating: true,
        pinned: false,
      ),
      SliverList(
          delegate: SliverChildBuilderDelegate(_createPostItem,
              childCount: postRepo.posts.length))
    ],
  );
Run Code Online (Sandbox Code Playgroud)

And*_*sky 20

只有这样:

RefreshIndicator(child: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: Text('POSTAPP'),
            centerTitle: true,
            floating: true,
            pinned: false,
          ),
          SliverList(
              delegate: SliverChildBuilderDelegate(_createPostItem,
                  childCount: postRepo.posts.length))
        ],
      ), onRefresh: () => Future.value(true));
Run Code Online (Sandbox Code Playgroud)

SliverList 不可滚动,因此您无法将其包装在 RefreshIndicator 中

  • 是否有任何选项可以在 SliverAppBar 和 SliverList 之间定位 RefreshIndicator ? (2认同)
  • @RobertApikyan您可以尝试使用 CupertinoSliverRefreshControl() 并修改其构建方法以根据需要显示自定义刷新指示器。快乐编码示例:https://gist.github.com/RabiRoshan/6ac289c26c2c726b145a69fa521e3c3d (2认同)

tom*_*wyr 8

如果您不一定要限制使用CustomScrollView,则可以通过以下方法实现所需的功能NestedScrollView

@override
Widget build(BuildContext context) {
  return NestedScrollView(
    headerSliverBuilder: (context, innerBoxScrolled) => [
          SliverAppBar(
            title: Text('POSTAPP'),
            centerTitle: true,
            floating: true,
            pinned: false,
          ),
        ],
    body: RefreshIndicator(
      onRefresh: _loadMorePosts,
      child: ListView.builder(
        itemBuilder: _createPostItem,
        itemCount: _postsCount,
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*ett 6

对于任何回到这个问题的人,我找到了一个使用包pull_to_refresh 的解决方案。

final RefreshController _refreshController = RefreshController();

Future<void> _onRefresh() async {
  await Future<void>.delayed(const Duration(milliseconds: 1000));
  _refreshController.refreshCompleted();
}

@override
Widget build(BuildContext context) {
  return CustomScrollView(
    slivers: [
      SliverAppBar(
        title: Text('Title'),
        centerTitle: true,
        floating: true,
        pinned: false,
      ),
      SliverFillRemaining(
        child: SmartRefresher(
          controller: _refreshController,
          onRefresh: _onRefresh,
          // ListView, CustomScrollView, etc. here
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  height: 200,
                  color: Colors.red,
                ),
                Container(
                  height: 200,
                  color: Colors.blue,
                ),
                Container(
                  height: 200,
                  color: Colors.yellow,
                ),
              ],
            ),
          ),
        ),
      ),
    ],
  );
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*san 5

您可以CupertinoSliverRefreshControl在条子列表中使用,您需要申请physics: BouncingScrollPhysics()到您的CustomScrollView.

例子:

    return Scaffold(
      body: CustomScrollView(
        physics: BouncingScrollPhysics(),
        slivers: [
          SliverAppBar(),
          CupertinoSliverRefreshControl(
            onRefresh: () => refreshList(),
          ),
          SliverList(delegate: delegate),
        ],
      ),
    );

Run Code Online (Sandbox Code Playgroud)

在上面的示例中,它将显示RefreshIndicatorAppBar和之间ListView