How to put opacity for container in flutter

不羁的心 提交于 2020-06-13 14:19:28

问题


I want to put opacity for container which contain hexadecimal color code. I am new to flutter. Please help me. Here is the code. Thanks in advance.

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],
  ),
);

回答1:


Change the line

const Color(0xFF0E3311)

to

const Color(0xFF0E3311).withOpacity(0.5)

or any value you want.




回答2:


If you just want to set an opacity to your color it's simple as adding 2 hex numbers before your color code. 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!




回答3:


Flutter uses a 32 bit color value in ARGB format, where A = Alpha, R = RED, G = GREEN and 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.

Hope that helps!




回答4:


here in code const Color(0xFF0E3311) after 0x two values (in above code 'FF') are for opacity. 'FF' for opaque and '00' for fully transparent. so by altering this value you can change color opacity. Also we get by Colors class diff opacity value color for white and black. for example Colors.white70 means white color with 70% opacity



来源:https://stackoverflow.com/questions/55294676/how-to-put-opacity-for-container-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!