在使用浮动appbar创建应用时使用StreamBuilder

Wil*_*ndi 6 flutter

这是使用浮动appbar构建应用的代码:

 @override
Widget build(BuildContext context) {
return new Scaffold(
  body: new CustomScrollView(slivers: <Widget>[
    new SliverAppBar(
      title: new Text('Sliver App Bar'),
      floating: true,
      snap: true,
      bottom: PreferredSize(
        preferredSize: const Size.fromHeight(90.0),
        child: new Text('dddd'),
      ),
    ),
    new SliverList(
        delegate: new SliverChildListDelegate(buildTextViews(50)))
  ]),
);
}
Run Code Online (Sandbox Code Playgroud)

这是使用StreamBuilder构建应用程序(没有浮动appbar)的代码:

 @override
Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    backgroundColor: Colors.orangeAccent,
    title: new Text('Find Anything'),
    bottom: PreferredSize(
      preferredSize: const Size.fromHeight(48.0),
      child: new Text('dddd'),
    ),
  ),

  body: new StreamBuilder(
      stream: Firestore.instance.collection('posts').snapshots(),
      builder: (context, snapshot) {
        List<TekongoPost> posts = preparePosts(snapshot.data.documents);
        print("************$posts[0]*************");
        if (!snapshot.hasData) return const Text('Loading...');
        return ListView.builder(
          itemCount: posts.length,
          //              padding: const EdgeInsets.only(top: 10.0),
          //              itemExtent: 25.0,
          itemBuilder: (context,index) {
            return getListItem(context,posts[index]);
          },
        );
      }),
);
 }
 }
Run Code Online (Sandbox Code Playgroud)

如何在上面的第一种情况下使用细长条创建一个带有浮动appbar的应用程序,同时使用如上所述的StreamBuilder?

Rém*_*let 7

扑朔迷离的建设者什么都不做。您可以很好地将a包装SliverListStreamBuilder或其他构建器中。它仍将按预期工作。

因此,您唯一需要确保的是构建器正确返回Sliver诸如的代码SliverList

CustomScrollView(
  slivers: <Widget>[
    SliverAppBar(),
    StreamBuilder<List<String>>(
      stream: myStream,
      builder: (context, snapshot) {
        return SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
              return ListTile(title: Text(snapshot.data[index]));
            },
            childCount: snapshot.hasData ? snapshot.data.length : 0,
          ),
        );
      },
    )
  ],
),
Run Code Online (Sandbox Code Playgroud)