7

为什么底部导航栏标题不显示?它应该显示在图标下方

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

我在这里错过了什么?

4

5 回答 5

20

当提供超过 3 个 BottomNavigationBar 项目时,如果未指定类型,则根据https://docs.flutter.io/flutter/material/BottomNavigationBar/BottomNavigationBar.html更改为 BottomNavigationBarType.shifting 。这部分信息应该在课程的文档中突出显示。很容易忽略它的位置(我忽略了它)。

添加类型:BottomNavigationBarType.fixed on BottomNavigationBar

BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   items: <BottomNavigationBarItem>[],
)
于 2020-01-29T04:35:19.577 回答
10

项目的长度必须至少为两个,并且每个项目的图标和标题不得为空。

如果 type 为 null 则BottomNavigationBarType.fixed在有两个或三个项目时使用,BottomNavigationBarType.shifting否则。

如果您想每次都显示标题,请添加类型:BottomNavigationBarType.fixed,执行此操作

 bottomNavigationBar: BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   currentIndex: _page,
   items: tabsList,
 )

在此处输入图像描述

如果您只想在 Selected 选项卡上添加标题,然后添加showSelectedLabels: true,showUnselectedLabels: false,喜欢这样,

 bottomNavigationBar: BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   showSelectedLabels: true,
   showUnselectedLabels: false,
   currentIndex: _page,
   items: tabsList,
 )

在此处输入图像描述

于 2020-09-01T06:55:03.630 回答
4

你应该使用这个代码:

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',
   ),
 ],
)
于 2021-01-01T21:48:59.227 回答
4

标题确实显示了,但white如果仔细观察,它是彩色的。只需在文本中添加一个color即可正确显示它。

title: new Text('One', style: TextStyle(color: Colors.black))

在此处输入图像描述

于 2019-08-14T10:57:28.483 回答
0

这是显示导航栏项目标签的简单技巧

 BottomNavigationBarItem(
            icon: Column(
              children: [
                new Icon(widget.currentTab == 2 ? Icons.dashboard_outlined : Icons.dashboard_outlined),
                Text("Homes")
              ],
            ),
            label: 'Home',

          ),
于 2021-04-15T07:59:40.630 回答