How to apply (pull to refresh) pagination in multiple tabs in flutter?

Viewed 322

##I had implemented Pull to Refresh library for applying pagination. To apply this pagination , refresh controller is used. But, for two tabs in a page when I was trying to handle pagination with multiple controller , it doesn't work. the 2nd refresh controller is not called. first refresh controller is open for both first and second tab. Second controller is not working. Can you please tell me a way of applying pagination in both tabs?##

  @override
  _WaterVehicleRequestPageState createState() => _WaterVehicleRequestPageState();
}

class _WaterVehicleRequestPageState extends State<WaterVehicleRequestPage> {

  List<bool> _isSelected = [true,false];
  bool _isLoading = false;
  WVRequestBloc _requestBloc;
  Customer _customer;
  ///***************var for pagination -start*******************
  int currentPageForPending = 1;
  int totalPagesForPending;
  //List<WaterVehicleRequest> _pendingRequestList;
  List<WaterVehicleRequest> _pendingList;
  Meta _metaForPending;
  ///initialize refreshController
  final RefreshController refreshControllerForPending =
  RefreshController(initialRefresh: false);

  //for completed tab
  int currentPageForCompleted = 1;
  int totalPagesForCompleted;
  List<WaterVehicleRequest> _completedList;
  Meta _metaForCompleted;
  final RefreshController refreshControllerForCompleted = RefreshController(initialRefresh: false);

  ///***************var for pagination -end*******************

  @override
  void initState() {
    super.initState();
    _requestBloc = BlocProvider.of<WVRequestBloc>(context);
    _requestBloc.add(WVRequestInitEvent());
    _pendingList = [];
    _completedList = [];
    _customer = Customer();
  }

  ///************method for Pending pagination logic -start**********************

  Future<bool> getPendingWVData({bool isRefresh = false}) async {
    currentPageForPending++;
    totalPagesForPending = _metaForPending.lastPage;
     print("Total Pages No: $totalPagesForPending");
    print("Current Page: $currentPageForPending");
     print("total items: ${_metaForPending.total}");
    if (isRefresh) {
      currentPageForPending = 1;
    } else {
      if (currentPageForPending > totalPagesForPending) {
        currentPageForPending = totalPagesForPending +1;
        refreshControllerForPending.loadNoData();
        return false;
      }
    }
    _requestBloc.add(WVRequestPendingEvent(currentPageForPending));


    return true;
  }

  ///************method for Pending pagination logic -end************************

  ///************method for completed pagination logic -start**********************

  Future<bool> getCompletedWVData({bool isRefresh = false}) async {
    currentPageForCompleted++;
    totalPagesForCompleted = _metaForCompleted.lastPage;
    print("Total Pages No: $totalPagesForCompleted");
    print("Current Page: $currentPageForCompleted");
    print("total items: ${_metaForCompleted.total}");
    if (isRefresh) {
      currentPageForCompleted = 1;
    } else {
      if (currentPageForCompleted > totalPagesForCompleted) {
        currentPageForCompleted = totalPagesForCompleted +1;
        refreshControllerForCompleted.loadNoData();
        return false;
      }
    }
    _requestBloc.add(WVRequestCompletedEvent(currentPageForCompleted));


    return true;
  }

  ///************method for completed pagination logic -end************************

  @override
  void dispose() {
    _requestBloc.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return MyScaffold(
        body: BlocBuilder<WVRequestBloc, WVRequestState>(
          builder: (context, state){
            if(state is WVRequestLoadingState){
              _isLoading = true;
              return onWVRequestPendingState();
            }
            if(state is WVRequestPendingState){
              _isLoading = false;
              _pendingList = state.pendingList;
              _isSelected = state.selected;
              _customer = state.customer;
              _metaForPending = state.meta;
              return onWVRequestPendingState();
            }
            if(state is WVRequestCompletedState){
              _isSelected = state.selected;
              _completedList = state.completedList;
              _metaForCompleted = state.meta;
              return onWVRequestCompletedState();
            }
            return Container();
            },
        ),
    );
  }

  Widget onWVRequestPendingState(){
    return ModalProgressHUD(
      inAsyncCall: _isLoading,
      child: SmartRefresher(
        enablePullDown: false,
        header: WaterDropHeader(),
        controller: refreshControllerForPending,
        enablePullUp: true,
        onLoading: () async{
          final result = await getPendingWVData(isRefresh: false);
          // print("GegItemsData Value: $result");
          //print("OnLoading calling going on");
          if (result) {
            refreshControllerForPending.loadComplete();
          }
          print("Onloading for Pending WV called");
        },
        child: SingleChildScrollView(
          child: Column(
            children: [
              MyAppBar(),
              BackPressLayout(getTranslated(context, 'water_vehicle_request')),

  
              getToggleButton(),

              getPendingList(),
            ],
          ),
        ),
      ),
    );
  }

