【发布时间】:2021-04-18 01:18:39
【问题描述】:
在具有类似以下结构的 Scaffold 页面中
@override
Widget build(BuildContext context){
body: PageView(
controller: _controller;
children: <Widget>[Page1(), Page2(), Page3()];
);
bottomNavigationBar: BottomNavBar(
onItemSelected: (index) => _controller.animateToPage()
)
}
从Page2() 到Page1() 有两种方式:
- 从左向右滑动屏幕
- 点击
bottomNavigationBar上的Page1()图标,从而调用_controller.animateToPage(0)
问题是,如何判断页面是通过滑动手势还是animateToPage()函数改变?
谢谢。
【问题讨论】:
-
PageView有一个属性onPageChanged。这样就够了吗? -
拖动行为和
animateToPage()都会触发onPageChanged(index)函数,很难区分。 -
animateToPage()是你在代码中调用的,其他的不是,你明白我的意思了吗,下面的答案解释了它