|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flutter/cupertino.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_redux/flutter_redux.dart'; |
| 5 | + |
| 6 | +import '../actions.dart'; |
| 7 | +import '../flet_app_services.dart'; |
| 8 | +import '../models/app_state.dart'; |
| 9 | +import '../models/control.dart'; |
| 10 | +import '../models/controls_view_model.dart'; |
| 11 | +import '../protocol/update_control_props_payload.dart'; |
| 12 | +import '../utils/colors.dart'; |
| 13 | +import '../utils/icons.dart'; |
| 14 | +import 'create_control.dart'; |
| 15 | +import '../utils/borders.dart'; |
| 16 | + |
| 17 | +class CupertinoNavigationBarControl extends StatefulWidget { |
| 18 | + final Control? parent; |
| 19 | + final Control control; |
| 20 | + final List<Control> children; |
| 21 | + final bool parentDisabled; |
| 22 | + final dynamic dispatch; |
| 23 | + |
| 24 | + const CupertinoNavigationBarControl( |
| 25 | + {Key? key, |
| 26 | + this.parent, |
| 27 | + required this.control, |
| 28 | + required this.children, |
| 29 | + required this.parentDisabled, |
| 30 | + required this.dispatch}) |
| 31 | + : super(key: key); |
| 32 | + |
| 33 | + @override |
| 34 | + State<CupertinoNavigationBarControl> createState() => |
| 35 | + _CupertinoNavigationBarControlState(); |
| 36 | +} |
| 37 | + |
| 38 | +class _CupertinoNavigationBarControlState |
| 39 | + extends State<CupertinoNavigationBarControl> { |
| 40 | + int _selectedIndex = 0; |
| 41 | + |
| 42 | + void _onTap(int index) { |
| 43 | + _selectedIndex = index; |
| 44 | + debugPrint("Selected index: $_selectedIndex"); |
| 45 | + List<Map<String, String>> props = [ |
| 46 | + {"i": widget.control.id, "selectedindex": _selectedIndex.toString()} |
| 47 | + ]; |
| 48 | + widget.dispatch( |
| 49 | + UpdateControlPropsAction(UpdateControlPropsPayload(props: props))); |
| 50 | + final server = FletAppServices.of(context).server; |
| 51 | + server.updateControlProps(props: props); |
| 52 | + server.sendPageEvent( |
| 53 | + eventTarget: widget.control.id, |
| 54 | + eventName: "change", |
| 55 | + eventData: _selectedIndex.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + Widget build(BuildContext context) { |
| 60 | + debugPrint("CupertinoNavigationBarControl build: ${widget.control.id}"); |
| 61 | + |
| 62 | + bool disabled = widget.control.isDisabled || widget.parentDisabled; |
| 63 | + var selectedIndex = widget.control.attrInt("selectedIndex", 0)!; |
| 64 | + |
| 65 | + if (_selectedIndex != selectedIndex) { |
| 66 | + _selectedIndex = selectedIndex; |
| 67 | + } |
| 68 | + |
| 69 | + var navBar = StoreConnector<AppState, ControlsViewModel>( |
| 70 | + distinct: true, |
| 71 | + converter: (store) => ControlsViewModel.fromStore( |
| 72 | + store, |
| 73 | + widget.children |
| 74 | + .where((c) => c.isVisible && c.name == null) |
| 75 | + .map((c) => c.id)), |
| 76 | + builder: (content, viewModel) { |
| 77 | + return CupertinoTabBar( |
| 78 | + backgroundColor: HexColor.fromString( |
| 79 | + Theme.of(context), widget.control.attrString("bgColor", "")!), |
| 80 | + activeColor: HexColor.fromString( |
| 81 | + Theme.of(context), widget.control.attrString("activeColor", "")!), |
| 82 | + inactiveColor: HexColor.fromString(Theme.of(context), |
| 83 | + widget.control.attrString("inactiveColor", "")!) ?? CupertinoColors.inactiveGray, |
| 84 | + iconSize: widget.control.attrDouble("iconSize", 30.0)!, |
| 85 | + currentIndex: _selectedIndex, |
| 86 | + border: parseBorder(Theme.of(context), widget.control, "border"), |
| 87 | + onTap: _onTap, |
| 88 | + items: viewModel.controlViews.map((destView) { |
| 89 | + var label = destView.control.attrString("label", "")!; |
| 90 | + |
| 91 | + var icon = |
| 92 | + getMaterialIcon(destView.control.attrString("icon", "")!); |
| 93 | + var iconContentCtrls = |
| 94 | + destView.children.where((c) => c.name == "icon_content"); |
| 95 | + |
| 96 | + var selectedIcon = getMaterialIcon( |
| 97 | + destView.control.attrString("selectedIcon", "")!); |
| 98 | + var selectedIconContentCtrls = destView.children |
| 99 | + .where((c) => c.name == "selected_icon_content"); |
| 100 | + |
| 101 | + return BottomNavigationBarItem( |
| 102 | + tooltip: destView.control.attrString("tooltip", "")!, |
| 103 | + icon: iconContentCtrls.isNotEmpty |
| 104 | + ? createControl(destView.control, |
| 105 | + iconContentCtrls.first.id, disabled) |
| 106 | + : Icon(icon), |
| 107 | + activeIcon: selectedIconContentCtrls.isNotEmpty |
| 108 | + ? createControl(destView.control, |
| 109 | + selectedIconContentCtrls.first.id, disabled) |
| 110 | + : selectedIcon != null |
| 111 | + ? Icon(selectedIcon) |
| 112 | + : null, |
| 113 | + label: label); |
| 114 | + }).toList()); |
| 115 | + }); |
| 116 | + |
| 117 | + return constrainedControl(context, navBar, widget.parent, widget.control); |
| 118 | + } |
| 119 | +} |
0 commit comments