我在我的flutter项目中有2个ImageAssets,我想根据动作在它们之间切换,我用肘和状态。在转到另一个小部件然后返回后,图像会发生变化如何刷新小部件,或者在没有状态小部件的情况下如何刷新小部件
这是我的图片我想在灰色和基本色之间切换
indicator:Image.asset(cubit.planeState ? ImageAssets.plane : ImageAssets.planeGrey )),
这个是OnButtonPressed
btnOkOnPress: () {
context.read<RehlatechCubit>().onPlaneStateChanged();
},
假设您已经使用cubit设置了页面。
- 从方法 修改状态(而不是比特)
- 发出新的状态
- 确保小部件被封装在BlocBuilder/BlocSelector/BlocConsumer
推出伪代码
// 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,
),
),
)
)
}
)
);