Dart 我们如何在Flatter中更改appbar背景色

Dart 我们如何在Flatter中更改appbar背景色,dart,flutter,flutter-layout,Dart,Flutter,Flutter Layout,我正在尝试为应用程序设置一个公共主题,因此我需要将appbar颜色更改为表示十六进制代码的颜色 const MaterialColor toolbarColor = const MaterialColor( 0xFF151026, const <int, Color>{0: const Color(0xFF151026)}); const MaterialColor工具栏颜色=const MaterialColor( 0xFF151026,常数{0:const Color(

我正在尝试为应用程序设置一个公共主题,因此我需要将
appbar
颜色更改为表示十六进制代码
的颜色

const MaterialColor toolbarColor = const MaterialColor(
    0xFF151026, const <int, Color>{0: const Color(0xFF151026)});
const MaterialColor工具栏颜色=const MaterialColor(
0xFF151026,常数{0:const Color(0xFF151026)});
我尝试使用这段代码来定制颜色,但失败了。 如何从主题数据中执行此操作?

声明您的颜色:

const primaryColor = const Color(0xFF151026);
MaterialApp
级别(将更改整个应用程序中的AppBar颜色)中,更改
primaryColor

return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
   primaryColor: primaryColor,
   ),
  home: MyApp(),
);
  appBar: AppBar(
    backgroundColor: primaryColor,
  ),
如果您想在小部件级别更改它,请修改
backgroundColor

return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
   primaryColor: primaryColor,
   ),
  home: MyApp(),
);
  appBar: AppBar(
    backgroundColor: primaryColor,
  ),

如果不想更改整个
PrimaryColor
,还可以在
主题数据中定义
AppBarTheme

MaterialApp(
标题:“颤振演示”,
主题:主题数据(
阿帕巴瑟姆:阿帕巴瑟姆(
颜色:颜色(0xFF151026),
)),
主页:myApp(),
)

包括背景颜色:至appbar

   appBar: AppBar(
      title: const Text('Example'),
      backgroundColor: Colors.black,
    ),

它是如何失败的?任何错误?仿真程序中显示NoSuchMethodException。primarycolor是否需要materialColor?谢谢,这应该是可以接受的答案,因为通过这种方法,我们可以保持primarycolor不变,但同时为AppBar指定颜色,而不是直接在代码中创建(例如,showLicensePage)。这是最好的方法,因为它直接针对AppBar。。。textTheme属性应该用于设置标题样式。。。谢谢我们如何使用定义文本appbar文本的颜色为整个应用程序中的所有appbar定义它?