-1

I want to implement RefreshIndicator in my Listview.builder but the problem is that when I place RefreshIndicator it's not working. And I search in Google the answer that I find is that place physic:

AlwaysScrollableScroll() in Listview.Builder and when I try it RefreshIndicator worked but Listview.Builder not working and I search in Google the answer that I find is that place physic:

NeverScrollableScroll() in Listview.Builder then My Listview.Builder is working OK but Refresh Indicator not working. What can I do?

I am little bit confused, what I can do: either I place AlwaysScrollableScroll() or NeverScrollable().

Here is my code:

  return Scaffold(
  body: SafeArea(
      child: Padding(
    padding: EdgeInsets.all(10),
    child: Column(
      children: [
        FirstRow(
          headingText: 'My Appointments',
          context: context,
        ),
        SizedBox(
          height: 10,
        ),
        Obx(() {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              AppButtonLarge(
                  selected: appointmentController.appointmentT ==
                          'upcomingAppointments' ??
                      false,
                  text: 'Upcoming',
                  onTap: onAppointmentChange),
              AppButtonLarge(
                  selected: appointmentController.appointmentT ==
                          'pastAppointments' ??
                      false,
                  text: 'Past',
                  onTap: onAppointmentChange),
            ],
          );
        }),
        SizedBox(
          height: 10,
        ),
        Expanded(
            child: SingleChildScrollView(
              physics: ScrollPhysics(),
              child: Column(
                children: [
                  GetX<DoctorAppointmentController>(
                    builder: (controller) {
                      // List<Appointment> controller.appointmentList=[];
                      // allAppointment.forEach((element) {
                      // if(element!=null){
                      //   controller.appointmentList=controller.appointmentList+element;
                      // }
                      // });
                      // List<Appointment> controller.appointmentList=snapshot.data;
                      print(controller.appointmentList.length);
                      return controller.isLoading.value
                          ? AppWidgetsCard.getProgressIndicator()
                          : controller.appointmentList.length > 0
                              ? RefreshIndicator(
                            key: refreshKey,
                        onRefresh: () async{
                          await Navigator.pushReplacement(context, PageRouteBuilder(pageBuilder: (a,b,c)=>GetAllDrAppointments(),
                              transitionDuration: Duration(seconds: 3)));
                        },
                                child: Obx(()=>ListView.builder(

                                  physics: const NeverScrollableScrollPhysics(),
                                    // reverse: true,

                                    itemCount: controller.appointmentList.length,
                                      scrollDirection: Axis.vertical,
                                      shrinkWrap: true,
                                      itemBuilder: (context, index) {
                                        void goToNext() {
                                          Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (context) =>
                                                    MyAppointments(
                                                        appointment: controller
                                                                .appointmentList[
                                                            index], isDoctor: true),
                                              ));
                                        }

                                        if (index ==
                                            controller.appointmentList.length -
                                                1) {
                                          id = controller
                                              .appointmentList[index].id;

                                          return Column(
                                            children: [
                                              TextButton(
                                                  onPressed: goToNext,
                                                  child: AppWidgetsCard
                                                      .getAppointmentCard(
                                                          controller.appointmentList[index],
                                                          goToNext, isDoctor:  true), ),
                                              Obx(
                                                () {
                                                  return controller.loadMore.value
                                                      ? AppWidgetsCard
                                                          .getProgressIndicator()
                                                      : Container(
                                                          height: 40,
                                                          width: 200,
                                                          child: AppMethodButton(
                                                              selected: true,
                                                              text: 'Load More',
                                                              onTapMethod: () {
                                                                print('data');
                                                                controller
                                                                    .fetchMoreAppointments(
                                                                        id);
                                                              }),
                                                        );
                                                },
                                              )
                                            ],
                                          );
                                        } else {
                                          return TextButton(
                                              onPressed: goToNext,
                                              child: AppWidgetsCard
                                                  .getAppointmentCard(
                                                      controller
                                                          .appointmentList[index],
                                                      goToNext, isDoctor: true));
                                        }
                                      },
                                    ),
                                ),
                              )
                              : AppWidgetsCard.getEmptyCard('Appointment');
                    },
                  ),
                ],
              ),
            )),
      ],
    ),
  )),
  bottomNavigationBar: Container(
      height: 50,
      child: LastRow(
        page: 'Appointment',
      )),
);'''
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You should follow this solution https://stackoverflow.com/questions/65674589/flutter-refreshing-listview-builder-with-getx – Shawon Jun 15 '22 at 10:04
  • Ijaz - carry on editing the post by all means, but correct spelling is an expectation here. Use a spell-checker if necessary - all browsers have one built in. – halfer Aug 01 '22 at 08:30

1 Answers1

0

About the ScrollPhysics there are different kinds of scrolling behavior. And AlwaysScrollableScroll means that the ListView is able to scroll no matter whether the body size of the ListView is larger than the assigned size. By default, the ListView only scrolls when the size of the body of the ListView is larger than the assigned parent size. So to adopt the RefreshIndicator does not matter with the scroll physics.

To avoid the cache issues of ListView to force the ListView update, you should update the data of the ListView. You would use the Obx wrapper with the ListView and create the list like below:

List<String> data = <String>[].obs;

Obx(()=> data.value.isEmpyty ? SizedBox() : ListView(children: data.value.map((value) => Text(value)).toList())
Klaus Wong
  • 59
  • 2