flutter - 如何更新另一个自定义 Flutter 小部件中存在的图像路径?
问题描述
我试图使用 Flutter 的桌面实现创建一个应用程序,该应用程序将使用 JSON 文件读取图像的路径,将路径传递给自定义小部件,小部件将在容器内显示带有悬停动画的图像。
声明的变量如下:
String characterName = 'Elise';
var skill_icon_path =
characterData[characterName]['ability 1']['icon path'];
然后使用以下代码将其传递到自定义小部件:
ButtonAnimation(
imagePath: skill_icon_path,
height: 70,
width: 70,
),
ButtonAnimation 小部件的代码如下:
import 'package:flutter/material.dart';
class ButtonAnimation extends StatefulWidget {
ButtonAnimation({Key key, this.imagePath, this.height, this.width})
: super(key: key);
final String imagePath;
final double height;
final double width;
@override
buttonAnimationState createState() =>
buttonAnimationState(imagePath, height, width);
}
class buttonAnimationState extends State<ButtonAnimation> {
final String imageSrc;
buttonAnimationState(this.imageSrc, this.height, this.width);
final double height;
final double width;
bool isHover = false;
bool animation_ended = false;
@override
Widget build(BuildContext context) {
return Container(
height: height,
width: width,
child: InkWell(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 1, color: isHover ? Colors.red : Colors.grey)),
child: Stack(
children: [
AnimatedContainer(
duration: Duration(milliseconds: 200),
padding: EdgeInsets.all(10),
child: Image(
image: AssetImage(imageSrc),
color: Colors.grey,
),
),
AnimatedContainer(
padding: EdgeInsets.all(10),
onEnd: () {
setState(() {
animation_ended = true;
});
},
duration: Duration(milliseconds: 50),
color: Colors.red,
child: Visibility(
visible: animation_ended,
child: Image(
image: AssetImage(imageSrc),
color: Colors.white,
),
),
height: double.infinity,
width: isHover ? width : 0,
)
],
),
),
onTap: () {},
onHover: (value) {
setState(() {
isHover = !isHover;
print(imageSrc);
});
},
),
);
}
}
但是,当characterName
变量更改时,主类中图像的 JSON 路径会更改,但不会更新 ButtonAnimation 小部件中的路径。也就是说,如果将characterName
设置characterName = "Jhon"
为主类中图像的路径从 更改为assets/media/skill_icons/elise/elise_ability.png
,assets/media/skill_icons/jhon/jhon_ability.png
但给 ButtonAnimation 的路径仍为assets/media/skill_icons/elise/elise_ability.png
。因此,我在考虑是否可以在主类中更新路径时更新 ButtonAnimation 中图像的路径。
解决方案
推荐阅读
- ruby - 为什么一个明显的条件没有满足?
- java - 错误代码:1822。添加外键约束失败。引用表“地址”中的约束“employee_ibfk_1”缺少索引
- angular - 以 NgForm 作为参数的 typescript 方法的 Karma 单元测试
- reactjs - 过滤器在几个词后改变TextInput的焦点,不允许输入长词
- ios - 如何知道已安装的应用程序是通过 TestFlight 或 AppStore 安装的?
- c - 这个汇编 x86 代码的反编译 (C) 代码结构是什么?
- java - 如何解决com.itextpdf.text.ExceptionConverter:-1
- ios - 在访问我的核心数据模型之前,如何确保所有网络调用都已进行?
- python - 如果发生冲突,在元数据(日期)的帮助下合并两个 XML 文件
- android - 如何设置onItemclick。它显示c的错误
?