EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
605 stars 201 forks source link

GetX Controller not working #312

Closed Nikotecnology closed 5 months ago

Nikotecnology commented 5 months ago

Hi, i was tryng to use this library using getx but when i load the page the only thing that it does is loading 40 cards and then if i go on the bottom it's not loading the other cards, i was seeing the post that describes how to use getx but all the things are not working, this below are the codes of the view and the controller:

list_client_screen.dart:

class ListClientsScreen extends StatelessWidget {
  ListClientsScreen({Key? key}) : super(key: key);

  final ClientController clientController = Get.put(ClientController());
  final TextEditingController _searchController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromRGBO(229,165,52,100),
        title: const Text(
          'CUSTOMER LIST',
          style: TextStyle(
            fontFamily: 'Readex Pro',
            fontSize: 20,
            color: Colors.white,
          ),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
      body: SafeArea(
        bottom: false,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const SizedBox(height: 8),
                  SizedBox(
                    height: 55,
                    child: TextFormField(
                      controller: _searchController,
                      decoration: InputDecoration(
                        labelText: 'Search Customer',
                        labelStyle: const TextStyle(
                          fontFamily: 'Readex Pro',
                          color: Colors.white,
                          fontSize: 16,
                        ),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: const BorderSide(
                            color: Color(0x0076c99b),
                            width: 2,
                          ),
                          borderRadius: BorderRadius.circular(6),
                        ),
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                            color: Theme.of(context).primaryColor,
                            width: 2,
                          ),
                          borderRadius: BorderRadius.circular(6),
                        ),
                        filled: true,
                        fillColor: const Color.fromRGBO(229,165,52,100),
                        prefixIcon: const Icon(
                          Icons.search_rounded,
                          color: Color.fromRGBO(229,165,52,100),
                          size: 20,
                        ),
                      ),
                      style: const TextStyle(
                        fontFamily: 'Readex Pro',
                        fontSize: 14,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 16),
             Container(
               height: 600,
               child: RefreshIndicator(
                 onRefresh: () => Future.sync(
                     () => clientController.pagingController.refresh(),
                 ),
                 child: PagedListView<int, CliForCard>(
                  pagingController: clientController.pagingController,
                  builderDelegate: PagedChildBuilderDelegate<CliForCard>(
                    itemBuilder: (context, item, index) => CardClient(client: item)
                  ),
                               ),
               ),
             ),
          ],
        ),
      ),
    );
  }
}

client_controller.dart:

class ClientController extends GetxController {
  var urlclientlist = "https://192.168.1.77:8000/api/clifor/retrieve";
  static const pageSize = 10;
  final PagingController<int, CliForCard> pagingController =
  PagingController(firstPageKey: 1);

  Future<List<CliForCard>> getClientList(page) async {
    List<CliForCard> clifors = [];
    var response = await dioService.getMethod("$urlclientlist/$page", FFAppState().token);

    if (response.statusCode == 200) {
      response.data.forEach((element) {
        clifors.add(CliForCard.fromJson(element));
      });
    }

    return clifors;
  }

  Future<void> _fetchPage(int pageKey) async {
    try {
      final newItems = await getClientList(pageKey);
      final isLastPage = newItems.length < pageSize;

      if (isLastPage) {
        pagingController.appendLastPage(newItems);
      } else {
        final nextPageKey = pageKey + newItems.length;
        pagingController.appendPage(newItems, nextPageKey);
      }
    } catch (error) {
      pagingController.error = error;
    }
  }

  @override
  void onReady() {
    pagingController.notifyPageRequestListeners(1);

    super.onReady();
  }

  @override
  void onInit() {
    super.onInit();
    pagingController.addPageRequestListener((pageKey) {
      _fetchPage(pageKey);
    });
  }

  @override
  void onClose() {
    pagingController.dispose();

    super.onClose();
  }
}