从 Flutter 2.5.0 开始,为了符合“Material You”,我们应该尽可能使用ColorScheme。应用栏颜色受以下因素控制:
-
如果主题brightness 是light,则使用primary 颜色。
-
如果主题brightness 是dark,则使用surface 颜色。
例如:
灯光模式
将brightness设置为light,然后将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")),
),
)
黑暗模式
将brightness 设置为dark,然后将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")),
),
)