BottomNavigationBar
顯示在應(yīng)用程序的底部,用于在少量視圖中進(jìn)行選擇翩蘸,通常在三到五之間所意。
底部導(dǎo)航欄通常與Scaffold
結(jié)合使用,它作為Scaffold.bottomNavigationBar
參數(shù)提供催首。
底部導(dǎo)航欄的類型更改其項(xiàng)目的顯示方式扶踊。如果未指定,則當(dāng)少于四個項(xiàng)時它會自動設(shè)置為BottomNavigationBarType.fixed
郎任,否則為BottomNavigationBarType.shifting
秧耗。
BottomNavigationBarType.fixed
,當(dāng)少于四個項(xiàng)目時的默認(rèn)值舶治。如果選中的項(xiàng)目為非null分井,則使用selectedItemColor
渲染所選項(xiàng)目,否則使用主題的ThemeData.primaryColor
霉猛。如果backgroundColor
為null
杂抽,則導(dǎo)航欄的背景顏色默認(rèn)為Material
背景顏色ThemeData.canvasColor
(基本上是不透明的白色)。
BottomNavigationBarType.shifting
韩脏,當(dāng)有四個或更多項(xiàng)時的默認(rèn)值。如果selectedItemColor
為null
铸磅,則所有項(xiàng)目都以白色呈現(xiàn)赡矢。導(dǎo)航欄的背景顏色與所選項(xiàng)目的BottomNavigationBarItem.backgroundColor
相同。在這種情況下阅仔,假設(shè)每個項(xiàng)目將具有不同的背景顏色吹散,并且背景顏色將與白色形成鮮明對比。
此示例顯示BottomNavigationBar八酒,因?yàn)樗赟caffold小部件中使用空民。 BottomNavigationBar有三個BottomNavigationBarItem小部件,currentIndex設(shè)置為索引0.所選項(xiàng)目為琥珀色。 _onItemTapped函數(shù)更改所選項(xiàng)的索引界轩,并在Scaffold的中心顯示相應(yīng)的消息:
源碼:
int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
構(gòu)造方法(Constructors)
創(chuàng)建一個底部導(dǎo)航欄画饥,通常用作Scaffold的Scaffold.bottomNavigationBar參數(shù)。
BottomNavigationBar({
Key key,
@required List<BottomNavigationBarItem> items,
ValueChanged<int> onTap,
int currentIndex: 0,
double elevation: 8.0,
BottomNavigationBarType type,
Color fixedColor,
Color backgroundColor,
double iconSize: 24.0,
Color selectedItemColor,
Color unselectedItemColor,
double selectedFontSize: 14.0,
double unselectedFontSize: 12.0,
bool showSelectedLabels: true,
bool showUnselectedLabels
})
屬性(Properties)
-
backgroundColor → Color
背景顏色
-
currentIndex → int
當(dāng)前活動
BottomNavigationBarItem
的項(xiàng)目索引(一般就是當(dāng)前選中的那個項(xiàng)目索引) -
elevation → double
此底部導(dǎo)航欄的Z坐標(biāo)
-
fixedColor → Color
選中項(xiàng)目顏色的值(只讀)
-
iconSize → double
所有
BottomNavigationBarItem
圖標(biāo)的大小 -
items → List<BottomNavigationBarItem>
定義在底部導(dǎo)航欄中排列的按鈕項(xiàng)的外觀
-
onTap → ValueChanged<int>
點(diǎn)擊其中一個項(xiàng)目時響應(yīng)事件
-
selectedFontSize → double
選中時
BottomNavigationBarItem
標(biāo)簽的字體大小 -
selectedItemColor → Color
選中時
BottomNavigationBarItem.icon
和BottomNavigationBarItem.label
的顏色 -
showSelectedLabels → bool
是否為未選擇的
BottomNavigationBarItems
顯示標(biāo)簽 -
showUnselectedLabels → bool
是否為選定的
BottomNavigationBarItem
顯示標(biāo)簽 -
type → BottomNavigationBarType
定義
BottomNavigationBar
的布局和行為 -
unselectedFontSize → double
未選中
BottomNavigationBarItem
標(biāo)簽的字體大小 -
unselectedItemColor → Color
未選中的
BottomNavigationBarItem.icon
和BottomNavigationBarItem.labels
的顏色 -
hashCode → int
對象的哈希值(只讀)
-
key → Key
控制一個小部件如何替換樹中的另一個小部件
-
runtimeType → Type
表示對象的運(yùn)行時類型(只讀)