Flutter:更改APP Bar顏色
Flutter顏色以#AARRGGBB格式來定義
2 min readSep 2, 2019
單色
declare your Color like this
const PrimaryColor = const Color(0xFF151026);
and then in the MaterialApp
level( will change the AppBar Color in the whole app ) change the PrimaryColor
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: PrimaryColor,
),
home: MyApp(),
);
and if you want to change it in the Widget level just change the backgroundColor
appBar: AppBar(
backgroundColor: PrimaryColor,
),
漸層
You can also add a gradient to the AppBar
like this,
new Scaffold(
appBar: AppBar(
title: Center(child: Text('Awesome AppBar')),
flexibleSpace: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [
const Color(0xFF3366FF),
const Color(0xFF00CCFF),
],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(1.0, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp),
),
),
),
body: ...,
);
LinearGradient parameters:
colors
: an array of the colors to be used in the gradient, in this case, two shades of blue.begin
,end
: position of the first color and the last color, in this case,FractionalOffset
allows us to treat the coordinates as a range from 0 to 1 both for x and y. As we want an horizontal gradient, we use same Y for both measures, and the x changes from 0.0 (left) to 1.0 (right).stops
: this array should have the same size than colors. It defines how the colors will distribute. [0.0, 1.0] will fill it from left to right. [0.0, 0.5] will fill the colors from left to half bar, etc.tileMode
: what to do if the stops do not fill the whole area. In this case, we added clamp (it will reuse the last color used), but as our gradient goes from 0.0 to 1.0, it’s not really necessary.
Hope this helps.