話不多說,上代碼
import 'package:flutter/material.dart'; import 'package:flutter_zhihu/pages/tabs/homeTab.dart'; class TabsController extends StatefulWidget { @override _TabsControllerState createState() => _TabsControllerState(); } class _TabsControllerState extends State<TabsController> { int _currentIndex = 0; final pages = [HomeTab(),HomeTab(),HomeTab(),HomeTab(),HomeTab()]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('標題'), actions: [ IconButton(icon: Icon(Icons.search), onPressed: (){ print('惦記了搜索'); }) ], ), bottomNavigationBar: BottomNavigationBar( items: bottomNavItems,//配置底部菜單 currentIndex: _currentIndex,//當前菜單在第幾個 onTap: (index){
//菜單切換事件 _changePage(index); }, type: BottomNavigationBarType.fixed,//菜單切換效果 ), body: pages[_currentIndex],//菜單頁面切換 ); }
//定義底部菜單 final List<BottomNavigationBarItem> bottomNavItems = [ BottomNavigationBarItem( backgroundColor: Colors.blue, icon: Icon(Icons.home), label: '首頁', ), BottomNavigationBarItem( backgroundColor: Colors.green, icon: Icon(Icons.message), label: '視頻', ), BottomNavigationBarItem( backgroundColor: Colors.amber, icon: Icon(Icons.shopping_cart), label: '會員', ), BottomNavigationBarItem( backgroundColor: Colors.red, icon: Icon(Icons.person), label: '消息', ), BottomNavigationBarItem( backgroundColor: Colors.red, icon: Icon(Icons.person), label: '我的', ), ]; /*切換頁面*/ void _changePage(int index) { /*如果點擊的導航項不是當前項 切換 */ if (index != _currentIndex) { setState(() { _currentIndex = index; }); } } }