Flutter底部導航

目錄

效果展示


實現(xiàn)步驟

1.創(chuàng)建切換的三個頁面

這里由于其他兩個頁面只是顯示的文字不一樣因此我這里就只展示一個頁面的代碼

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("首頁"),
    );
  }
}
2.添加BottomNavigationBar

這里實現(xiàn)底部導航是用的BottomNavigationBar,因此我們需要在主頁面中加入BottomNavigationBar,代碼如下(注意:要在Scaffold中添加)

import 'package:bottomnavigationbar_demo/pages/HomePage.dart';
import 'package:bottomnavigationbar_demo/pages/MinePage.dart';
import 'package:bottomnavigationbar_demo/pages/OrderPage.dart';
import 'package:flutter/material.dart';

void main()=>runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  var pages = [HomePage(),OrderPage(),MinePage()];
  var currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("底部導航"),
      ),
      body: pages[currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index){
          setState(() {
            currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            backgroundColor: Colors.pink,
            icon: Icon(Icons.home),
            label: "首頁"
          ),
          BottomNavigationBarItem(
            backgroundColor: Colors.green,
            icon: Icon(Icons.list),
            label: "訂單"
          ),
          BottomNavigationBarItem(
            backgroundColor: Colors.orange,
            icon: Icon(Icons.person),
            label: "我的"
          ),
        ],
      ),
    );
  }
}

這里BottomNavigationBar必須要設(shè)置items,items里面是多個BottomNavigationBarItem,BottomNavigationBarItem可以設(shè)置圖標和標題同時也可以設(shè)置背景顏色,這里需要注意的是BottomNavigationBar的currentIndex是當前選中的Tab的索引,因此必須要設(shè)置才能看到點擊后的切換效果,另外BottomNavigationBar的type屬性是設(shè)置選擇的效果,就如同開頭展示的那兩種效果,其中BottomNavigationBarType.fixed是普通的效果,BottomNavigationBarType.shifting是比較花哨的效果,設(shè)置了花哨的效果必須要給BottomNavigationBarItem設(shè)置背景顏色才能看到效果,為了方便查看,下面以表格的形式展示出重要的屬性:
BottomNavigationBar

屬性 說明
items 底部導航欄的顯示項(BottomNavigationBarItem)
onTap 選擇TAB時的回調(diào)
currentIndex 當前的索引
type 選擇TAB時的效果(普通:BottomNavigationBarType.fixed,花哨:BottomNavigationBarType.shifting)
fixedColor 底部導航欄type為fixed時導航欄的顏色,如果為空的話默認使用ThemeData.primaryColor

BottomNavigationBarItem

屬性 說明
icon 要顯示的圖標控件(Icon)
label 顯示的標題
backgroundColor BottomNavigationBarType為shifting時的背景顏色

案例源碼

https://gitee.com/itfitness/bottom-navigation-bar-demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容