The front-end uses css3
the newly added filter
attributes to realize the graying function of the page. One-click global modification of styles, adding filters, adding exposure, etc. flutter
Use ColorFiltered
wrap our 根widget
to achieve page graying.
Today, from the perspective of the front end, let's take a look at the principle and implementation process of graying.
In addition to the common front-end methods, this article will also tell you flutter
how to gray out all the pages in .
Originally, it was planned to be introduced in two articles, but flutter
the content is not much. Finally, after careful consideration, it is better to use one article.
About flutter
how to realize the graying of the page? at the end of the text.
Common front-end implementation and problems
Implementation ideas and principles
Let's take a look at how common mainstream websites 掘金
, 淘宝
, scdn
and are implemented:
nuggets
Taobao
csdn
Through the above cases of mainstream websites, we know that these mainstream websites are css3
all filter
realized through the newly added attribute in .
filter
What is the use of attributes? What are you doing? We can first look at the explanation in MDN :
The CSS property filter applies graphical effects such as blurring or color shifting to elements. Filters are often used to adjust the rendering of images, backgrounds, and borders
The vernacular is that filter
attributes are used to add different filters to elements. This attribute supports passing in various Filter
functions . Among them grayscale
is the key for setting ash. Using grayscale
will change the input image grayscale.
Therefore, you only need to set the following style on html
the element to gray out the page:
html {
filter: grayscale(100%);
}
But we just saw that Taobao and other mainstream websites have many lines, not just one line like the one above, why is this?
answer:
This is because filter
it is css3
a new attribute, not all browsers (kernels) and versions support it, so in order to make more browsers (kernels) and versions grayed out, we see of many rows. But no matter how many lines there are, the effect is actually the same.
html {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-o-filter: grayscale(100%);
-ms-filter: grayscale(100%);
filter: grayscale(100%);
filter: gray;
filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
}
Adapted kernel and version notes
*The picture comes from the Internet
-webkit-
webkit
:webkit
The browser with the prefix can take effect in the kernel, such as the commonchrome
,safari
,移动端浏览器
;-moz-
: withmoz
the prefix canFirefox
take effect in the browser;-o-
: witho
the prefix canOpera
take effect in the browser;-ms-
: withms
the prefix canIE
take effect in the browser;
The last three lines in the above code are all browsers compatible with IE
the kernel . As we all know, ie
browsers are the browsers with the most things, and they can’t do anything, and they all have to be individually adapted.
filter
property extension
1、blur
This property is used to set the element 模糊
effect to apply a Gaussian blur visual effect to the element.
This property is often used to achieve the frosted glass effect of pictures.
.image{
width: 200px;
height: 200px;
filter: blur(1px);
}
2、brightness
This property is used to adjust 亮度
the level of the image to make it appear brighter or darker.
.image{
width: 200px;
height: 200px;
filter: brightness(120%);
}
3、contrast
This property is used to resize the image 对比度
.
.image{
width: 200px;
height: 200px;
filter: contrast(190%);
}
4、opacity
This property is used to adjust 透明
the effect of the image.
.image{
width: 200px;
height: 200px;
filter: opacity(40%);
}
filter
The difference between the opacity
and opacity
these two properties is explained:
filter
Enabled hardwareopacity
-浏览器
acceleratedGPU
renderingopacity
Disable hardware浏览器
acceleratedGPU
rendering
5、sepia
This property is used to adjust the image to add softness 褐色色调
, making the image look warmer and more vintage.
.image{
width: 200px;
height: 200px;
filter: sepia(40%);
}
6、drop-shadow
This property is used to increase the image 阴影
, similar box-shadow
to making the image look more three-dimensional.
.image{
width: 200px;
height: 200px;
filter: drop-shadow(offset-x offset-y blur-radius color);
}
Accepts four parameters:
-
offset-x
:x轴
The length value, specifying the horizontal distance between the element and the projection. Positive values place the shadow to the right of the element, negative values place the shadow to the left. -
offset-y
:y轴
The length value, specifying the vertical distance between the element and the projection. Positive values place the shadow below the element, negative values place the shadow above it. -
blur-radius
: The blur radius of the shadow. The larger the value, the blurrier the shadow becomes. Negative values are not allowed. -
color
: The color of the shadow. Default is black.
.image{
width: 200px;
height: 200px;
filter: drop-shadow(10px 10px 10px #ff0000);
}
7、saturate
This property is used to change the color of the element 饱和度
. Saturated elements are more vibrant in color; saturation can be increased for underexposed images and vice versa.
.image{
width: 200px;
height: 200px;
filter: saturate(40%);
}
8、initial
filter
The default value of will be parsed as none
, using this value has no effect.
.image{
width: 200px;
height: 200px;
filter: initial;
}
9、hereditary
Inherit filter
the property .
/* 父盒子 */
.father-image{
filter: saturate(40%);
}
/* 图片 */
.image{
width: 200px;
height: 200px;
filter: inherit;
}
10、url
Accepts a XML
file that sets a SVG
filter and can contain an anchor to specify a specific filter element.
I'm too lazy to find materials here, so I won't post pictures. last code.
.image{
width: 200px;
height: 200px;
filter: url('./sucai.xml');
}
Kind tips
filter
Multiple attributes can be written and rendered according to the order.
Remember: you cannot use
initial
, in multiple attributesinherit
, otherwise it will have no effect.
.image{
width: 200px;
height: 200px;
filter: blur(1px) saturate(40%);
}
filter
Problems after use
position
If the value used in our project is absolute
or fixed
is used, it will cause style confusion.
reason
When the element position
's value is absolute
or fixed
, filter
one will be created for it 新的包含块或容器
, causing the absolute
or fixed
element's positioning to change.
The vernacular is to absolute
change fixed
the positioning element basis of the or element and become a newly created element.
body
After using the attribute in the tag filter
, filter
one is generated 新的包含块/容器
, which 位置大小
is body
the same as the . fixed
The or absolute
element will be based on this 包含块/容器进行定位
, but not based on it 原元素作为依据进行定位
, so filter
after we can see some interfaces with disordered or invalid styles;
solution
- We can
根元素(也就是html)
addfilter
attributes on it for unified addition - Add
filter
attributes , such as:span
,p
,img
,image
,text
,video
etc.
flutter
Implement graying of the application page in
introduce
Flutter is Google
an open source user interface (UI) toolkit that helps developers efficiently build multi-platform beautiful applications through a set of code bases, supporting 移动
, Web
, 桌面
and 嵌入式平台
. Flutter is an open source and free cross-terminal framework with a loose open source agreement, suitable for commercial projects.
Method to realize
In flutter
is not as complicated as in and 网页
in 小程序
, we only need to use ColorFiltered
the package root directory widget
to achieve the overall page graying.
Add toColorFiltered
We use ColorFilter.mode
the attribute here, BlendMode
the value is color
, we can achieve our effect.
@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: const ColorFilter.mode(
Color(0xff8a8a8a),
BlendMode.color,
),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
The effect after adding: