use*_*978 2 device-orientation flutter flutter-layout flutter-pageview
最近也有类似的问题,不过已经在 GitHub 上解决并关闭了。由于我是新手,我可能会在这里遗漏一些东西。方向更改后,页面索引恢复为零,但选定的 BottomNavigationBarItem 保持原样。这里正在显示页面“ONE”,但在旋转设备之前选择了选项卡“FOUR”

void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Pageview Orientation Bug',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _page = 0;
PageController _controller = PageController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: OrientationBuilder(
builder: (context,orientation) {
return orientation == Orientation.portrait
? Column(
children: [
Expanded(child: Container(color: Colors.grey)),
Expanded(
child: PageView(
controller: _controller,
children: [Text("ONE"), Text("TWO"),Text("THREE"), Text("FOUR"),],
),
),
],
)
: Row(
children: [
Expanded(child: Container(color: Colors.grey)),
Expanded(
child: PageView(
controller: _controller,
children: [Text("ONE"), Text("TWO"),Text("THREE"), Text("FOUR"),],
),
),
],
);
}
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _page,
onTap: (page) {
setState(() {
_page = page;
});
_controller.jumpToPage(page);
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.directions_bike),
label: "ONE",
),
BottomNavigationBarItem(
icon: Icon(Icons.directions_boat_outlined),
label: "TWO",
),
BottomNavigationBarItem(
icon: Icon(Icons.directions_car),
label: "THREE",
),
BottomNavigationBarItem(
icon: Icon(Icons.directions_run),
label: "FOUR",
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
添加final Key _key = GlobalKey();并HomeState传递key给两个PageView构造函数。这将告诉 Flutter 它是相同的PageView,并且在方向改变时需要移动,而不是替换。
在编写代码时,PageView每次都会创建一个新元素而不维护状态,因此在构建时使用PageController's initialValue。
不相关的建议
PageView是可滚动的,并且在编写代码时,如果用户滚动,它不会更新BottomNavigationBar. 以下是几种方法,具体取决于您所追求的用户体验。
PageView(
// use never scrollable physics if you only want the user to change pages via the nav bar
physics: NeverScrollableScrollPhysics(),
// use onPageChanged to sync scroll and nav bar
onPageChanged: (page) {
// remove this line from nav bar onTap
setState(() => _page = page);
},
key: _key,
controller: _controller,
children: [
Text("ONE"),
Text("TWO"),
Text("THREE"),
Text("FOUR"),
],
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1439 次 |
| 最近记录: |