[英]change Image assets depend on state changed with cubit (flutter)
我的 flutter 项目中有 2 个 ImageAssets 我想根据动作在它们之间切换,我使用的是 cubit 和 states。 转到另一个小部件然后返回后图像发生变化如何刷新小部件或在没有有状态小部件的情况下执行此操作
这是我的图片,我想在灰色图片和基本图片之间切换
indicator:Image.asset(cubit.planeState ? ImageAssets.plane : ImageAssets.planeGrey )),
这是 OnButtonPressed
btnOkOnPress: () {
context.read<RehlatechCubit>().onPlaneStateChanged();
},
假设你已经用 cubit 设置了页面。
伪代码
// rehlatech_cubit.dart
onPlaneStateChanged() {
...
/// Mutate the current state with the plane state
/// Make a copy or create a new state all together
emit(state.copyWith(planeState: true));
}
确保要更新的小部件包装在 BlocBuilder 中
// widget.dart
Scaffold(
body: BlocBuilder< RehlatechCubit, RehlatechState>(
builder: (context, state) {
// The state that is to be changed
return Content(
...
child: WidgetToBeUpdated(
indicator:Image.asset(state.planeState ?
ImageAssets.plane : ImageAssets.planeGrey,
),
),
)
)
}
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.