bottomNavigationBar 配置底部导航条。
import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Retrieve Text Input',home: MyBottomNavigationBar(),);}}class MyBottomNavigationBar extends StatefulWidget {@override_MyBottomNavigationBarState createState() => _MyBottomNavigationBarState();}class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {// 定义当前索引值int _currentIndex = 0;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Hello Flutter'),),body: Center(child: Text('Hello Flutter'),),// bottomNavigationBar 底部导航bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex, // 当前选中的索引值onTap: (int index) {setState(() {this._currentIndex = index;});},items: [BottomNavigationBarItem(// icon 可以是 Icon 组件 也可以是 imageicon: Icon(Icons.home),label: '首页',),BottomNavigationBarItem(icon: Icon(Icons.search),label: '搜索',),],),);}}
切换页面:
import 'package:flutter/material.dart';import 'tabs/home.dart';import 'tabs/search.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Retrieve Text Input',home: MyBottomNavigationBar(),);}}class MyBottomNavigationBar extends StatefulWidget {@override_MyBottomNavigationBarState createState() => _MyBottomNavigationBarState();}class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {int _currentIndex = 0;// 定义页面 ListList _pageList = [HomeWidget(),SearchWidget(),];@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Hello Flutter'),),// 根据当前的 currentIndex 渲染对应的页面body: this._pageList[this._currentIndex],bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,onTap: (int index) {setState(() {this._currentIndex = index;});},iconSize: 30, // 改变icon大小fixedColor: Colors.pink, // 改变颜色type: BottomNavigationBarType.fixed, // 配置多个tabItemitems: [BottomNavigationBarItem(icon: Icon(Icons.home),label: '首页',),BottomNavigationBarItem(icon: Icon(Icons.search),label: '搜索',),],),);}}
