|
| 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 '../web_socket_client.dart'; |
| 9 | +import 'create_control.dart'; |
| 10 | + |
| 11 | +enum LabelPosition { right, left } |
| 12 | + |
| 13 | +class CheckboxControl extends StatefulWidget { |
| 14 | + final Control? parent; |
| 15 | + final Control control; |
| 16 | + final bool parentDisabled; |
| 17 | + |
| 18 | + const CheckboxControl( |
| 19 | + {Key? key, |
| 20 | + this.parent, |
| 21 | + required this.control, |
| 22 | + required this.parentDisabled}) |
| 23 | + : super(key: key); |
| 24 | + |
| 25 | + @override |
| 26 | + State<CheckboxControl> createState() => _CheckboxControlState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _CheckboxControlState extends State<CheckboxControl> { |
| 30 | + bool? _value; |
| 31 | + |
| 32 | + @override |
| 33 | + void initState() { |
| 34 | + super.initState(); |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + void dispose() { |
| 39 | + super.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + debugPrint("Checkbox build: ${widget.control.id}"); |
| 45 | + |
| 46 | + String label = widget.control.attrString("label", "")!; |
| 47 | + LabelPosition labelPosition = LabelPosition.values.firstWhere( |
| 48 | + (p) => |
| 49 | + p.name.toLowerCase() == |
| 50 | + widget.control.attrString("labelPosition", "")!.toLowerCase(), |
| 51 | + orElse: () => LabelPosition.right); |
| 52 | + bool tristate = widget.control.attrBool("tristate", false)!; |
| 53 | + bool disabled = widget.control.isDisabled || widget.parentDisabled; |
| 54 | + |
| 55 | + return StoreConnector<AppState, Function>( |
| 56 | + distinct: true, |
| 57 | + converter: (store) => store.dispatch, |
| 58 | + builder: (context, dispatch) { |
| 59 | + debugPrint("Checkbox StoreConnector build: ${widget.control.id}"); |
| 60 | + |
| 61 | + bool? value = |
| 62 | + widget.control.attrBool("value", tristate ? null : false); |
| 63 | + if (_value != value) { |
| 64 | + _value = value; |
| 65 | + } |
| 66 | + |
| 67 | + onChange(bool? value) { |
| 68 | + debugPrint(value?.toString()); |
| 69 | + setState(() { |
| 70 | + _value = value; |
| 71 | + }); |
| 72 | + List<Map<String, String>> props = [ |
| 73 | + { |
| 74 | + "i": widget.control.id, |
| 75 | + "value": value != null ? value.toString() : "" |
| 76 | + } |
| 77 | + ]; |
| 78 | + dispatch(UpdateControlPropsAction( |
| 79 | + UpdateControlPropsPayload(props: props))); |
| 80 | + ws.updateControlProps(props: props); |
| 81 | + } |
| 82 | + |
| 83 | + var checkbox = Checkbox( |
| 84 | + value: _value, |
| 85 | + tristate: tristate, |
| 86 | + onChanged: !disabled |
| 87 | + ? (bool? value) { |
| 88 | + onChange(value); |
| 89 | + } |
| 90 | + : null); |
| 91 | + |
| 92 | + Widget result = checkbox; |
| 93 | + if (label != "") { |
| 94 | + var labelWidget = disabled |
| 95 | + ? Text(label, |
| 96 | + style: TextStyle(color: Theme.of(context).disabledColor)) |
| 97 | + : MouseRegion( |
| 98 | + cursor: SystemMouseCursors.click, child: Text(label)); |
| 99 | + result = GestureDetector( |
| 100 | + onTap: !disabled |
| 101 | + ? () { |
| 102 | + bool? newValue; |
| 103 | + if (!tristate) { |
| 104 | + newValue = !_value!; |
| 105 | + } else if (tristate && _value == null) { |
| 106 | + newValue = false; |
| 107 | + } else if (tristate && _value == false) { |
| 108 | + newValue = true; |
| 109 | + } |
| 110 | + onChange(newValue); |
| 111 | + } |
| 112 | + : null, |
| 113 | + child: labelPosition == LabelPosition.right |
| 114 | + ? Row(children: [checkbox, labelWidget]) |
| 115 | + : Row(children: [labelWidget, checkbox])); |
| 116 | + } |
| 117 | + |
| 118 | + return constrainedControl(result, widget.parent, widget.control); |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
0 commit comments