|
| 1 | +import 'package:flet_view/utils/icons.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_redux/flutter_redux.dart'; |
| 4 | + |
| 5 | +import '../models/app_state.dart'; |
| 6 | +import '../models/control.dart'; |
| 7 | +import '../models/controls_view_model.dart'; |
| 8 | +import '../web_socket_client.dart'; |
| 9 | +import 'create_control.dart'; |
| 10 | + |
| 11 | +class PopupMenuButtonControl extends StatefulWidget { |
| 12 | + final Control? parent; |
| 13 | + final Control control; |
| 14 | + final bool parentDisabled; |
| 15 | + final List<Control> children; |
| 16 | + |
| 17 | + const PopupMenuButtonControl( |
| 18 | + {Key? key, |
| 19 | + this.parent, |
| 20 | + required this.control, |
| 21 | + required this.children, |
| 22 | + required this.parentDisabled}) |
| 23 | + : super(key: key); |
| 24 | + |
| 25 | + @override |
| 26 | + State<PopupMenuButtonControl> createState() => _PopupMenuButtonControlState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _PopupMenuButtonControlState extends State<PopupMenuButtonControl> { |
| 30 | + @override |
| 31 | + void initState() { |
| 32 | + super.initState(); |
| 33 | + } |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + debugPrint("PopupMenuButton build: ${widget.control.id}"); |
| 38 | + |
| 39 | + var icon = getMaterialIcon(widget.control.attrString("icon", "")!); |
| 40 | + bool disabled = widget.control.isDisabled || widget.parentDisabled; |
| 41 | + |
| 42 | + var popupButton = StoreConnector<AppState, ControlsViewModel>( |
| 43 | + distinct: true, |
| 44 | + converter: (store) => ControlsViewModel.fromStore( |
| 45 | + store, widget.children.map((c) => c.id)), |
| 46 | + builder: (content, viewModel) { |
| 47 | + return PopupMenuButton<String>( |
| 48 | + enabled: !disabled, |
| 49 | + icon: icon != null ? Icon(icon) : null, |
| 50 | + shape: Theme.of(context).useMaterial3 |
| 51 | + ? RoundedRectangleBorder( |
| 52 | + borderRadius: BorderRadius.circular(10)) |
| 53 | + : null, |
| 54 | + onCanceled: () { |
| 55 | + ws.pageEventFromWeb( |
| 56 | + eventTarget: widget.control.id, |
| 57 | + eventName: "cancelled", |
| 58 | + eventData: ""); |
| 59 | + }, |
| 60 | + onSelected: (itemId) { |
| 61 | + ws.pageEventFromWeb( |
| 62 | + eventTarget: itemId, eventName: "click", eventData: ""); |
| 63 | + }, |
| 64 | + itemBuilder: (BuildContext context) => |
| 65 | + viewModel.controlViews.map((cv) { |
| 66 | + var itemIcon = |
| 67 | + getMaterialIcon(cv.control.attrString("icon", "")!); |
| 68 | + var text = cv.control.attrString("text", "")!; |
| 69 | + var checked = cv.control.attrBool("checked"); |
| 70 | + |
| 71 | + var contentCtrls = |
| 72 | + cv.children.where((c) => c.name == "content"); |
| 73 | + |
| 74 | + Widget? child; |
| 75 | + if (contentCtrls.isNotEmpty) { |
| 76 | + // custom content |
| 77 | + child = createControl(cv.control, contentCtrls.first.id, |
| 78 | + widget.parentDisabled); |
| 79 | + } else if (itemIcon != null && text != "") { |
| 80 | + // icon and text |
| 81 | + child = Row(children: [ |
| 82 | + Icon(itemIcon), |
| 83 | + const SizedBox(width: 8), |
| 84 | + Text(text) |
| 85 | + ]); |
| 86 | + } else if (text != "") { |
| 87 | + child = Text(text); |
| 88 | + } |
| 89 | + |
| 90 | + var item = checked != null |
| 91 | + ? CheckedPopupMenuItem<String>( |
| 92 | + value: cv.control.id, |
| 93 | + checked: checked, |
| 94 | + child: child, |
| 95 | + ) |
| 96 | + : PopupMenuItem<String>( |
| 97 | + value: cv.control.id, child: child); |
| 98 | + |
| 99 | + return child != null |
| 100 | + ? item |
| 101 | + : const PopupMenuDivider() as PopupMenuEntry<String>; |
| 102 | + }).toList()); |
| 103 | + }); |
| 104 | + |
| 105 | + return constrainedControl(popupButton, widget.parent, widget.control); |
| 106 | + } |
| 107 | +} |
0 commit comments