flutter 底部導航欄 BottomNavigationBar


話不多說,上代碼

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; }); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM