From 2b3b6ef4d8773dc69d40db97cdc573cd57c71c58 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Sun, 5 Nov 2023 10:44:03 -0800 Subject: [PATCH] State on the control level --- package/lib/src/controls/alert_dialog.dart | 20 +++-------- package/lib/src/controls/create_control.dart | 2 +- package/lib/src/controls/date_picker.dart | 18 +++------- package/lib/src/models/app_state.dart | 10 +++--- package/lib/src/models/control.dart | 37 ++++++++++++-------- 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/package/lib/src/controls/alert_dialog.dart b/package/lib/src/controls/alert_dialog.dart index b9337acde..d5bea8e7a 100644 --- a/package/lib/src/controls/alert_dialog.dart +++ b/package/lib/src/controls/alert_dialog.dart @@ -8,7 +8,6 @@ import '../models/control.dart'; import '../protocol/update_control_props_payload.dart'; import '../utils/alignment.dart'; import '../utils/borders.dart'; -import '../utils/control_global_state.dart'; import '../utils/edge_insets.dart'; import 'create_control.dart'; import 'error.dart'; @@ -34,21 +33,15 @@ class AlertDialogControl extends StatefulWidget { } class _AlertDialogControlState extends State { - String? _id; - ControlsGlobalState? _globalState; - @override void initState() { - super.initState(); debugPrint("AlertDialog initState() ($hashCode)"); + super.initState(); } @override void dispose() { debugPrint("AlertDialog dispose() ($hashCode)"); - if (_id != null) { - _globalState?.remove(_id!, "open", hashCode); - } super.dispose(); } @@ -91,11 +84,9 @@ class _AlertDialogControlState extends State { Widget build(BuildContext context) { debugPrint("AlertDialog build ($hashCode): ${widget.control.id}"); - _id = widget.control.id; - _globalState = FletAppServices.of(context).globalState; var server = FletAppServices.of(context).server; - bool lastOpen = _globalState?.get(widget.control.id, "open") ?? false; + bool lastOpen = widget.control.state["open"] ?? false; return StoreConnector( distinct: true, @@ -115,18 +106,17 @@ class _AlertDialogControlState extends State { return dialog; } - _globalState?.set(widget.control.id, "open", open, hashCode); + widget.control.state["open"] = open; WidgetsBinding.instance.addPostFrameCallback((_) { showDialog( barrierDismissible: !modal, context: context, builder: (context) => _createAlertDialog()).then((value) { - lastOpen = - _globalState?.get(widget.control.id, "open") ?? false; + lastOpen = widget.control.state["open"] ?? false; debugPrint("Dialog should be dismissed ($hashCode): $lastOpen"); bool shouldDismiss = lastOpen; - _globalState?.set(widget.control.id, "open", false, hashCode); + widget.control.state["open"] = false; if (shouldDismiss) { List> props = [ diff --git a/package/lib/src/controls/create_control.dart b/package/lib/src/controls/create_control.dart index 7c11c0acc..f6fdc6c9c 100644 --- a/package/lib/src/controls/create_control.dart +++ b/package/lib/src/controls/create_control.dart @@ -57,6 +57,7 @@ import 'progress_bar.dart'; import 'progress_ring.dart'; import 'radio.dart'; import 'radio_group.dart'; +import 'range_slider.dart'; import 'responsive_row.dart'; import 'row.dart'; import 'safe_area.dart'; @@ -75,7 +76,6 @@ import 'tooltip.dart'; import 'transparent_pointer.dart'; import 'vertical_divider.dart'; import 'window_drag_area.dart'; -import 'range_slider.dart'; Widget createControl(Control? parent, String id, bool parentDisabled, {Widget? nextChild}) { diff --git a/package/lib/src/controls/date_picker.dart b/package/lib/src/controls/date_picker.dart index 9dbff164b..732c36d1d 100644 --- a/package/lib/src/controls/date_picker.dart +++ b/package/lib/src/controls/date_picker.dart @@ -4,7 +4,6 @@ import '../actions.dart'; import '../flet_app_services.dart'; import '../models/control.dart'; import '../protocol/update_control_props_payload.dart'; -import '../utils/control_global_state.dart'; import '../utils/icons.dart'; import 'form_field.dart'; @@ -29,21 +28,15 @@ class DatePickerControl extends StatefulWidget { } class _DatePickerControlState extends State { - String? _id; - ControlsGlobalState? _globalState; - @override void initState() { - super.initState(); debugPrint("DatePicker initState() ($hashCode)"); + super.initState(); } @override void dispose() { debugPrint("DatePicker dispose() ($hashCode)"); - if (_id != null) { - _globalState?.remove(_id!, "open", hashCode); - } super.dispose(); } @@ -51,10 +44,7 @@ class _DatePickerControlState extends State { Widget build(BuildContext context) { debugPrint("DatePicker build: ${widget.control.id}"); - _id = widget.control.id; - _globalState = FletAppServices.of(context).globalState; - - bool lastOpen = _globalState?.get(widget.control.id, "open") ?? false; + bool lastOpen = widget.control.state["open"] ?? false; var open = widget.control.attrBool("open", false)!; DateTime? value = widget.control.attrDateTime("value"); @@ -107,7 +97,7 @@ class _DatePickerControlState extends State { stringValue = dateValue.toIso8601String(); eventName = "change"; } - _globalState?.set(widget.control.id, "open", false, hashCode); + widget.control.state["open"] = false; List> props = [ {"i": widget.control.id, "value": stringValue, "open": "false"} ]; @@ -155,7 +145,7 @@ class _DatePickerControlState extends State { } if (open && (open != lastOpen)) { - _globalState?.set(widget.control.id, "open", open, hashCode); + widget.control.state["open"] = open; WidgetsBinding.instance.addPostFrameCallback((_) { showDialog( diff --git a/package/lib/src/models/app_state.dart b/package/lib/src/models/app_state.dart index 25fa9b245..16e462382 100644 --- a/package/lib/src/models/app_state.dart +++ b/package/lib/src/models/app_state.dart @@ -39,7 +39,7 @@ class AppState extends Equatable { required this.displayBrightness, required this.controls}); - factory AppState.initial() => const AppState( + factory AppState.initial() => AppState( pageUri: null, assetsDir: "", route: "", @@ -49,8 +49,8 @@ class AppState extends Equatable { isRegistered: false, reconnectDelayMs: 0, error: "", - size: Size(0, 0), - sizeBreakpoints: { + size: const Size(0, 0), + sizeBreakpoints: const { "xs": 0, "sm": 576, "md": 768, @@ -65,8 +65,8 @@ class AppState extends Equatable { pid: "", type: "page", name: "", - childIds: [], - attrs: {}) + childIds: const [], + attrs: const {}) }); AppState copyWith( diff --git a/package/lib/src/models/control.dart b/package/lib/src/models/control.dart index 52fd2a8df..d96ac636b 100644 --- a/package/lib/src/models/control.dart +++ b/package/lib/src/models/control.dart @@ -9,8 +9,9 @@ class Control extends Equatable { final String? name; final List childIds; final Map attrs; + final Map state = {}; - const Control( + Control( {required this.id, required this.pid, required this.type, @@ -46,7 +47,7 @@ class Control extends Equatable { bool get isNonVisual { return [ //"alertdialog", - "audio", + //"audio", "banner", //"bottomsheet", "clipboard", @@ -95,19 +96,25 @@ class Control extends Equatable { } Control copyWith( - {String? id, - String? pid, - String? type, - String? name, - List? childIds, - Map? attrs}) => - Control( - id: id ?? this.id, - pid: pid ?? this.pid, - type: type ?? this.type, - name: name ?? this.name, - childIds: childIds ?? this.childIds, - attrs: attrs ?? this.attrs); + {String? id, + String? pid, + String? type, + String? name, + List? childIds, + Map? attrs, + Map? state}) { + Control c = Control( + id: id ?? this.id, + pid: pid ?? this.pid, + type: type ?? this.type, + name: name ?? this.name, + childIds: childIds ?? this.childIds, + attrs: attrs ?? this.attrs); + for (var element in this.state.entries) { + c.state[element.key] = element.value; + } + return c; + } @override List get props => [id, pid, type, name, childIds, attrs];