flutter - Flutter - blurRadius 奇怪的行为
问题描述
所以我有一个旋转容器
这是工作代码:
@override
void initState() {
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 32),
)..repeat();
super.initState();
}
....
@override
Widget build(BuildContext context) {
double screenHeight = MediaQuery.of(context).size.height;
double screenWidth = MediaQuery.of(context).size.width;
return Container(
child: Column(
children: <Widget>[
SizedBox(
height: screenHeight,
child: Stack(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
],
),
Divider(),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Center(
child: AnimatedBuilder(
animation: _controller,
builder: (_, child) {
return Transform(
alignment: FractionalOffset.center,
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateY(0.5 * -math.pi)
..rotateZ(-0.1 * math.pi)
..rotateY((_controller.value - 0.6) * -math.pi)
..rotateX(0.5 * math.pi),
child: child,
);
},
child: Container(
//color: Colors.yellow.withOpacity(0.5),
height: 300,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
boxShadow: [
BoxShadow(
color: Colors.yellow.withOpacity(0.5),
spreadRadius: 40,
//UNCOMMENTING THIS LINE CAUSES STRANGE BEHAVIOUR
//blurRadius: 50,
//offset: const Offset(0, 0),
),
],
),
),
//),
),
),
SizedBox(
height: 170,
)
],
),
],
),
),
],
),
);
}
但是,当我取消注释 blurRadius 代码行时,它的行为很奇怪。看看容器在垂直于屏幕时是如何变形的。
我希望它始终模糊。这是一件很奇怪的事情。
解决方案
Flutter 绘制阴影的成本非常高,因此像这样对它们进行动画处理是不好的,因为 Flutter 必须不断地重新绘制阴影。由于阴影也会导致您的变换问题,您应该尝试在 GIMP/Adobe 中制作您想要的阴影或将其截屏,然后将其添加到您的项目中。
您的项目目录中应该有一个 assets 或 images 文件夹,即YOUR_FLUTTER_PROJECT/images
. 把阴影图像放在那里。
然后,在pubspec.yaml
文件中,您的资产应该有一个字段。在那里添加影子文件的路径,例如:
assets:
- images/a_dot_burr.jpeg
- images/a_dot_ham.jpeg
- images/YOUR_SHADOW_FILE
跑flutter pub get
在您的代码中,将您的替换Container
为:
Image.asset('images/YOUR_SHADOW_FILE', height: 300, width: 200),
我还没有测试它,但我认为它应该仍然有效。
推荐阅读
- javascript - 使用 textInput 和函数组件在本机反应中更改数组对象值
- algorithm - 数据结构按树中的位置排序,支持孔?
- typescript - Babel-node 无法处理 Typescript 文件中导入的 flowtype 文件?
- spring - Spring MVC - 无法在控制器中加载图像
- javascript - 按钮 onClick="javascript:calleoChat" 在 Html 上运行良好,但在脚本中不行
- nginx - Nginx 反向代理将上游端口附加到我的浏览器 url
- apache-spark - 如何在 spark 中编写编码指定的 csv 文件?
- batch-file - bat脚本中的嵌套循环
- javascript - RecatNative - 记录时显示对象键,但在应用程序中显示时,它显示未定义?
- vue.js - Vue 3 单元测试 - 找不到模块