我有一个图标按钮,点击它将固定/取消固定窗口,图标将根据固定或不固定而变化.第一次单击该按钮将触发重建事件,但后续单击不会.我对此一无所知.
我在这里创建了一个最小的示例:https://gist.github.com/TENX-S/7a811cb23b3b0ed220b75f4b2cda7fd3,用于repo:https://github.com/TENX-S/miniexample
我试着阅读https://bloclibrary.dev/的文档,但我没有得到答案.
我有一个图标按钮,点击它将固定/取消固定窗口,图标将根据固定或不固定而变化.第一次单击该按钮将触发重建事件,但后续单击不会.我对此一无所知.
我在这里创建了一个最小的示例:https://gist.github.com/TENX-S/7a811cb23b3b0ed220b75f4b2cda7fd3,用于repo:https://github.com/TENX-S/miniexample
我试着阅读https://bloclibrary.dev/的文档,但我没有得到答案.
问题是SettingsCubit的实现
下面是我对你的设置的更新版本.dart
@JsonSerializable()
class Settings {
final bool alwaysOnTop;
Settings({required this.alwaysOnTop});
const Settings._({
this.alwaysOnTop = false,
});
const Settings.initial() : this._();
const Settings.update(bool onTop): this._(alwaysOnTop: onTop);
factory Settings.fromJson(Map<String, dynamic> json) => _$SettingsFromJson(json);
Map<String, dynamic> toJson() => _$SettingsToJson(this);
}
class SettingsCubit extends HydratedCubit<Settings> {
SettingsCubit() : super(const Settings.initial());
set alwaysOnTop(bool pinned) => emit(Settings.update(pinned));
bool get alwaysOnTop => state.alwaysOnTop;
@override
Settings? fromJson(Map<String, dynamic> json) {
return Settings.fromJson(json);
}
@override
Map<String, dynamic>? toJson(Settings state) {
return state.toJson();
}
}
@override
Widget build(BuildContext context) {
final settingsCubit = context.read<SettingsCubit>();
return Scaffold(
body: Center(
child: IconButton(
icon: settingsCubit.alwaysOnTop ? const Icon(FluentIcons.pin_24_filled): const Icon(FluentIcons.pin_24_regular),
onPressed: () {
settingsCubit.alwaysOnTop = !widget.pinned;
},
),
),
);
}