Вот возможное решение, где страницы все еще находятся на пороге, но они автоматически пролистывают их. (Для отображения отключенных заголовок страницы становится серым).
Я использовал контроллер страницы и свойство onPageChanged
. Это свойство всегда вызывается после полного выполнения страницы. При использовании метода .nextPage
& .previousPage
он автоматически перелистывает страницу в зависимости от направления перелистывания и от того, заблокирована страница или нет (определено в списке disabledPages
)
Чтобы различать направление прокрутки, последний просмотренный индекс страницы сохраняется в переменной previousPageViewIndex
class PageViewDemo extends StatefulWidget {
@override
_PageViewDemoState createState() => _PageViewDemoState();
}
class _PageViewDemoState extends State<PageViewDemo> {
int _index = 0;
List<Color> myColors = [
Colors.blue,
Colors.greenAccent,
Colors.green,
Colors.grey,
Colors.deepPurpleAccent,
Colors.deepOrangeAccent,
Colors.pink,
Colors.amber,
Colors.cyanAccent,
Colors.teal
];
PageController pageController = PageController();
int previousPageViewIndex = 0;
// here are the current pages which should be disabled
List<int> disabledPages = [2, 6, 7];
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
onPageChanged: (index) {
if(disabledPages.contains(index)) { // check if current page is disabled
if(index > previousPageViewIndex) { // swipe right
pageController.nextPage(duration: Duration(milliseconds: 800), curve: Curves.ease);
} else if(index < previousPageViewIndex) { // swipe left
pageController.previousPage(duration: Duration(milliseconds: 800), curve: Curves.ease);
}
}
previousPageViewIndex = index;
},
controller: pageController,
itemBuilder: (context, index) {
return Container(
color: myColors[index],
alignment: Alignment.center,
child: Text('Page $index', style: TextStyle(fontSize: 28, color: disabledPages.contains(index) ? Colors.grey[500] : null)),
);
},
itemCount: 10,
),
);
}
}
Чтобы перейти на указанную c страницу при инициализации, используйте свойство .initPage
в PageController
.