Flutter:SliverList中的ListView Builder [英] Flutter: ListView Builder inside SliverList

查看:1155
本文介绍了Flutter:SliverList中的ListView Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Listbox Builder中使用Flutter EasyRefresh插件。
https://pub.dev/packages/flutter_easyrefresh

I am trying to use Flutter EasyRefresh plugin in Listbox Builder. https://pub.dev/packages/flutter_easyrefresh

我以前使用Scroll Controller加载更多功能并且可以正常工作,但是现在我决定添加下拉菜单以刷新。

Earlier I was using Scroll Controller for load more function and that was working but now I decided to add pull down to refresh.

我的代码很长,但是我试图

My code is quite long but I am trying to make it smaller for this question.

Future <List> getData() async{ 
   if(endofrecord == false){
    try{  
      var deviceid = '1234';
      var uid = '999';

  
  var bodyss = { "uid" : id, "deviceid": deviceid, "offset": offset};
  var url = 'http://192.168.100.4:8080/get-followerdata.php';

                // Starting Web API Call.
       var response = await http.post(url, body: json.encode(bodyss)).timeout(Duration(seconds: 5),
        onTimeout: (){
                //  throw Exception();
                  //or you can also
          return null;
        });
     if(response.statusCode == 200){
       final data = getFollowingFromJson(response.body);
                  setState(() {
                    _inProcess = false;
                    if(data.count == null){
                      count = 0;
                    }else{
                      offset = offset + 1;
                      print(offset);
                      count = data.count;
                    }
                  if(data.content.length > 0 && data.content[0].name != 'Empty'){
                    for (var i in data.content) {
                      lists.add(i);
                    }  
                  }else{
                    nodata = 'No Record Found';
                    endofrecord = true;
                  }
                
                  });
            print(lists.length);
            
     }
 
}catch(e){
   print("Exception Caught: $e");
}

 return lists;
    }
  } 

以下是与代码相关的其他小工具。

Here are other widget related to codes.

body: EasyRefresh.custom(
          enableControlFinishRefresh: false,
          enableControlFinishLoad: true,
          controller: _controllers,
          header: ClassicalHeader(),
          footer: ClassicalFooter(),
          onRefresh: () async {
            await Future.delayed(Duration(seconds: 2), () {
              print('onRefresh');
              setState(() {
                _count = 2;
                getData();
              });
              _controllers.resetLoadState();
            });
          },
          onLoad: () async {
            await Future.delayed(Duration(seconds: 2), () {
              print('onLoad');
              setState(() {
                _count += 2;
                getData();
              });
              _controllers.finishLoad(noMore: _count >= 4);
            });
          },
       
         slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) {
        return Container(
        //child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: Stack(
            children: <Widget>[
        TabBarView(
          controller: _controller,
          children: [
           // Text("TAB ONE CONTENT"),
           Container(
                decoration: BoxDecoration(
                 // borderRadius: BorderRadius.circular(25),
                  color: Colors.black87,
                ),
                padding: EdgeInsets.only(top: 10, bottom: 5),
                height: MediaQuery.of(context).size.height,
                width: double.infinity,
                child: ListView.builder(
                   // controller: _scrollcontroller,
                    itemCount: lists.length, 
                    itemBuilder: (BuildContext context, int index) {
                      return buildList1(context, index);
                    }),
              ),

一旦数据加载到Listview.builder onRefresh或onload函数中没有开火。简而言之,一旦将数据加载到刷新或加载中就不会发生。如果我没有加载数据,那么我可以在加载时看到并下拉刷新。

Once data is loaded in Listview.builder onRefresh or onload function didn't fire. In simple words, once data is loaded onRefresh or on load didn't happen. If I didn't load the data then I can see on load and pull down to refresh is firing.

编辑:
我发现onRefresh正在射击,但我需要将其拖动到屏幕顶部。如果我从中心或底部拖动,则不会触发。用简单的话来说,只要有一个列表视图项就不会触发。

I found that onRefresh is firing but I need to drag it top of the screen. If I drag from center or bottom then it is not firing. In Simple words wherever there is a listview item it is not firing.

编辑2

我怀疑是

 return Container(
        //child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: Stack(
            children: <Widget>[
        TabBarView(
          controller: _controller,
          children: [
           // Text("TAB ONE CONTENT"),


推荐答案

您需要等待getData(),而不是等待延迟2秒。将来的工作完成后,您应该更改小部件的状态

You need to await for getData(), instead of await delayed 2 seconds. Once the future is completed, you should change the state of your widget

您的代码应该像下面这样:

Your code should we something like:


  1. 等待getData();

  2. 更新状态

这篇关于Flutter:SliverList中的ListView Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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