简体   繁体   English

如何在颤动中为容器设置不透明度

[英]How to put opacity for container in flutter

I want to put opacity for container which contain hexadecimal color code.我想为包含十六进制颜色代码的容器设置不透明度。 How can it be done?如何做呢?

Here is my current code:这是我当前的代码:

final body = Container(
  width: MediaQuery.of(context).size.width,

  margin: const EdgeInsets.only(left: 40.0, right: 40.0),
  padding: EdgeInsets.all(28.0),
   decoration: new BoxDecoration(
     color:   const Color(0xFF0E3311),//here i want to add opacity

   border: new Border.all(color: Colors.black54,
   ),
       borderRadius: new BorderRadius.only(
           topLeft: const Radius.circular(40.0),
           topRight: const Radius.circular(40.0),
       bottomLeft: const Radius.circular(40.0),
       bottomRight:const Radius.circular(40.0) )
),

  child: Column(
    children: <Widget>[ email, password,loginButton],
  ),
);

Change the line换线

const Color(0xFF0E3311)

to

const Color(0xFF0E3311).withOpacity(0.5)

or any value you want.或您想要的任何值。

If you just want to set an opacity to your color it's simple as adding 2 hex numbers before your color code.如果您只想为颜色设置不透明度,只需在颜色代码前添加 2 个十六进制数字即可。 Check this answer to know all the values.检查此答案以了解所有值。

But if you want to change the opacity of all the widget, in your case a Container, you can wrap it into a Opacity widget like this:但是如果你想改变所有小部件的不透明度,在你的情况下是一个容器,你可以将它包装成一个不透明度小部件,如下所示:

double _opacityValue = 0.50;//This value goes from 0.0 to 1.0. In this case the opacity is from 50%

final Widget _bodyWithOpacity = Opacity(
  opacity: _opacityValue,
  child: body,
);

Check here the Opacity's documentation and this quick video if you want to know more about it!如果您想了解更多信息,请在此处查看 Opacity 的文档和此快速视频

Flutter uses hexadecimal ARGB values for colors, which are formatted as const Color(0xAARRGGBB). Flutter 使用十六进制的 ARGB 颜色值,格式为 const Color(0xAARRGGBB)。 That first pair of letters, the AA, represent the alpha channel.第一对字母 AA 代表 alpha 通道。 You must convert your decimal opacity values to a hexadecimal value.您必须将十进制不透明度值转换为十六进制值。

Hex Opacity Values:十六进制不透明度值:

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

For instance:例如:

static const Color mainColor = Color(0xff0097A7);

to:到:

static  Color accentColor = Color(0x1A0097A7);

will change the opacity to 10%.会将不透明度更改为 10%。

Flutter uses a 32 bit color value in ARGB format, where A = Alpha, R = RED, G = GREEN and B = BLUE. Flutter 使用 ARGB 格式的 32 位颜色值,其中 A = Alpha,R = RED,G = GREEN 和 B = BLUE。

So to control the opacity you can change the values of first two digits of the hex value in const Color(0xFF0E3311) , you can use values ranging from 0x000E3311 , 0x010E3311 .... 0xFF0E3311 .因此,要控制不透明度,您可以更改const Color(0xFF0E3311)十六进制值的前两位的值,您可以使用范围从0x000E33110x010E3311 .... 0xFF0E3311

Hope that helps!希望有帮助!

here in code const Color(0xFF0E3311) after 0x two values (in above code 'FF') are for opacity.在代码const Color(0xFF0E3311) ,0x 之后的两个值(在上面的代码“FF”中)用于不透明度。 'FF' for opaque and '00' for fully transparent. 'FF' 表示不透明,'00' 表示完全透明。 so by altering this value you can change color opacity.所以通过改变这个值,你可以改变颜色的不透明度。 Also we get by Colors class diff opacity value color for white and black.我们还通过 Colors 类获得白色和黑色的 diff 不透明度值颜色。 for example Colors.white70 means white color with 70% opacity例如Colors.white70表示不透明度为 70% 的白色

我们也可以将Color.fromRGBO(255, 255, 255, 0.5)用于不透明度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM