flutter: NotificationListener in TabController for infinite scroll

后端 未结 1 1369
-上瘾入骨i
-上瘾入骨i 2021-01-26 01:32

I have 4 tabs and i want to add lazy load or infinite scroll option in them. Earlier i tried with Scroll Controller but when it reaches to the end. Event firing more than once.

1条回答
  •  温柔的废话
    2021-01-26 02:26

    I managed to get it work but not sure this is the efficient way or not. May be it will others who may have faced similar issue.

    Below code will help to identify the active tab.

    void initState(){ 
            _controller = TabController(vsync: this, length: 4);
            currentTab = (_controller.index);
            _controller.addListener(() {
            if(_controller.indexIsChanging) {
              print("tab is animating. from active (getting the index) to inactive(getting the index) ");
            }else {
              //tab is finished animating you get the current index
              print(_controller.index);
              currentTab = (_controller.index);
              _handleTabSelection();
            }
          });
    

    and below code i have added in NotificationListner.

      onNotification: (notificationInfo) {
              if (notificationInfo is ScrollEndNotification && notificationInfo.metrics.pixels == notificationInfo.metrics.maxScrollExtent) {
                print("scroll");
                  if(currentTab == 0){
                    if(recommended == true && tab0 == 'Suggested' ){
                      // getData();
                     print('fire the 1 event');
                    }else{
                      print('Name()1;');
                    }
                  }
                 if(currentTab == 1){
                    if(nearme == true && tab1 == 'Near Me'){
                      //getData();
                      print('fire the 2nd event ');
                    }else{
    
                    }
                 }
                 if(currentTab == 2){
                    if(byRating == true && tab2 == 'By Rating'){
                      //getData();
                      print('fire the 3rd event');
                    }else{
                      
                    }
                 }
    
                 if(currentTab == 3){
                    if(byprice == true && tab3 == 'Active'){
                      //getData();
                      print('fire the 4 event');
                    }else{
    
                    }
                 }
                /// your code
              }
              return true;
            },
          );
    

    Edit: As i have multiple taps so, above code is firing on left and right scroll too. To prevent that i have changed the code to as below.

    if (notificationInfo is ScrollEndNotification && notificationInfo.metrics.axisDirection == AxisDirection.down  && notificationInfo.metrics.pixels == notificationInfo.metrics.maxScrollExtent) {
    

    0 讨论(0)
提交回复
热议问题