我们如何在Flutter中更改Appbar背景颜色

Vin*_*han 8 dart flutter flutter-layout

我正在尝试为应用设置通用主题,因此我需要将appbar颜色更改为表示十六进制代码#0f0a1a的颜色

const MaterialColor toolbarColor = const MaterialColor(
    0xFF151026, const <int, Color>{0: const Color(0xFF151026)});
Run Code Online (Sandbox Code Playgroud)

我尝试使用这段代码来制作自定义颜色,但失败了。我该如何从themeData做到这一点?

Hug*_*o H 31

如果你不想改变整体,PrimaryColor你也可以AppBarTheme在你的ThemeData

MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
       appBarTheme: AppBarTheme(
     color: Color(0xFF151026),
  )),
  home: myApp(),
)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这应该是可接受的答案,因为通过这种方法,我们可以保留 PrimaryColor 不变,但同时为 AppBar 指定颜色,甚至不直接在我们的代码中创建(例如,showLicensePage) (3认同)

use*_*613 22

在当前版本的 Flutter 中ColorScheme,为了顺应新的“Material You”设计,我们应该尽可能地使用。应用栏颜色由以下各项控制:

  1. 如果主题brightnesslight,则使用primary颜色。

  2. 如果主题brightnessdark,则使用surface颜色。

举些例子:

灯光模式

设置brightnesslight,然后将primary和设置onPrimary为黄色和黑色,并将所有其他颜色设置为灰色以表明它们与 AppBar 无关:

灯光模式演示

MaterialApp(
  theme: ThemeData(
    colorScheme: ColorScheme(
      brightness: Brightness.light,
      primary: Colors.yellow,
      onPrimary: Colors.black,
      // Colors that are not relevant to AppBar in LIGHT mode:
      primaryVariant: Colors.grey,
      secondary: Colors.grey,
      secondaryVariant: Colors.grey,
      onSecondary: Colors.grey,
      background: Colors.grey,
      onBackground: Colors.grey,
      surface: Colors.grey,
      onSurface: Colors.grey,
      error: Colors.grey,
      onError: Colors.grey,
    ),
  ),
  home: Scaffold(
    appBar: AppBar(title: Text("Light Mode Demo")),
  ),
)
Run Code Online (Sandbox Code Playgroud)

深色模式

设置brightnessdark,然后surface将 和设置onSurface为黄色和黑色,所有其他设置为灰色以表明它们与 AppBar 无关。

在此输入图像描述

MaterialApp(
  theme: ThemeData(
    colorScheme: ColorScheme(
      brightness: Brightness.dark,
      surface: Colors.yellow,
      onSurface: Colors.black,
      // Colors that are not relevant to AppBar in DARK mode:
      primary: Colors.grey,
      onPrimary: Colors.grey,
      primaryVariant: Colors.grey,
      secondary: Colors.grey,
      secondaryVariant: Colors.grey,
      onSecondary: Colors.grey,
      background: Colors.grey,
      onBackground: Colors.grey,
      error: Colors.grey,
      onError: Colors.grey,
    ),
  ),
  home: Scaffold(
    appBar: AppBar(title: Text("Dark Mode Demo")),
  ),
)
Run Code Online (Sandbox Code Playgroud)


Rao*_*che 16

像这样声明你的颜色

const PrimaryColor = const Color(0xFF151026);
Run Code Online (Sandbox Code Playgroud)

然后在MaterialApp关卡中(将更改整个应用中的AppBar颜色),PrimaryColor

return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
   primaryColor: PrimaryColor,
   ),
  home: MyApp(),
);
Run Code Online (Sandbox Code Playgroud)

如果您想在Widget级进行更改,只需更改 backgroundColor

  appBar: AppBar(
    backgroundColor: PrimaryColor,
  ),
Run Code Online (Sandbox Code Playgroud)

  • 你好。这在 Flutter 2.8 中不起作用有人可以确认一下吗? (7认同)

ALE*_*ANO 6

包括 backgroundColor: 到应用栏

   appBar: AppBar(
      title: const Text('Example'),
      backgroundColor: Colors.black,
    ),
Run Code Online (Sandbox Code Playgroud)


Dhr*_*rma 6

随着新的 Material 3 和 Flutter 3 更新,可以使用 surfaceTintColor 更改 AppBar 的背景颜色。

在 AppBar 内,如下所示:

return AppBar(
  ...
  surfaceTintColor: backgroundColor ?? CommonColors.lightColor,
 );
Run Code Online (Sandbox Code Playgroud)

或者在 ThemeData 类中像这样:

ThemeData.light().copyWith(
  ...
  appBarTheme: AppBarTheme(
    backgroundColor: CommonColors.lightColor,
    surfaceTintColor: CommonColors.lightColor,
    actionsIconTheme: const IconThemeData(color: Colors.white),
  ),
),
Run Code Online (Sandbox Code Playgroud)