flutter - Flutter 中的 BottomNavigationBar 样式

我正在尝试 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,
      ),
    );
  }

之前我以为我可以通过在我的主应用主题上将 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(),
    );
  }
}

最佳答案

没有指定BottomNavigationBar的背景颜色的选项,只能更改canvasColor。在不弄乱整个应用程序的情况下实现它的一种方法是将 BottomNavigationBar 包装在带有所需 canvasColorTheme 中。

例子:

  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"),
        )
      ],
    ),
  ),

希望有帮助!

https://stackoverflow.com/questions/49307858/

相关文章:

flutter - 什么情况下 textAlign 属性在 Flutter 中有效?

flutter - BuildContext 在 Flutter 中做了什么?

flutter - 在 dispose() 之后调用 setState()

flutter - 如何在 Flutter 中创建圆形图标按钮?

android - Flutter 找不到 android sdk

android - 在 flutter 应用程序中添加日期选择器的正确方法是什么?

dart - Dart VM 还在使用吗?

flutter - 如何将一列小部件紧紧包裹在卡片内?

dart - 如何在 flutter 中制作全屏对话框?

dart - Flutter:设置AppBar的高度