28

I have a PageView with four pages in it. I want to start on the third page. That means there are two pages available when the user scrolls up and one page when the user scrolls down.

I tried:

home: PageView(
   controller: MyPageController(),
   children: [Page1(), Page2(), Page3(), Page4()],
   scrollDirection: Axis.vertical,
),

With:

class MyPageController extends PageController {
   @override
   int get initialPage => 3;
}

Unfortunately, that doesn't help me.

Kavin-K
  • 1,948
  • 3
  • 17
  • 48
Christian
  • 25,249
  • 40
  • 134
  • 225

3 Answers3

41

PageController constructor has named parameter initialPage. You can use it, just create the controller somewhere outside of build function:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  PageController controller;

  @override
  void initState() {
    super.initState();
    controller = PageController(initialPage: 3);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: PageView(
            controller: controller,
            children: [
              for (var i = 0; i < 4; i++)
                Text('test $i')
            ],
            scrollDirection: Axis.vertical,
          )
        )
      ),
    );
  }
}
Igor Kharakhordin
  • 9,185
  • 3
  • 40
  • 39
  • 3
    You should not use a `PageController` like this in a Stateless widget! You have to make sure to properly dispose it. Just have a look at the documentation for a full example using `StatefulWidget` https://api.flutter.dev/flutter/widgets/PageController-class.html – DAG Nov 20 '19 at 15:07
  • I'm sorry, yes, you're right. I forgot that you should dispose it. – Igor Kharakhordin Nov 20 '19 at 15:09
  • exactly what i looking for Awesome :) – Abhishek Thapliyal Sep 06 '20 at 07:44
  • Thank you! I was creating the controller inside the build method and it was not working. It is working now like you mentioned. – user3563059 Feb 02 '23 at 08:11
12

For me initialization of PageController with initialPage didn't work for a large number of pages. I also noticed that there is scrolling animation which is undesirable if you want to land to desired page directly.

I used following

  PageController _pageViewController = PageController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    
    WidgetsBinding.instance.addPostFrameCallback((_) {

      if (_pageViewController.hasClients)
        _pageViewController.jumpToPage(3);

    });

    super.didChangeDependencies();
  }

bikram
  • 7,127
  • 2
  • 51
  • 63
  • 2
    This is the right answer when `initialPage` can't be used. Typically when the list is filled by a network response (async) and you want to display the N page first. – Eric Taix Feb 01 '22 at 17:02
10

You need to set initial page like,

PageController _controller = PageController(initialPage: 3);
Kavin-K
  • 1,948
  • 3
  • 17
  • 48