flutter_desktop: refactor GetX in popup menu

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-09-07 18:39:45 -07:00
parent 9b694cbac0
commit 41a5d53de6
2 changed files with 66 additions and 74 deletions

View File

@ -8,7 +8,7 @@ import './material_mod_popup_menu.dart' as mod_menu;
// https://stackoverflow.com/questions/68318314/flutter-popup-menu-inside-popup-menu // https://stackoverflow.com/questions/68318314/flutter-popup-menu-inside-popup-menu
class PopupMenuChildrenItem<T> extends mod_menu.PopupMenuEntry<T> { class PopupMenuChildrenItem<T> extends mod_menu.PopupMenuEntry<T> {
const PopupMenuChildrenItem({ PopupMenuChildrenItem({
key, key,
this.height = kMinInteractiveDimension, this.height = kMinInteractiveDimension,
this.padding, this.padding,
@ -43,6 +43,16 @@ class PopupMenuChildrenItem<T> extends mod_menu.PopupMenuEntry<T> {
class MyPopupMenuItemState<T, W extends PopupMenuChildrenItem<T>> class MyPopupMenuItemState<T, W extends PopupMenuChildrenItem<T>>
extends State<W> { extends State<W> {
RxBool enabled = true.obs;
@override
void initState() {
super.initState();
if (widget.enabled != null) {
enabled.value = widget.enabled!.value;
}
}
@protected @protected
void handleTap(T value) { void handleTap(T value) {
widget.onTap?.call(); widget.onTap?.call();
@ -56,27 +66,25 @@ class MyPopupMenuItemState<T, W extends PopupMenuChildrenItem<T>>
TextStyle style = widget.textStyle ?? TextStyle style = widget.textStyle ??
popupMenuTheme.textStyle ?? popupMenuTheme.textStyle ??
theme.textTheme.subtitle1!; theme.textTheme.subtitle1!;
return Obx(() { return Obx(() => mod_menu.PopupMenuButton<T>(
return mod_menu.PopupMenuButton<T>( enabled: enabled.value,
enabled: widget.enabled != null ? widget.enabled!.value : true, position: widget.position,
position: widget.position, offset: widget.offset,
offset: widget.offset, onSelected: handleTap,
onSelected: handleTap, itemBuilder: widget.itemBuilder,
itemBuilder: widget.itemBuilder, padding: EdgeInsets.zero,
padding: EdgeInsets.zero, child: AnimatedDefaultTextStyle(
child: AnimatedDefaultTextStyle( style: style,
style: style, duration: kThemeChangeDuration,
duration: kThemeChangeDuration, child: Container(
child: Container( alignment: AlignmentDirectional.centerStart,
alignment: AlignmentDirectional.centerStart, constraints: BoxConstraints(minHeight: widget.height),
constraints: BoxConstraints(minHeight: widget.height), padding:
padding: widget.padding ?? const EdgeInsets.symmetric(horizontal: 16),
widget.padding ?? const EdgeInsets.symmetric(horizontal: 16), child: widget.child,
child: widget.child, ),
), ),
), ));
);
});
} }
} }
@ -342,7 +350,7 @@ typedef SwitchSetter = Future<void> Function(bool);
abstract class MenuEntrySwitchBase<T> extends MenuEntryBase<T> { abstract class MenuEntrySwitchBase<T> extends MenuEntryBase<T> {
final String text; final String text;
final Rx<TextStyle>? textStyle; Rx<TextStyle>? textStyle;
MenuEntrySwitchBase({ MenuEntrySwitchBase({
required this.text, required this.text,
@ -357,6 +365,11 @@ abstract class MenuEntrySwitchBase<T> extends MenuEntryBase<T> {
@override @override
List<mod_menu.PopupMenuEntry<T>> build( List<mod_menu.PopupMenuEntry<T>> build(
BuildContext context, MenuConfig conf) { BuildContext context, MenuConfig conf) {
textStyle ??= const TextStyle(
color: Colors.black,
fontSize: MenuConfig.fontSize,
fontWeight: FontWeight.normal)
.obs;
return [ return [
mod_menu.PopupMenuItem( mod_menu.PopupMenuItem(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
@ -366,23 +379,10 @@ abstract class MenuEntrySwitchBase<T> extends MenuEntryBase<T> {
alignment: AlignmentDirectional.centerStart, alignment: AlignmentDirectional.centerStart,
height: conf.height, height: conf.height,
child: Row(children: [ child: Row(children: [
() { Obx(() => Text(
if (textStyle != null) {
final style = textStyle!;
return Obx(() => Text(
text,
style: style.value,
));
} else {
return Text(
text, text,
style: const TextStyle( style: textStyle!.value,
color: Colors.black, )),
fontSize: MenuConfig.fontSize,
fontWeight: FontWeight.normal),
);
}
}(),
Expanded( Expanded(
child: Align( child: Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
@ -485,6 +485,7 @@ class MenuEntrySubMenu<T> extends MenuEntryBase<T> {
@override @override
List<mod_menu.PopupMenuEntry<T>> build( List<mod_menu.PopupMenuEntry<T>> build(
BuildContext context, MenuConfig conf) { BuildContext context, MenuConfig conf) {
super.enabled ??= true.obs;
return [ return [
PopupMenuChildrenItem( PopupMenuChildrenItem(
enabled: super.enabled, enabled: super.enabled,
@ -500,9 +501,7 @@ class MenuEntrySubMenu<T> extends MenuEntryBase<T> {
Obx(() => Text( Obx(() => Text(
text, text,
style: TextStyle( style: TextStyle(
color: (super.enabled != null ? super.enabled!.value : true) color: super.enabled!.value ? Colors.black : Colors.grey,
? Colors.black
: Colors.grey,
fontSize: MenuConfig.fontSize, fontSize: MenuConfig.fontSize,
fontWeight: FontWeight.normal), fontWeight: FontWeight.normal),
)), )),
@ -511,9 +510,7 @@ class MenuEntrySubMenu<T> extends MenuEntryBase<T> {
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: Obx(() => Icon( child: Obx(() => Icon(
Icons.keyboard_arrow_right, Icons.keyboard_arrow_right,
color: (super.enabled != null ? super.enabled!.value : true) color: super.enabled!.value ? conf.commonColor : Colors.grey,
? conf.commonColor
: Colors.grey,
)), )),
)) ))
]), ]),
@ -537,35 +534,31 @@ class MenuEntryButton<T> extends MenuEntryBase<T> {
); );
Widget _buildChild(BuildContext context, MenuConfig conf) { Widget _buildChild(BuildContext context, MenuConfig conf) {
return Obx(() { const enabledStyle = TextStyle(
bool enabled = true; color: Colors.black,
if (super.enabled != null) { fontSize: MenuConfig.fontSize,
enabled = super.enabled!.value; fontWeight: FontWeight.normal);
} const disabledStyle = TextStyle(
const enabledStyle = TextStyle( color: Colors.grey,
color: Colors.black, fontSize: MenuConfig.fontSize,
fontSize: MenuConfig.fontSize, fontWeight: FontWeight.normal);
fontWeight: FontWeight.normal); super.enabled ??= true.obs;
const disabledStyle = TextStyle( return Obx(() => TextButton(
color: Colors.grey, onPressed: super.enabled!.value
fontSize: MenuConfig.fontSize, ? () {
fontWeight: FontWeight.normal); if (super.dismissOnClicked && Navigator.canPop(context)) {
return TextButton( Navigator.pop(context);
onPressed: enabled }
? () { proc();
if (super.dismissOnClicked && Navigator.canPop(context)) {
Navigator.pop(context);
} }
proc(); : null,
} child: Container(
: null, alignment: AlignmentDirectional.centerStart,
child: Container( constraints: BoxConstraints(minHeight: conf.height),
alignment: AlignmentDirectional.centerStart, child: childBuilder(
constraints: BoxConstraints(minHeight: conf.height), super.enabled!.value ? enabledStyle : disabledStyle),
child: childBuilder(enabled ? enabledStyle : disabledStyle), ),
), ));
);
});
} }
@override @override
@ -573,7 +566,6 @@ class MenuEntryButton<T> extends MenuEntryBase<T> {
BuildContext context, MenuConfig conf) { BuildContext context, MenuConfig conf) {
return [ return [
mod_menu.PopupMenuItem( mod_menu.PopupMenuItem(
enabled: super.enabled != null ? super.enabled!.value : true,
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
height: conf.height, height: conf.height,
child: _buildChild(context, conf), child: _buildChild(context, conf),

View File

@ -611,7 +611,7 @@ class _RemoteMenubarState extends State<RemoteMenubar> {
MenuEntryRadioOption(text: translate('Map mode'), value: 'map'), MenuEntryRadioOption(text: translate('Map mode'), value: 'map'),
], ],
curOptionGetter: () async { curOptionGetter: () async {
return await bind.sessionGetKeyboardName(id: widget.id) ?? 'legacy'; return await bind.sessionGetKeyboardName(id: widget.id);
}, },
optionSetter: (String oldValue, String newValue) async { optionSetter: (String oldValue, String newValue) async {
await bind.sessionSetKeyboardMode( await bind.sessionSetKeyboardMode(