【问题标题】:Flutter Refreshindicator rebuilding Listview.Builder after adding new items with StreamBuilderFlutter Refreshindicator 在使用 StreamBuilder 添加新项目后重建 Listview.Builder
【发布时间】:2021-10-12 16:45:13
【问题描述】:

我有一个包含在 StreamBuilder 中的 RefreshIndicator。每当 Streambuilder 添加了新数据时,RefreshIndicator 都会重新构建其 Listview.Builder,并在此过程中跳转到顶部。

流生成器( 流:apiResultStream.stream, 建设者: (BuildContext 上下文,AsyncSnapshot 快照){ 如果(快照.hasError){ print("有错误"); } if(!snapshot.hasData) {

            return  RefreshIndicator(
                onRefresh: () {
                  _apiResult=[];
                  return _loadMoreData(0);
                },
                child:snapshot.data.length == 0 ? Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Center(child: Text('No items', style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),)),
                ):SingleChildScrollView(
                    controller: scrollController,
                    physics: const AlwaysScrollableScrollPhysics(),
                    child:ListView.builder(
                    itemBuilder:(BuildContext ctxt, int index){
                      return InkWell(
                          
                          child:TripItem(key: ValueKey(snapshot.data[index].Id),item:snapshot.data[index])
                      );
                    }
                    ,itemCount:snapshot.data.length,
                    shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                    key: UniqueKey(),


                ))

            );
     

【问题讨论】:

    标签: flutter


    【解决方案1】:

    解决方案是在 RefreshIndicator 中添加一个 GlobalKey。这可以防止 RefreshIndicator 在 StreamBuilder 有新数据时重建其子节点。

    StreamBuilder(
        stream: apiResultStream.stream,
        builder:
            (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
              if(snapshot.hasError){
                print("has error");
              }
              if(!snapshot.hasData)
              {
    
    
           
                return  RefreshIndicator(
                    key: _refreshIndicatorKey ,
                    onRefresh: () {
                      _apiResult=[];
                      return _loadMoreData(0);
                    },
                    child:snapshot.data.length == 0 ? Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: Center(child: Text('No items', style: 
                      TextStyle(fontSize: 16.0, fontWeight: 
                      FontWeight.bold),)),
                    ):SingleChildScrollView(
                        controller: scrollController,
                        physics: const AlwaysScrollableScrollPhysics(),
                        child:ListView.builder(
                        itemBuilder:(BuildContext ctxt, int index){
                          return InkWell(
                              
                              child:TripItem(key: 
                ValueKey(snapshot.data[index].Id),item:snapshot.data[index])
                          );
                        }
                        ,itemCount:snapshot.data.length,
                        shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                        key: UniqueKey(),
    
    
                    ))
    
    );
    
         
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2021-07-19
      • 2021-04-09
      • 2018-12-30
      • 1970-01-01
      相关资源
      最近更新 更多