如何在SliverList

时间:2018-09-03 09:27:21

标签: flutter

我想将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,
            ),
          );
        }
    );
  }
}

如何将相同的FutureBuilder逻辑转换为SliverList?

2 个答案:

答案 0 :(得分:5)

import pyspark.sql.functions as F spjsoya.withColumn( 'date_str', F.from_unixtime( F.unix_timestamp('a', 'yyyyMMdd') ) ).withColumn( "week_num", F.weekofyear(F.col("date_str")) ).withColumn( "month", F.month(F.col("date_str")) ).withColumn( "year", F.year(F.col("date_str")) ).groupby( "year", "week_num" ).agg( F.avg("rate").alias("average_rate") ).orderBy( "week_num" ).show() 的子代必须是薄片,不能使用CustomScrollView

相反,在将来完成时重建FutureBuilder

CustomScrollView

如果您有多条依赖于// 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

答案 1 :(得分:0)

使用SliverFillRemaining

Widget build(BuildContext context) {
return new Scaffold(
  body: CustomScrollView(
    slivers: <Widget>[
      const SliverAppBar(
        pinned: true,
        title: Text("anything"),
        expandedHeight: 250.0,
        flexibleSpace: FlexibleSpaceBar(
        ),

      ),
      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);
          },
        ),
      )
    ],
  )
);