BackdropFilter是Flutter中一种用于应用滤镜效果的小部件。它可以用于给背景图像或其他小部件添加模糊、颜色过滤等效果。
以下是一个使用BackdropFilter应用滤镜效果的示例代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://example.com/image.jpg'),
fit: BoxFit.cover,
),
),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(
color: Colors.black.withOpacity(0.5),
child: Center(
child: Text(
'Hello World',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
),
),
);
}
}
在上面的示例中,我们首先使用一个Container小部件来展示一个背景图像。然后,我们将BackdropFilter小部件放在Container内部,并设置其filter属性为ImageFilter.blur(sigmaX: 10, sigmaY: 10)。这个滤镜效果将会给背景图像添加一个模糊效果。
接下来,我们在BackdropFilter小部件内部添加了一个子容器Container,并设置其颜色为黑色,透明度为0.5,用于给背景图像添加一个半透明的蒙层。
最后,我们在子容器Container中添加了一个居中显示的文本“Hello World”。
通过以上代码,我们可以看到BackdropFilter成功应用了滤镜效果,给背景图像添加了模糊和颜色过滤的效果。