在 Flutter 中未显示 BottomNavigationBar 标题

Ton*_*ony 6 flutter android-bottomnavigationview

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

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);
          },
        ));
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

Ter*_*ius 14

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

添加类型:BottomNavigationBarType.fixed on BottomNavigationBar

BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   items: <BottomNavigationBarItem>[],
)
Run Code Online (Sandbox Code Playgroud)


Par*_*iya 8

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

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

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

 bottomNavigationBar: BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   currentIndex: _page,
   items: tabsList,
 )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果您只想要“选定”选项卡上的标题,请添加showSelectedLabels: true,showUnselectedLabels: false,像这样,

 bottomNavigationBar: BottomNavigationBar(
   type: BottomNavigationBarType.fixed,
   showSelectedLabels: true,
   showUnselectedLabels: false,
   currentIndex: _page,
   items: tabsList,
 )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


小智 6

你应该使用这个代码:

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',
   ),
 ],
)
Run Code Online (Sandbox Code Playgroud)


Dar*_*han 4

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

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

在此输入图像描述

  • 您有“类型:BottomNavigationBarType.shifting”。如果将其更改为“固定”,则默认情况下会显示文本。 (3认同)