And that I want to change the display order of a page according to the user’s action. If he clicks on button 1, the red page is displayed first, if he clicks on button 2, the yellow page is displayed first.
How can I change the order of the pages?
would it be correct for me to use pageview.builder?
class PageViewTest extends StatefulWidget {
const PageViewTest({super.key});
@override
State<PageViewTest> createState() => _PageViewTestState();
}
class _PageViewTestState extends State<PageViewTest> {
final _pageController = PageController();
@override
Widget build(BuildContext context) {
return PageView(
controller: _pageController,
children: [
Column(
children: [
TextButton(
onPressed: () {
//show container red with next page
},
child: const Text("Button 1")),
TextButton(
onPressed: () {
//show container yellow next page
},
child: const Text("Button 2")),
],
),
Container(
color: Colors.red,
),
Container(
color: Colors.yellow,
),
],
);
}
}
2
Answers
You can use
PageView.builder
or normalPageView
both will work.As for the changing of pages you should use the
PageController
passed into yourPageView
and call_pageController.animateToPage(pageNumber)
or_pageController.jumpToPage(pageNumber)
, if you don’t want animation,in your button.Try this way,