Skip to content

Commit

Permalink
State on the control level
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Nov 5, 2023
1 parent 03b9c9d commit 2b3b6ef
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 50 deletions.
20 changes: 5 additions & 15 deletions package/lib/src/controls/alert_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,21 +33,15 @@ class AlertDialogControl extends StatefulWidget {
}

class _AlertDialogControlState extends State<AlertDialogControl> {
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();
}

Expand Down Expand Up @@ -91,11 +84,9 @@ class _AlertDialogControlState extends State<AlertDialogControl> {
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<AppState, Function>(
distinct: true,
Expand All @@ -115,18 +106,17 @@ class _AlertDialogControlState extends State<AlertDialogControl> {
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<Map<String, String>> props = [
Expand Down
2 changes: 1 addition & 1 deletion package/lib/src/controls/create_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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}) {
Expand Down
18 changes: 4 additions & 14 deletions package/lib/src/controls/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -29,32 +28,23 @@ class DatePickerControl extends StatefulWidget {
}

class _DatePickerControlState extends State<DatePickerControl> {
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();
}

@override
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");
Expand Down Expand Up @@ -107,7 +97,7 @@ class _DatePickerControlState extends State<DatePickerControl> {
stringValue = dateValue.toIso8601String();
eventName = "change";
}
_globalState?.set(widget.control.id, "open", false, hashCode);
widget.control.state["open"] = false;
List<Map<String, String>> props = [
{"i": widget.control.id, "value": stringValue, "open": "false"}
];
Expand Down Expand Up @@ -155,7 +145,7 @@ class _DatePickerControlState extends State<DatePickerControl> {
}

if (open && (open != lastOpen)) {
_globalState?.set(widget.control.id, "open", open, hashCode);
widget.control.state["open"] = open;

WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog<DateTime>(
Expand Down
10 changes: 5 additions & 5 deletions package/lib/src/models/app_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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,
Expand All @@ -65,8 +65,8 @@ class AppState extends Equatable {
pid: "",
type: "page",
name: "",
childIds: [],
attrs: {})
childIds: const [],
attrs: const {})
});

AppState copyWith(
Expand Down
37 changes: 22 additions & 15 deletions package/lib/src/models/control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class Control extends Equatable {
final String? name;
final List<String> childIds;
final Map<String, String> attrs;
final Map<String, dynamic> state = {};

const Control(
Control(
{required this.id,
required this.pid,
required this.type,
Expand Down Expand Up @@ -46,7 +47,7 @@ class Control extends Equatable {
bool get isNonVisual {
return [
//"alertdialog",
"audio",
//"audio",
"banner",
//"bottomsheet",
"clipboard",
Expand Down Expand Up @@ -95,19 +96,25 @@ class Control extends Equatable {
}

Control copyWith(
{String? id,
String? pid,
String? type,
String? name,
List<String>? childIds,
Map<String, String>? 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<String>? childIds,
Map<String, String>? attrs,
Map<String, dynamic>? 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<Object?> get props => [id, pid, type, name, childIds, attrs];
Expand Down

0 comments on commit 2b3b6ef

Please sign in to comment.