在CustomScrollView中使用StreamBuilder和SliverLists [英] Using a StreamBuilder and a SliverLists In CustomScrollView

查看:182
本文介绍了在CustomScrollView中使用StreamBuilder和SliverLists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 StreamBuilder 来获取数据,我想使用 SliverList 全部显示该数据在 CustomScrollView 内,因此我可以利用 CustomScrollView 附带的功能。

I am trying to use a StreamBuilder to fetch data and I want to display that data using a SliverList all inside a CustomScrollView so I can take advantage of the features that come with the CustomScrollView.

关于如何实现此目标的任何想法?

Any ideas on how I can achieve this?

推荐答案

很简单,这里有一个代码示例:

Sure, it's easy, here you have a code sample:

    class SampleStreamBuilder extends StatelessWidget {
      Stream<List<String>> loadData() async* {
        await Future.delayed(Duration(seconds: 3));
        yield List.generate(10, (index) => "Index $index");
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder<List<String>>(
            stream: loadData(),
            builder: (context, snapshot) {
              return snapshot.hasData
                  ? CustomScrollView(
                      slivers: [
                        SliverList(
                          delegate: SliverChildBuilderDelegate((context, index) {
                            return ListTile(
                              title: Text(snapshot.data[index]),
                            );
                          }, childCount: snapshot.data.length),
                        )
                      ],
                    )
                  : Center(
                      child: CircularProgressIndicator(),
                    );
            },
          ),
        );
      }
    }

这篇关于在CustomScrollView中使用StreamBuilder和SliverLists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