[英]How can we change appbar background color in flutter
I am trying to set a common theme for app so I need to change appbar
color as a color that indicates hex code #0f0a1a
我正在尝试为应用程序设置一个通用主题,因此我需要将应用程序栏颜色更改为指示十六进制代码
#0f0a1a
appbar
颜色
const MaterialColor toolbarColor = const MaterialColor(
0xFF151026, const <int, Color>{0: const Color(0xFF151026)});
I try this piece of code to make a custom color but fails.我尝试使用这段代码制作自定义颜色,但失败了。 How can I do this from
themeData
?我怎样才能从
themeData
做到这一点?
Declare your Color:声明你的颜色:
const primaryColor = Color(0xFF151026);
In the MaterialApp
level (will change the AppBar Color in the whole app ) change primaryColor
在
MaterialApp
级别(将更改整个应用中的 AppBar 颜色)更改primaryColor
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: primaryColor,
),
home: MyApp(),
);
and if you want to change it on the Widget level modify the backgroundColor
如果您想在 Widget 级别更改它,请修改
backgroundColor
appBar: AppBar(
backgroundColor: primaryColor,
),
If you don't want to change the whole PrimaryColor
you can also define AppBarTheme
in your ThemeData
:如果您不想更改整个
PrimaryColor
,您还可以在ThemeData
AppBarTheme
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
appBarTheme: AppBarTheme(
color: const Color(0xFF151026),
)),
home: myApp(),
)
As of Flutter 2.5.0 , to comply with "Material You", we should try to use ColorScheme
whenever possible.从 Flutter 2.5.0开始,为了符合“Material You”,我们应该尽可能地尝试使用
ColorScheme
。 The app bar color is controlled by:应用栏颜色由以下控制:
If theme brightness
is light
, use primary
color.如果主题
brightness
light
,请使用primary
。
If theme brightness
is dark
, use surface
color.如果主题
brightness
dark
,请使用surface
颜色。
For examples:举些例子:
Light Mode灯光模式
Set brightness
to light
, then set primary
and onPrimary
to yellow and black, and set all other colors to grey to showcase that they are not relevant to AppBar:将
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")),
),
)
Dark Mode黑暗模式
Set brightness
to dark
, then set surface
and onSurface
to yellow and black, all others to grey to showcase that they are not relevant to AppBar.将
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")),
),
)
include backgroundColor: to appbar包括 backgroundColor: 到 appbar
appBar: AppBar(
title: const Text('Example'),
backgroundColor: Colors.black,
),
According to AppBar
description On Flutter 2.5 , it uses ColorScheme.primary
by default.根据
AppBar
的描述On Flutter 2.5 ,它默认使用ColorScheme.primary
。
The default app bar [backgroundColor] is the overall theme's [ColorScheme.primary] if the overall theme's brightness is [Brightness.light].
如果整体主题的亮度为 [Brightness.light],则默认应用栏 [backgroundColor] 为整体主题的 [ColorScheme.primary]。 Unfortunately this is the same as the default [ButtonStyle.foregroundColor] for [TextButton] for light themes.
不幸的是,这与浅色主题的 [TextButton] 的默认 [ButtonStyle.foregroundColor] 相同。 In this case a preferable text button foreground color is [ColorScheme.onPrimary], a color that contrasts nicely with [ColorScheme.primary].
在这种情况下,优选的文本按钮前景色是 [ColorScheme.onPrimary],这是一种与 [ColorScheme.primary] 形成鲜明对比的颜色。 to remedy the problem, override [TextButton.style]:
要解决此问题,请覆盖 [TextButton.style]:
try using colorScheme
尝试使用
colorScheme
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: const Color(0xFF151026),
),
),
home: MyApp(),
),
And to use somewhere else并在其他地方使用
Theme.of(context).colorScheme.primary,
Or you can just call backgroundColor
on Appbar
.或者你可以在
Appbar
上调用backgroundColor
。
For more, visit ThemeData-class有关更多信息,请访问ThemeData 类
With the new Material 3 and Flutter 3 updates, background color for AppBar can be changed using surfaceTintColor.随着新的 Material 3 和 Flutter 3 的更新,AppBar 的背景颜色可以使用 surfaceTintColor 进行更改。
Either inside the AppBar like this:像这样在 AppBar 内部:
return AppBar(
...
surfaceTintColor: backgroundColor ?? CommonColors.lightColor,
);
Or inside the ThemeData class like this:或在 ThemeData 类中,如下所示:
ThemeData.light().copyWith(
...
appBarTheme: AppBarTheme(
backgroundColor: CommonColors.lightColor,
surfaceTintColor: CommonColors.lightColor,
actionsIconTheme: const IconThemeData(color: Colors.white),
),
),
You can use Flutter ThemeData,if you want to set a theme for your entire application:如果要为整个应用程序设置主题,可以使用 Flutter ThemeData:
class HomePage extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'MyApp',),
);
}
}....
Then if you want to change certain elements of your primary and secondary colors,you can achieve this using the colorScheme from Swatch.然后,如果您想更改原色和二次色的某些元素,您可以使用 Swatch 的 colorScheme 来实现。
Here is an example using colorScheme :这是使用 colorScheme 的示例:
class HomePage extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.blue,//the color of your Appbar will be blue
).copyWith(
secondary: Colors.green,
//your accent color-floating action will appear green
),
),
home: MyHomePage(title: 'MyApp',),
);
要在整个应用程序中更改 Appbar 背景颜色:
MaterialApp(theme: ThemeData(appBarTheme: AppBarTheme(backgroundColor: Colors.blueGrey),),);
Since flutter 2.5+, the working solution will be simply use ColorScheme
in ThemeData:从颤振 2.5+ 开始,工作解决方案将简单地在
ColorScheme
中使用 ColorScheme:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: const AppBarWidget(),
theme: ThemeData.light().copyWith(
colorScheme: ColorScheme.fromSwatch().copyWith(
// change the appbar color
primary: Colors.green[800],
),
),
);
}
}
class AppBarWidget extends StatelessWidget {
const AppBarWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AppBar'),
),
);
}
}
Except change AppBar's background color, you need to set shadowColor
to transparent or change elevation
to 0:除了改变 AppBar 的背景颜色,你需要设置
shadowColor
为透明或改变elevation
为 0:
appBar: AppBar(
backgroundColor: Colors.red,
shadowColor: Colors.transparent,
//elevation: 0,
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.