  Widget onWVRequestCompletedState(){
    //todo: controller issue have to be resolved.
    print("***************Completed widget part is running***************");
    return ModalProgressHUD(
      inAsyncCall: _isLoading,
      child: SmartRefresher(
        enablePullDown: false,
        header: WaterDropHeader(),
        controller: refreshControllerForCompleted,
        enablePullUp: true,
        onLoading: () async{
          final result = await getCompletedWVData(isRefresh: false);
          // print("GegItemsData Value: $result");
          //print("OnLoading calling going on");
          if (result) {
            refreshControllerForCompleted.loadComplete();
          }
          print("Onloading for Completed WV called");
        },
        child: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            children: [
              MyAppBar(),
              BackPressLayout(getTranslated(context, 'water_vehicle_request')),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 3 * SizeConfig.widthMultiplier),
                child: GestureDetector(
                  onTap: onCreateNewRequest,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Icon(
                        Icons.add,
                        color: Color(0xFF8212F8),
                      ),
                      Text(
                        getTranslated(context, 'create_new_request'),
                        style: AppStyle.textBoldPurple_14,
                      ),
                    ],
                  ),
                ),
              ),
              getToggleButton(),

              getCompletedList(),
            ],
          ),
        ),
      ),
    );
  }

  ///********************* toggle button : pending or completed -start***********************
  Widget getToggleButton(){
    return Container(
      margin: EdgeInsets.symmetric(vertical: 2 * SizeConfig.heightMultiplier),
      child: ToggleButtons(
        constraints: BoxConstraints(
            maxWidth: 33.33 * SizeConfig.widthMultiplier,
            minWidth: 33.33 * SizeConfig.widthMultiplier),
        borderColor: Color(0xFF8212F8),
        fillColor: Color(0xFF8212F8),
        borderWidth: 2,
        selectedBorderColor: Color(0xFF8212F8),
        selectedColor: Colors.white,
        borderRadius:
        BorderRadius.circular(2.5 * SizeConfig.heightMultiplier),
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(SizeConfig.heightMultiplier),
            child: Text(
              'PENDING',
              style: TextStyle(fontSize: 1.8 * SizeConfig.textMultiplier),
            ),
          ),
          Padding(
            padding: EdgeInsets.all(SizeConfig.heightMultiplier),
            child: Text(
              'COMPLETED',
              style: TextStyle(fontSize: 1.8 * SizeConfig.textMultiplier),
            ),
          ),
        ],
        onPressed: onToggleButtonClick,
        isSelected: _isSelected,
      ),
    );
  }
  ///********************* toggle button : pending or completed -end***********************

  ///*********************  pending list view -start************************************
  Widget getPendingList(){
    return Container(
      height: 12 * SizeConfig.heightMultiplier * _pendingList.length + 20,
      child: ListView.builder(
          itemCount: _pendingList.length,
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (context, position){
            return Container(
              height: 11 * SizeConfig.heightMultiplier,
              padding: EdgeInsets.symmetric(horizontal: 2 * SizeConfig.widthMultiplier),
              margin: EdgeInsets.symmetric(vertical: 0.6 * SizeConfig.heightMultiplier),
              child: Card(
                shadowColor: Colors.white,
                elevation: 2,
                child: Padding(
                  padding: EdgeInsets.only(right: _pendingList[position].status == Status.PENDING ? 4 * SizeConfig.widthMultiplier : 10 * SizeConfig.widthMultiplier, left: 3 * SizeConfig.widthMultiplier),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("${_pendingList[position].waterVolume} ${waterUnitValues.reverse[_pendingList[position].waterUnit]}",
                              style: AppStyle.textBoldBlack_14
                          ),

                          Text("${DateFormat("dd MMM, y").format(_pendingList[position].createdAt)}",
                            style: AppStyle.textRegularBlack_14,
                          ),
                        ],
                      ),

                      Container(
                        padding: EdgeInsets.all(4),
                        margin: EdgeInsets.only(right: 4 * SizeConfig.widthMultiplier),
                        child: Text("${statusValues.reverse[_pendingList[position].status]}",
                          style: TextStyle(
                              fontSize: 1.33 * SizeConfig.textMultiplier,
                              color: Colors.white
                          ),
                        ),
                        decoration: BoxDecoration(
                            color: _pendingList[position].status == Status.PENDING ? Color(0xFFFFC107) : Color(0xFF007BFF),
                            borderRadius: BorderRadius.circular(4)
                        ),
                      ),

                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("৳${_pendingList[position].totalAmount}",
                              style: AppStyle.textBoldPurple_12
                          ),
                          _pendingList[position].status == Status.PENDING ?
                          OutlineButton(
                              onPressed: (){
                                Navigator.push(context,
                                    MaterialPageRoute(builder: (context) => WaterVehiclePayment(_pendingList[position], _customer)));
                              },
                              borderSide: BorderSide(color: Color(0xFF8212F8)),
                              child: Text(
                                getTranslated(context, 'pay_now'),
                                style: TextStyle(
                                  fontSize: 1.43 * SizeConfig.textMultiplier,
                                  color: Color(0xFF8212F8),
                                ),
                              )
                          ):
                          Container(),

                        ],
                      )
                    ],
                  ),
                ),
              ),
            );
          }
      ),
    );
  }```

0 Answers
Related