PageController class

A controller for PageView.

A page controller lets you manipulate which page is visible in a PageView. In addition to being able to control the pixel offset of the content inside the PageView, a PageController also lets you control the offset in terms of pages, which are increments of the viewport size.

See also:

  • PageView, which is the widget this object controls.

This widget introduces a MaterialApp, Scaffold and PageView with two pages using the default constructor. Both pages contain an ElevatedButton allowing you to animate the PageView using a PageController.
link
class MyPageView extends StatefulWidget {
  const MyPageView({super.key});

  @override
  State<MyPageView> createState() => _MyPageViewState();
}

class _MyPageViewState extends State<MyPageView> {
  final PageController _pageController = PageController();

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: PageView(
          controller: _pageController,
          children: <Widget>[
            ColoredBox(
              color: Colors.red,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    if (_pageController.hasClients) {
                      _pageController.animateToPage(
                        1,
                        duration: const Duration(milliseconds: 400),
                        curve: Curves.easeInOut,
                      );
                    }
                  },
                  child: const Text('Next'),
                ),
              ),
            ),
            ColoredBox(
              color: Colors.blue,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    if (_pageController.hasClients) {
                      _pageController.animateToPage(
                        0,
                        duration: const Duration(milliseconds: 400),
                        curve: Curves.easeInOut,
                      );
                    }
                  },
                  child: const Text('Previous'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Inheritance

Constructors

PageController({int initialPage = 0, bool keepPage = true, double viewportFraction = 1.0, ScrollControllerCallback? onAttach, ScrollControllerCallback? onDetach})
Creates a page controller.

Properties

debugLabel String?
A label that is used in the toString output. Intended to aid with identifying scroll controller instances in debug output.
finalinherited
hasClients bool
Whether any ScrollPosition objects have attached themselves to the ScrollController using the attach method.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
initialPage int
The page to show when first creating the PageView.
final
initialScrollOffset double
The initial value to use for offset.
no setterinherited
keepPage bool
Save the current page with PageStorage and restore it if this controller's scrollable is recreated.
final
keepScrollOffset bool
Each time a scroll completes, save the current scroll offset with PageStorage and restore it if this controller's scrollable is recreated.
finalinherited
offset double
The current scroll offset of the scrollable widget.
no setterinherited
onAttach ScrollControllerCallback?
Called when a ScrollPosition is attached to the scroll controller.
finalinherited
onDetach ScrollControllerCallback?
Called when a ScrollPosition is detached from the scroll controller.
finalinherited
page double?
The current page displayed in the controlled PageView.
no setter
position ScrollPosition
Returns the attached ScrollPosition, from which the actual scroll offset of the ScrollView can be obtained.
no setterinherited
positions Iterable<ScrollPosition>
The currently attached positions.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
viewportFraction double
The fraction of the viewport that each page should occupy.
final

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
animateTo(double offset, {required Duration duration, required Curve curve}) Future<void>
Animates the position from its current value to the given value.
inherited
animateToPage(int page, {required Duration duration, required Curve curve}) Future<void>
Animates the controlled PageView from the current page to the given page.
attach(ScrollPosition position) → void
Register the given position with this controller.
override
createScrollPosition(ScrollPhysics physics, ScrollContext context, ScrollPosition? oldPosition) ScrollPosition
Creates a ScrollPosition for use by a Scrollable widget.
override
debugFillDescription(List<String> description) → void
Add additional information to the given description for use by toString.
inherited
detach(ScrollPosition position) → void
Unregister the given position with this controller.
inherited
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
jumpTo(double value) → void
Jumps the scroll position from its current value to the given value, without animation, and without checking if the new value is in range.
inherited
jumpToPage(int page) → void
Changes which page is displayed in the controlled PageView.
nextPage({required Duration duration, required Curve curve}) Future<void>
Animates the controlled PageView to the next page.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
previousPage({required Duration duration, required Curve curve}) Future<void>
Animates the controlled PageView to the previous page.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited