如何改变AppBar的文字颜色,FAB的图标颜色普遍使用主题?

rya*_*sh7 20 flutter

我能够设置背景色AppBarColors.amber.这会自动将文本颜色设置为黑色.我知道可能出现的可访问性问题,但无论如何我希望文本颜色为白色.

我仍然可以设置文本颜色,AppBar但我想普遍设置它.

这是我用于我的应用程序的主题.

  title: 'Flutter Demo',
  theme: new ThemeData(
    primarySwatch: Colors.amber,
    textTheme: Theme.of(context).textTheme.apply(
          bodyColor: Colors.white,
          displayColor: Colors.white,
        ),
  ),
Run Code Online (Sandbox Code Playgroud)

Pav*_*r V 37

到目前为止,我测试的解决方案是在appBarTheme中使用foregroundColor

这对我来说效果很好

MaterialApp(
     theme: ThemeData(
          appBarTheme: AppBarTheme(
          backgroundColor: Colors.blue,
          foregroundColor: Colors.white //here you can give the text color
          )
     )
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

MaterialApp(
     theme: ThemeData(
          appBarTheme: AppBarTheme(
          backgroundColor: Colors.white,
          foregroundColor: Colors.black//here you can give the text color
          )
     )
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Nad*_*bit 20

我认为最简单的方法是调整您正在使用的主题的标题颜色:

theme: new ThemeData(
  primarySwatch: Colors.grey,
  primaryTextTheme: TextTheme(
    title: TextStyle(
      color: Colors.white
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

  • 你怎么知道 AppBar 使用 `headline6`?有文档吗..?如果有的话可以指点一下吗?? (7认同)
  • header6 的@Ramesh-X 文档“用于应用程序栏和对话框中的主要文本(例如 AppBar.title 和 AlertDialog.title)”来源:https://api.flutter.dev/flutter/material/TextTheme/headline6。 html (6认同)
  • 该解决方案不再有效,因为触发的错误如下:“title”已弃用,不应使用。这是 2014 年版材料设计中使用的术语。现代术语是标题6。此功能在 v1.13.8 后已弃用。尝试用替换成员替换已弃用成员的使用。 (3认同)

Art*_*ine 18

我使用了稍微不同的技术,我没有使用主题,我只是自定义它的外观,所以当我创建它时看起来像这样:

appBar: new AppBar(
          iconTheme: IconThemeData(color: Colors.white),
          title: const Text('Saved Suggestions', style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.pink,
        ),
Run Code Online (Sandbox Code Playgroud)

  • 这暂时有效,但 OP 特别询问如何使用主题来普遍做到这一点。 (11认同)

dhu*_*981 9

以下是设置AppBar Title颜色的方法.

return new MaterialApp(
  theme: Theme.of(context).copyWith(
      accentIconTheme: Theme.of(context).accentIconTheme.copyWith(
        color: Colors.white
      ),
      accentColor: Colors.amber,
      primaryColor: Colors.amber,
      primaryIconTheme: Theme.of(context).primaryIconTheme.copyWith(
        color: Colors.white
      ),
      primaryTextTheme: Theme
          .of(context)
          .primaryTextTheme
          .apply(bodyColor: Colors.white)),
  home: Scaffold(
    appBar: AppBar(
      title: Text("Theme Demo"),
      leading: IconButton(
        onPressed: (){},
        icon: Icon(Icons.menu),
      ),
    ),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.add),
    ),
  ),
);
Run Code Online (Sandbox Code Playgroud)


ko2*_*2ic 6

我将写出情感属性的变化ThemeData

此处编写的内容是尽可能不影响其他小部件的一种方式。

如果您想更改应用栏标题的颜色,

  primaryTextTheme: TextTheme(
    title: TextStyle(
      color: Colors.white
    )
  ),
Run Code Online (Sandbox Code Playgroud)

如果您想更改应用栏的图标颜色,

  primaryIconTheme: const IconThemeData.fallback().copyWith(
    color: Colors.white,
  ),
Run Code Online (Sandbox Code Playgroud)

如果要更改FAB的图标颜色。

  accentIconTheme: const IconThemeData.fallback().copyWith(
    color: Colors.white,
  ),
Run Code Online (Sandbox Code Playgroud)

另外,当您想更改FAB的颜色时。
仅通过ThemeData的属性是不可能的。因此,您需要直接指定它。但是,最好使用Theme.of(context)

  floatingActionButton: FloatingActionButton(
    backgroundColor: Theme.of(context).primaryColor,
Run Code Online (Sandbox Code Playgroud)


Min*_*mir 5

更新到 Flutter 2.5.1 稳定频道后

他们为主题添加了很多更新。此外,您几乎可以使用以下属性为浮动操作按钮和应用栏的所有内容设置主题。

ThemeData(
        floatingActionButtonTheme: FloatingActionButtonThemeData(
            foregroundColor: Coolor.FLOAT_ACTION_BTN_ICON,
            backgroundColor: Coolor.FLOAT_ACTION_BTN_BACKGROUND),
        appBarTheme: AppBarTheme(
            titleTextStyle: TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
                color: Coolor.APP_BLUE),
            backgroundColor: Coolor.WHITE,
            foregroundColor: Coolor.APP_BLUE));
  
Run Code Online (Sandbox Code Playgroud)

在您的材料应用程序小部件中,您可以传递我们刚刚实现的主题。

MaterialApp(
        title: 'FMS',
        theme: your_theme);
Run Code Online (Sandbox Code Playgroud)

希望有帮助。