KEVIN
KEVIN

Reputation: 69

automatically scrolling to a specific pageview when a button is pressed in Flutter

I have a horizontal list of 10 containers wrapped using PageView builder.

PageView.builder( controller: _pageController, itemCount: items.length, itemBuilder: (BuildContext context, int index) { return ContainerItem(index); }, ), I have another button which on pressing, should scroll the containers to get to that specific container. How do I achieve this

Upvotes: 0

Views: 763

Answers (1)

davidn
davidn

Reputation: 1266

Using your PageController you can achieve this, the purpose of the controller is to control so.... This is what you are looking for:

_pageController.animateToPage(
  index,
  duration: const Duration(milliseconds: 300),
  curve: Curves.easeInOut,
);

index is the page to want to scroll to, from 0 to the (items.length - 1). If you have 3 items, then setting index to 0 will represent the first...and so on

Look at the docs: https://api.flutter.dev/flutter/widgets/PageController-class.html

Upvotes: 1

Related Questions