|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_redux/flutter_redux.dart'; |
| 3 | + |
| 4 | +import '../actions.dart'; |
| 5 | +import '../models/app_state.dart'; |
| 6 | +import '../models/control.dart'; |
| 7 | +import '../protocol/update_control_props_payload.dart'; |
| 8 | +import '../utils/alignment.dart'; |
| 9 | +import '../utils/edge_insets.dart'; |
| 10 | +import '../web_socket_client.dart'; |
| 11 | +import 'create_control.dart'; |
| 12 | +import 'error.dart'; |
| 13 | + |
| 14 | +class AlertDialogControl extends StatefulWidget { |
| 15 | + final Control? parent; |
| 16 | + final Control control; |
| 17 | + final List<Control> children; |
| 18 | + final bool parentDisabled; |
| 19 | + |
| 20 | + const AlertDialogControl( |
| 21 | + {Key? key, |
| 22 | + this.parent, |
| 23 | + required this.control, |
| 24 | + required this.children, |
| 25 | + required this.parentDisabled}) |
| 26 | + : super(key: key); |
| 27 | + |
| 28 | + @override |
| 29 | + State<AlertDialogControl> createState() => _AlertDialogControlState(); |
| 30 | +} |
| 31 | + |
| 32 | +class _AlertDialogControlState extends State<AlertDialogControl> { |
| 33 | + bool _open = false; |
| 34 | + |
| 35 | + @override |
| 36 | + void initState() { |
| 37 | + super.initState(); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void dispose() { |
| 42 | + super.dispose(); |
| 43 | + } |
| 44 | + |
| 45 | + Widget _createAlertDialog() { |
| 46 | + bool disabled = widget.control.isDisabled || widget.parentDisabled; |
| 47 | + var titleCtrls = widget.children.where((c) => c.name == "title"); |
| 48 | + var contentCtrls = widget.children.where((c) => c.name == "content"); |
| 49 | + var actionCtrls = widget.children.where((c) => c.name == "action"); |
| 50 | + final actionsAlignment = parseMainAxisAlignment( |
| 51 | + widget.control, "actionsAlignment", MainAxisAlignment.start); |
| 52 | + if (titleCtrls.isEmpty && contentCtrls.isEmpty && actionCtrls.isEmpty) { |
| 53 | + return const ErrorControl("AlertDialog does not have any content."); |
| 54 | + } |
| 55 | + |
| 56 | + return AlertDialog( |
| 57 | + title: titleCtrls.isNotEmpty |
| 58 | + ? createControl(widget.control, titleCtrls.first.id, disabled) |
| 59 | + : null, |
| 60 | + titlePadding: parseEdgeInsets(widget.control, "titlePadding"), |
| 61 | + content: contentCtrls.isNotEmpty |
| 62 | + ? createControl(widget.control, contentCtrls.first.id, disabled) |
| 63 | + : null, |
| 64 | + contentPadding: parseEdgeInsets(widget.control, "contentPadding") ?? |
| 65 | + const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0), |
| 66 | + actions: actionCtrls |
| 67 | + .map((c) => createControl(widget.control, c.id, disabled)) |
| 68 | + .toList(), |
| 69 | + actionsPadding: |
| 70 | + parseEdgeInsets(widget.control, "actionsPadding") ?? EdgeInsets.zero, |
| 71 | + actionsAlignment: actionsAlignment, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + Widget build(BuildContext context) { |
| 77 | + debugPrint("AlertDialog build: ${widget.control.id}"); |
| 78 | + |
| 79 | + return StoreConnector<AppState, Function>( |
| 80 | + distinct: true, |
| 81 | + converter: (store) => store.dispatch, |
| 82 | + builder: (context, dispatch) { |
| 83 | + debugPrint("AlertDialog StoreConnector build: ${widget.control.id}"); |
| 84 | + |
| 85 | + var open = widget.control.attrBool("open", false)!; |
| 86 | + var modal = widget.control.attrBool("modal", false)!; |
| 87 | + // var removeCurrentSnackbar = |
| 88 | + // widget.control.attrBool("removeCurrentSnackBar", false)!; |
| 89 | + |
| 90 | + debugPrint("Current open state: $_open"); |
| 91 | + debugPrint("New open state: $open"); |
| 92 | + |
| 93 | + if (open && (open != _open)) { |
| 94 | + var dialog = _createAlertDialog(); |
| 95 | + if (dialog is ErrorControl) { |
| 96 | + return dialog; |
| 97 | + } |
| 98 | + |
| 99 | + WidgetsBinding.instance!.addPostFrameCallback((_) { |
| 100 | + // if (removeCurrentSnackbar) { |
| 101 | + // ScaffoldMessenger.of(context).removeCurrentSnackBar(); |
| 102 | + // } |
| 103 | + |
| 104 | + showDialog( |
| 105 | + barrierDismissible: !modal, |
| 106 | + context: context, |
| 107 | + builder: (context) => _createAlertDialog()).then((value) { |
| 108 | + debugPrint("Dialog dismissed"); |
| 109 | + _open = false; |
| 110 | + List<Map<String, String>> props = [ |
| 111 | + {"i": widget.control.id, "open": "false"} |
| 112 | + ]; |
| 113 | + dispatch(UpdateControlPropsAction( |
| 114 | + UpdateControlPropsPayload(props: props))); |
| 115 | + ws.updateControlProps(props: props); |
| 116 | + }); |
| 117 | + }); |
| 118 | + } else if (open != _open && _open) { |
| 119 | + Navigator.pop(context); |
| 120 | + } |
| 121 | + |
| 122 | + _open = open; |
| 123 | + |
| 124 | + return const SizedBox.shrink(); |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments