Flutter中的Style BottomNavigationBar

spo*_*oss 29 dart flutter bottomnavigationview

我正在尝试Flutter,我正在尝试改变BottomNavigationBar应用程序的颜色,但我所能实现的只是改变了BottomNavigationItem(图标和文本)的颜色.

这是我宣布我的地方BottomNavigationBar:

class _BottomNavigationState extends State<BottomNavigationHolder>{

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: null,
      body: pages(),
      bottomNavigationBar:new BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
              icon: const Icon(Icons.home),
              title: new Text("Home")
          ),
          new BottomNavigationBarItem(
              icon: const Icon(Icons.work),
              title: new Text("Self Help")
          ),
          new BottomNavigationBarItem(
              icon: const Icon(Icons.face),
              title: new Text("Profile")
          )
        ],
        currentIndex: index,
        onTap: (int i){setState((){index = i;});},
        fixedColor: Colors.white,
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

之前我以为我通过canvasColor在我的主应用主题上编辑绿色来解决它,但它搞砸了整个应用程序配色方案:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
        canvasColor: Colors.green
      ),
      home: new FirstScreen(),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Hem*_*Raj 83

没有选项可以指定背景颜色BottomNavigationBar但可以更改canvasColor.你可以实现它不会弄乱整个应用程序的一种方法是通过包装BottomNavigationBar在一个Theme与期望canvasColor.

例:

  bottomNavigationBar: new Theme(
    data: Theme.of(context).copyWith(
        // sets the background color of the `BottomNavigationBar`
        canvasColor: Colors.green,
        // sets the active color of the `BottomNavigationBar` if `Brightness` is light
        primaryColor: Colors.red,
        textTheme: Theme
            .of(context)
            .textTheme
            .copyWith(caption: new TextStyle(color: Colors.yellow))), // sets the inactive color of the `BottomNavigationBar`
    child: new BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: 0,
      items: [
        new BottomNavigationBarItem(
          icon: new Icon(Icons.add),
          title: new Text("Add"),
        ),
        new BottomNavigationBarItem(
          icon: new Icon(Icons.delete),
          title: new Text("Delete"),
        )
      ],
    ),
  ),
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 实际上,禁用的颜色是从textTheme的caption获取的。我已经更新了答案,以显示您如何实现它。希望有帮助! (3认同)

小智 57

接受的答案并非完全错误。但是,BottomNavigationBar实际上是否具有backgroundColor. 根据文档

如果 type 为 BottomNavigationBarType.shifting 并且项目设置了 BottomNavigationBarItem.backgroundColor,则项目的 backgroundColor 将溅起并覆盖此颜色。

这意味着BottomNavigation的 backgroundColor 将被单个项目 backgroundColor 覆盖,因为默认类型是BottomNavigationBarType.shifting.

要解决此问题,只需将以下属性添加到声明的BottomNavigationbar小部件。

type: BottomNavigationBarType.fixed,
Run Code Online (Sandbox Code Playgroud)

注意:但是,如果您想要移动效果,则必须为每个项目声明颜色,或者包装允许覆盖子小部件背景颜色的小部件。

即类似Container小部件的东西。


Ish*_*ndo 13

如果类型是固定的,可以通过将颜色设置为 backgroundColor 属性来更改。

BottomNavigationBar(
          backgroundColor: Colors.red,
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
                icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Home'),),
            BottomNavigationBarItem(
                icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Self Help'),),
            BottomNavigationBarItem(
                icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Profile'),),
          ]
        )
Run Code Online (Sandbox Code Playgroud)

如果类型正在转换,它将在底部导航栏项中使用颜色。

BottomNavigationBar(
          backgroundColor: Colors.red,
          type: BottomNavigationBarType.shifting,
          items: [
            BottomNavigationBarItem(
                icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Home'),
                backgroundColor: Colors.red),
            BottomNavigationBarItem(
                icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Self Help'),
                backgroundColor: Colors.blue),
            BottomNavigationBarItem(
                icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
                title: new Text('Profile'),
                backgroundColor: Colors.amber),
          ]

        )
Run Code Online (Sandbox Code Playgroud)

您可以看到,即使我设置了 backgroundColor 属性,它也不会应用该颜色,并且 BottomNavigationBarItem 小部件内的背景颜色将覆盖该颜色。

这里找到


小智 12

BottomNavigationBar目前,您可以直接从 中设置它们的Theme样式,如下所示:

ThemeData(
    bottomNavigationBarTheme: BottomNavigationBarThemeData(
        backgroundColor: Colors.grey[900],
        elevation: 10,
        selectedLabelStyle: TextStyle(
            color: Color(0xFFA67926), fontFamily: 'Montserrat', fontSize: 14.0
        ),
        unselectedLabelStyle: TextStyle(
            color: Colors.grey[600], fontFamily: 'Montserrat', fontSize: 12.0
        ),
        selectedItemColor: Color(0xFFA67926),
        unselectedItemColor: Colors.grey[600],
        showUnselectedLabels: true,
    ),
)
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 10

在此处输入图片说明

以前没有直接设置颜色的方法,但是现在可以使用了。

BottomNavigationBar(
  backgroundColor: Colors.red,
  selectedItemColor: Colors.black,
  unselectedItemColor: Colors.white,
  ...
)  
Run Code Online (Sandbox Code Playgroud)


Mr *_*ial 6

title弃用。我们用label它来代替。
对于label,我们可以使用相应的属性:selectedLabelStyle, unselectedLabelStyle
例如:

bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Theme.of(context).accentColor,
          selectedFontSize: 0,
          unselectedFontSize: 0,
          iconSize: 22,
          elevation: 0,
          backgroundColor: Colors.transparent,
          selectedIconTheme: IconThemeData(size: 28),
          unselectedItemColor: Theme.of(context).focusColor.withOpacity(1),
          selectedLabelStyle: Theme.of(context).textTheme.bodyText1.merge(TextStyle(fontSize: 12)),
          unselectedLabelStyle: Theme.of(context).textTheme.button.merge(TextStyle(fontSize: 11)),
          showUnselectedLabels: true,
          currentIndex: widget.currentTabIdx,
          onTap: (int i) {
            this._selectTab(i);
          },
          showSelectedLabels: true,
          // this will be set when a new tab is tapped
          items: [
            BottomNavigationBarItem(
              icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME) ,
              activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME, color: Theme.of(context).accentColor),
              label: 'Home',
            ),
            BottomNavigationBarItem(
                label: 'Categories',
              icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY),
              activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY, color: Theme.of(context).accentColor) ,
            ),
            BottomNavigationBarItem(
              icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, ) ,
              activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, color: Theme.of(context).accentColor ) ,
              label: 'Order History',
            ),
            BottomNavigationBarItem(
              icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART,) ,
              activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART, color: Theme.of(context).accentColor) ,
              label: 'Cart',
            ),
          ],
Run Code Online (Sandbox Code Playgroud)


Rah*_*ade 5

设置以下属性以更改背景选中未选中的颜色

bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.blue,
        selectedItemColor: Colors.black,
        unselectedItemColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        ...
)
Run Code Online (Sandbox Code Playgroud)