代码之家  ›  专栏  ›  技术社区  ›  Yeahia2508

如何在SliverList中使用FutureBuilder

  •  0
  • Yeahia2508  · 技术社区  · 6 年前

    我想隐蔽起来 ListView CustomScrollView 所以我需要把我的 FutureBuilder 进入之内 SliverList .

    这是我的密码:

    class LatestNewsList extends StatefulWidget {
      @override
      _LatestNewsListState createState() => _LatestNewsListState();
    }
    
    class _LatestNewsListState extends State<LatestNewsList> {
      @override
      Widget build(BuildContext context) {
        return FutureBuilder<List<Data>>(
          future: getQuake(),
          builder: (context, snapshot){
            if(snapshot.hasError) print(snapshot.error);
    
            return snapshot.hasData
                ? NewsList(latestNews: snapshot.data,)
                : Center(child: CircularProgressIndicator());
          },
        );
      }
    }
    
    class NewsList extends StatelessWidget {
      final List<Data> latestNews;
    
      NewsList({this.latestNews});
    
      @override
      Widget build(BuildContext context) {
    
    
    
        return new ListView.builder(
            itemCount: latestNews.length,
            itemBuilder: (context, index){
              return new GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => Browser(url: latestNews[index].post_link,)
                      )
                  );
                },
                child: new LatestNewsItem(
                  post_title: latestNews[index].post_title,
                  post_link: latestNews[index].post_link,
                  img_src: latestNews[index].img_src,
                ),
              );
            }
        );
      }
    }
    

    1 回复  |  直到 6 年前
        1
  •  26
  •   boformer    6 年前

    CustomScrollView 一定是条子,不能用 FutureBuilder .

    相反,重建 自定义滚动视图

    // build fixed items outside of the FutureBuilder for efficiency
    final someOtherSliver = SliverToBoxAdapter(...);
    
    return FutureBuilder<List<Data>>(
      future: getQuake(), // this is a code smell. Make sure that the future is NOT recreated when build is called. Create the future in initState instead.
      builder: (context, snapshot){
        Widget newsListSliver;
        if(snapshot.hasData) {
          newsListSliver = SliverList(delegate: SliverChildBuilderDelegate(...))
        } else {
          newsListSliver = SliverToBoxAdapter(child: CircularProgressIndicator(),);
        }
    
        return CustomScrollView(
          slivers: <Widget>[
            someOtherSliver,
            newsListSliver
          ],
        );
      },
    );
    

    如果您有多条依赖于 Future Stream s、 你可以把建筑商链起来:

    return FutureBuilder<..>(
      ...
      builder: (context, snapshot1) {
        return FutureBuilder<..>(
          ...
          builder: (context, snapshot2) {
            return CustomScrollView(...);
          }
        )
      }
    )
    
        2
  •  2
  •   Sarthak Singhal    6 年前

    Widget build(BuildContext context) {
    return new Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverFillRemaining(
            child: FutureBuilder(
              future: getData(),
              builder: (context, snapshot) {
                if (snapshot.data == null)
                  return new Container(
                    child: Center(child: new CircularProgressIndicator()),
                  );
                else
                  return Text(snapshot.data.name);
              },
            ),
          )
        ],
      )
    );
    

    或者使用SliverList

    Widget build(BuildContext context) {
    return new Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
             SliverList(
            delegate: SliverChildListDelegate([Container(
                child: FutureBuilder(
                 future : getData(),
                 builder: (C,snap){
                    //do whatever you want
                  }
              )
              )]),
            )
          ]
        )
      )
     }