Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dynamic initialPage not working in PageController

This is my page view code :

late PageController pageController;
int pageInd = 0;

class DetailedWorkoutScreenStatePageView extends StatefulWidget {
  int initialPage = 0;
  DetailedWorkoutScreenStatePageView({super.key, required this.initialPage});

  @override
  State<DetailedWorkoutScreenStatePageView> createState() =>
      _DetailedWorkoutScreenStatePageViewState();
}

class _DetailedWorkoutScreenStatePageViewState
    extends State<DetailedWorkoutScreenStatePageView> {
  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: widget.initialPage); // adding the index to PageController
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView.builder(
      onPageChanged: (ind) {
        setState(() {
          pageInd = ind;
        });
      },
      itemCount: allBodyPartWorkouts.length,
      itemBuilder: (context, ind) {
        WorkoutModel data = allBodyPartWorkouts[ind];
        return DetailedWorkoutScreen(
          name: data.name,
          bodyPart: data.bodyPart,
          gifUrl: data.gifUrl,
          target: data.target,
          instructions: data.instructions,
        );
      },
    ));
  }
}

The initialPage variable is getting the right index still the page view is showing the first page.
where did i get it wrong? I really don’t understand.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have not assigned a controller to your PageView. Add pageController to the controller field like this:

PageView.builder(
  controller: pageController,
  ...
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading