为什么底部导航栏标题不显示?它应该显示在图标下方
class FlutterProject extends StatefulWidget {
final String title = "Flutter Bottom Tab demo";
@override
GoalsListState createState() {
return GoalsListState();
}
}
class GoalsListState extends State<FlutterProject>
with SingleTickerProviderStateMixin {
int _cIndex = 0;
void _incrementTab(index) {
setState(() {
_cIndex = index;
});
}
final List<Widget> _children = [
new One(),
new Two(),
new Three(),
new Four(),
new More()
];
@override
Widget build(BuildContext context) {
return new Scaffold(
body: _children[_cIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _cIndex,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
icon:
Icon(Icons.graphic_eq, color: Color.fromARGB(255, 0, 0, 0)),
title: new Text('One')),
BottomNavigationBarItem(
icon: Icon(Icons.report_problem,
color: Color.fromARGB(255, 0, 0, 0)),
title: new Text('Two')),
BottomNavigationBarItem(
icon: Icon(Icons.work, color: Color.fromARGB(255, 0, 0, 0)),
title: new Text('Three')),
BottomNavigationBarItem(
icon: Icon(Icons.domain, color: Color.fromARGB(255, 0, 0, 0)),
title: new Text('Four')),
BottomNavigationBarItem(
icon: Icon(Icons.menu, color: Color.fromARGB(255, 0, 0, 0)),
title: new Text('Five')),
],
onTap: (index) {
_incrementTab(index);
},
));
}
}
我在这里想念什么?
答案 0 :(得分:9)
如果提供了3个以上的BottomNavigationBar项目,但类型未指定,则根据https://docs.flutter.io/flutter/material/BottomNavigationBar/BottomNavigationBar.html更改为BottomNavigationBarType.shifting。这些信息应在课程文档中突出显示。很容易忽略它在哪里(我忽略了它)。
添加类型:BottomNavigationBarType。已固定在BottomNavigationBar上
BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[],
)
答案 1 :(得分:4)
项目的长度必须至少为两个,并且每个项目的图标和标题不能为空。
如果type为null,则当有两个或三个项目时使用BottomNavigationBarType.fixed
,否则使用BottomNavigationBarType.shifting
。
如果您想每次都显示标题,请添加类型:BottomNavigationBarType.fixed,
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _page,
items: tabsList,
)
如果只希望标题位于“选定”选项卡上,则像这样添加到showSelectedLabels: true,
和showUnselectedLabels: false,
,
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
showSelectedLabels: true,
showUnselectedLabels: false,
currentIndex: _page,
items: tabsList,
)
答案 2 :(得分:1)
确实显示了标题,但如果您仔细观察,则标题颜色为white
。
只需在文本中添加color
即可正确显示它。
title: new Text('One', style: TextStyle(color: Colors.black))
答案 3 :(得分:1)
你应该使用这个代码:
bottomNavigationBar: BottomNavigationBar(
//use both properties
type: BottomNavigationBarType.fixed,
showUnselectedLabels: true,
//-----------
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.icon1),
label:'item 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.icon2),
label: 'item 2',
),
],
)
答案 4 :(得分:0)
这里有一个简单的技巧来显示导航栏项目的标签
BottomNavigationBarItem(
icon: Column(
children: [
new Icon(widget.currentTab == 2 ? Icons.dashboard_outlined : Icons.dashboard_outlined),
Text("Homes")
],
),
label: 'Home',
),