|
| 1 | +import 'package:assorted_layout_widgets/assorted_layout_widgets.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:yaru/yaru.dart'; |
| 4 | + |
| 5 | +enum _YaruSplitButtonVariant { elevated, filled, outlined } |
| 6 | + |
| 7 | +class YaruSplitButton extends StatelessWidget { |
| 8 | + const YaruSplitButton({ |
| 9 | + super.key, |
| 10 | + required this.items, |
| 11 | + this.onPressed, |
| 12 | + this.child, |
| 13 | + this.onOptionsPressed, |
| 14 | + this.icon, |
| 15 | + this.radius, |
| 16 | + this.menuWidth = menuDefaultWidth, |
| 17 | + }) : _variant = _YaruSplitButtonVariant.elevated; |
| 18 | + |
| 19 | + const YaruSplitButton.filled({ |
| 20 | + super.key, |
| 21 | + required this.items, |
| 22 | + this.onPressed, |
| 23 | + this.child, |
| 24 | + this.onOptionsPressed, |
| 25 | + this.icon, |
| 26 | + this.radius, |
| 27 | + this.menuWidth = menuDefaultWidth, |
| 28 | + }) : _variant = _YaruSplitButtonVariant.filled; |
| 29 | + |
| 30 | + const YaruSplitButton.outlined({ |
| 31 | + super.key, |
| 32 | + required this.items, |
| 33 | + this.onPressed, |
| 34 | + this.child, |
| 35 | + this.onOptionsPressed, |
| 36 | + this.icon, |
| 37 | + this.radius, |
| 38 | + this.menuWidth = menuDefaultWidth, |
| 39 | + }) : _variant = _YaruSplitButtonVariant.outlined; |
| 40 | + |
| 41 | + final _YaruSplitButtonVariant _variant; |
| 42 | + final void Function()? onPressed; |
| 43 | + final void Function()? onOptionsPressed; |
| 44 | + final Widget? child; |
| 45 | + final Widget? icon; |
| 46 | + final List<PopupMenuEntry<Object?>> items; |
| 47 | + final double? radius; |
| 48 | + final double menuWidth; |
| 49 | + |
| 50 | + static const menuDefaultWidth = 148.0; |
| 51 | + |
| 52 | + @override |
| 53 | + Widget build(BuildContext context) { |
| 54 | + // TODO: fix common_themes to use a fixed size for buttons instead of fiddling around with padding |
| 55 | + // then we can rely on this size here |
| 56 | + const size = Size.square(36); |
| 57 | + const dropdownPadding = EdgeInsets.only(top: 16, bottom: 16); |
| 58 | + |
| 59 | + final defaultRadius = Radius.circular(radius ?? kYaruButtonRadius); |
| 60 | + |
| 61 | + final mainActionShape = RoundedRectangleBorder( |
| 62 | + borderRadius: BorderRadius.only( |
| 63 | + topLeft: defaultRadius, |
| 64 | + bottomLeft: defaultRadius, |
| 65 | + ), |
| 66 | + ); |
| 67 | + |
| 68 | + final dropdownShape = switch (_variant) { |
| 69 | + _YaruSplitButtonVariant.outlined => |
| 70 | + const NonUniformRoundedRectangleBorder(hideLeftSide: true), |
| 71 | + _ => RoundedRectangleBorder( |
| 72 | + borderRadius: BorderRadius.only( |
| 73 | + topRight: defaultRadius, |
| 74 | + bottomRight: defaultRadius, |
| 75 | + ), |
| 76 | + ), |
| 77 | + }; |
| 78 | + |
| 79 | + final onDropdownPressed = onPressed == null |
| 80 | + ? null |
| 81 | + : (onOptionsPressed ?? |
| 82 | + () => showMenu( |
| 83 | + context: context, |
| 84 | + position: _menuPosition(context), |
| 85 | + items: items, |
| 86 | + menuPadding: EdgeInsets.symmetric(vertical: defaultRadius.x), |
| 87 | + constraints: BoxConstraints( |
| 88 | + minWidth: menuWidth, |
| 89 | + maxWidth: menuWidth, |
| 90 | + ), |
| 91 | + )); |
| 92 | + |
| 93 | + return Row( |
| 94 | + mainAxisSize: MainAxisSize.min, |
| 95 | + children: [ |
| 96 | + switch (_variant) { |
| 97 | + _YaruSplitButtonVariant.elevated => ElevatedButton( |
| 98 | + style: ElevatedButton.styleFrom(shape: mainActionShape), |
| 99 | + onPressed: onPressed, |
| 100 | + child: child, |
| 101 | + ), |
| 102 | + _YaruSplitButtonVariant.filled => FilledButton( |
| 103 | + style: FilledButton.styleFrom(shape: mainActionShape), |
| 104 | + onPressed: onPressed, |
| 105 | + child: child, |
| 106 | + ), |
| 107 | + _YaruSplitButtonVariant.outlined => OutlinedButton( |
| 108 | + style: OutlinedButton.styleFrom(shape: mainActionShape), |
| 109 | + onPressed: onPressed, |
| 110 | + child: child, |
| 111 | + ), |
| 112 | + }, |
| 113 | + switch (_variant) { |
| 114 | + _YaruSplitButtonVariant.elevated => ElevatedButton( |
| 115 | + style: ElevatedButton.styleFrom( |
| 116 | + fixedSize: size, |
| 117 | + minimumSize: size, |
| 118 | + maximumSize: size, |
| 119 | + padding: dropdownPadding, |
| 120 | + shape: dropdownShape, |
| 121 | + ), |
| 122 | + onPressed: onDropdownPressed, |
| 123 | + child: icon ?? const Icon(YaruIcons.pan_down), |
| 124 | + ), |
| 125 | + _YaruSplitButtonVariant.filled => FilledButton( |
| 126 | + style: FilledButton.styleFrom( |
| 127 | + fixedSize: size, |
| 128 | + minimumSize: size, |
| 129 | + maximumSize: size, |
| 130 | + padding: dropdownPadding, |
| 131 | + shape: dropdownShape, |
| 132 | + ), |
| 133 | + onPressed: onDropdownPressed, |
| 134 | + child: icon ?? const Icon(YaruIcons.pan_down), |
| 135 | + ), |
| 136 | + _YaruSplitButtonVariant.outlined => OutlinedButton( |
| 137 | + style: OutlinedButton.styleFrom( |
| 138 | + fixedSize: size, |
| 139 | + minimumSize: size, |
| 140 | + maximumSize: size, |
| 141 | + padding: dropdownPadding, |
| 142 | + shape: dropdownShape, |
| 143 | + ), |
| 144 | + onPressed: onDropdownPressed, |
| 145 | + child: icon ?? const Icon(YaruIcons.pan_down), |
| 146 | + ), |
| 147 | + }, |
| 148 | + ], |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + RelativeRect _menuPosition(BuildContext context) { |
| 153 | + final bar = context.findRenderObject() as RenderBox; |
| 154 | + final overlay = Overlay.of(context).context.findRenderObject() as RenderBox; |
| 155 | + const offset = Offset.zero; |
| 156 | + |
| 157 | + return RelativeRect.fromRect( |
| 158 | + Rect.fromPoints( |
| 159 | + bar.localToGlobal( |
| 160 | + bar.size.bottomCenter(offset), |
| 161 | + ancestor: overlay, |
| 162 | + ), |
| 163 | + bar.localToGlobal( |
| 164 | + bar.size.bottomLeft(offset), |
| 165 | + ancestor: overlay, |
| 166 | + ), |
| 167 | + ), |
| 168 | + offset & overlay.size, |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
0 commit comments