在Flutter中,页面拆分可以通过以下几种方式实现:
1. 使用 PageView 组件
适用于水平或垂直滑动的页面拆分,常见于引导页、图片浏览器等。
PageView(
children: <Widget>[
Container(color: Colors.red), // 页面1
Container(color: Colors.green), // 页面2
Container(color: Colors.blue), // 页面3
],
)
2. 使用 TabBar + TabBarView
适用于顶部或底部选项卡式的页面拆分。
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(text: '页面1'),
Tab(text: '页面2'),
Tab(text: '页面3'),
],
),
),
body: TabBarView(
children: [
Container(color: Colors.red),
Container(color: Colors.green),
Container(color: Colors.blue),
],
),
),
)
3. 使用 IndexedStack
适用于保持页面状态(不重新构建)的页面切换。
int _currentIndex = 0;
IndexedStack(
index: _currentIndex,
children: <Widget>[
Page1(),
Page2(),
Page3(),
],
)
// 通过 setState 改变 _currentIndex 切换页面
4. 使用 Navigator 管理多个页面
适用于需要独立导航栈的复杂场景(如底部导航+各自页面栈)。
// 在 Scaffold body 中使用 Navigator
Navigator(
key: navigatorKey,
onGenerateRoute: (settings) {
// 根据 settings.name 返回对应页面
},
)
选择建议:
- 简单滑动:用
PageView
- 选项卡式:用
TabBarView
- 保持状态:用
IndexedStack
- 独立导航:用
Navigator
根据具体需求选择合适的方式即可。