rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
6.29k stars 960 forks source link

PageController stateprovider doesn't work when .previousPage() or nextPage() is pressed #3569

Open mohadel92 opened 5 months ago

mohadel92 commented 5 months ago

using a simple stateprovider the .nextPage(..) method or .previousPage(..) doesn't work

AutoDisposeStateProvider<PageController> _pageControllerProvider =
    StateProvider.autoDispose<PageController>(
        (StateProviderRef<PageController> ref) {
  return PageController(
    initialPage: 0,
    viewportFraction: 0.9,
  );
});

my code: 
PageView.builder(
                            allowImplicitScrolling: true,
                            controller:
                                ref.watch(_pageControllerProvider),
                            itemCount: response?.length ?? 0,
                            itemBuilder: (BuildContext context, int index) {

                              return Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: MyWidget()
                              );
                            },
                          ),

 press handler:

Consumer(builder:
                              (BuildContext context, WidgetRef ref, _) {
                            return PrevNextRow(
                              onNextPressed: () async {
                                ref
                                    .read(
                                        _pageControllerProvider)
                                    .nextPage(
                                        duration: const Duration(
                                          milliseconds: 500,
                                        ),
                                        curve: Curves.decelerate);
                              },
                              onPrevPressed: () async {
  ref
                                    .read(
                                        _pageControllerProvider)
                                    .previousPage(
                                        duration: const Duration(
                                          milliseconds: 500,
                                        ),
                                        curve: Curves.decelerate);
},
                            );
                          })

Expected behavior PageController should move the list to the next item

b2nkuu commented 4 months ago

Hi, mohadel92 In my opinion, Maybe you use StateProvider is wrong concept. Class is must to has immutable. PageController is not immutable class is cannot use in state.

This hope you help.