Last active
February 4, 2023 10:02
-
-
Save nemoramune/10a399808666d06d030ffa6123cc5d97 to your computer and use it in GitHub Desktop.
usePagingController is a FlutterHooks for PagingController in infinite_scroll_pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/widgets.dart'; | |
| import 'package:flutter_hooks/flutter_hooks.dart'; | |
| import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; | |
| /// Creates [PagingController] that will be disposed automatically. | |
| /// | |
| /// See also: | |
| /// - [useScrollController] | |
| /// - [PagingController] | |
| PagingController<PageKeyType, ItemType> usePagingController<PageKeyType, ItemType>({ | |
| required PageKeyType firstPageKey, | |
| required List<ItemType>? itemList, | |
| required PageKeyType? nextPageKey, | |
| required void Function(PageKeyType) addPageRequestListener, | |
| dynamic error, | |
| List<Object?>? keys, | |
| }) { | |
| final pagingController = use( | |
| _PagingControllerHook<PageKeyType, ItemType>( | |
| firstPageKey: firstPageKey, | |
| addPageRequestListener: addPageRequestListener, | |
| keys: keys, | |
| ), | |
| ); | |
| final state = PagingState<PageKeyType, ItemType>( | |
| itemList: itemList, | |
| nextPageKey: nextPageKey, | |
| error: error, | |
| ); | |
| useValueChanged<PagingState<PageKeyType, ItemType>, void>( | |
| state, | |
| ((_, __) { | |
| pagingController.value = state; | |
| }), | |
| ); | |
| return pagingController; | |
| } | |
| PagingController<void, ItemType> useStateLessPagingController<ItemType>({ | |
| required List<ItemType>? itemList, | |
| required bool? isLast, | |
| required void Function() loadMore, | |
| dynamic error, | |
| List<Object?>? keys, | |
| }) => | |
| usePagingController<bool, ItemType>( | |
| firstPageKey: true, | |
| itemList: itemList, | |
| nextPageKey: isLast ?? false ? null : false, | |
| addPageRequestListener: (isFirstPage) { | |
| if (!isFirstPage) loadMore(); | |
| }, | |
| error: error, | |
| keys: keys, | |
| ); | |
| class _PagingControllerHook<PageKeyType, ItemType> | |
| extends Hook<PagingController<PageKeyType, ItemType>> { | |
| const _PagingControllerHook({ | |
| required this.firstPageKey, | |
| required this.addPageRequestListener, | |
| super.keys, | |
| }); | |
| final PageKeyType firstPageKey; | |
| final void Function(PageKeyType) addPageRequestListener; | |
| @override | |
| HookState<PagingController<PageKeyType, ItemType>, Hook<PagingController<PageKeyType, ItemType>>> | |
| createState() => _PagingControllerHookState(); | |
| } | |
| class _PagingControllerHookState<PageKeyType, ItemType> extends HookState< | |
| PagingController<PageKeyType, ItemType>, _PagingControllerHook<PageKeyType, ItemType>> { | |
| late final pagingController = | |
| PagingController<PageKeyType, ItemType>(firstPageKey: hook.firstPageKey); | |
| @override | |
| PagingController<PageKeyType, ItemType> build(BuildContext context) => pagingController; | |
| @override | |
| void initHook() { | |
| pagingController.addPageRequestListener(hook.addPageRequestListener); | |
| } | |
| @override | |
| void dispose() => pagingController.dispose(); | |
| @override | |
| String get debugLabel => 'usePagingController'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment