|
| 1 | +import 'package:design_system/design_system.dart'; |
| 2 | +import 'package:design_system/extensions/color_extensions.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 5 | + |
| 6 | +typedef MenuItems<T> = ({T value, String label}); |
| 7 | + |
| 8 | +const _kAnimationDuration = Duration(milliseconds: 200); |
| 9 | + |
| 10 | +class AppSelectDropdown<T> extends StatefulWidget { |
| 11 | + final double? width; |
| 12 | + final String label; |
| 13 | + final List<MenuItems<T>> items; |
| 14 | + final Function(T value)? onChanged; |
| 15 | + |
| 16 | + const AppSelectDropdown({ |
| 17 | + required this.items, |
| 18 | + required this.label, |
| 19 | + this.onChanged, |
| 20 | + this.width, |
| 21 | + super.key, |
| 22 | + }); |
| 23 | + |
| 24 | + @override |
| 25 | + State<AppSelectDropdown<T>> createState() => _AppSelectDropdownState<T>(); |
| 26 | +} |
| 27 | + |
| 28 | +class _AppSelectDropdownState<T> extends State<AppSelectDropdown<T>> |
| 29 | + with SingleTickerProviderStateMixin { |
| 30 | + late final FocusNode _buttonFocusNode; |
| 31 | + final MenuController _menuController = MenuController(); |
| 32 | + final List<MenuItems<T>> _selectedValues = []; |
| 33 | + late AnimationController _animationController; |
| 34 | + static final Animatable<double> _easeInTween = |
| 35 | + CurveTween(curve: Curves.easeIn); |
| 36 | + static final Animatable<double> _halfTween = |
| 37 | + Tween<double>(begin: 0.0, end: 0.5); |
| 38 | + |
| 39 | + // ignore: unused_field |
| 40 | + late Animation<double> _iconTurns; |
| 41 | + @override |
| 42 | + void initState() { |
| 43 | + _buttonFocusNode = FocusNode(debugLabel: 'Menu Button-${widget.label}'); |
| 44 | + super.initState(); |
| 45 | + _animationController = AnimationController( |
| 46 | + duration: _kAnimationDuration, |
| 47 | + vsync: this, |
| 48 | + ); |
| 49 | + |
| 50 | + _iconTurns = _animationController.drive(_halfTween.chain(_easeInTween)); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + void dispose() { |
| 55 | + _buttonFocusNode.dispose(); |
| 56 | + _animationController.dispose(); |
| 57 | + super.dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + Widget build(BuildContext context) => MenuAnchor( |
| 62 | + childFocusNode: _buttonFocusNode, |
| 63 | + menuChildren: widget.items |
| 64 | + .map( |
| 65 | + (elem) => CheckboxMenuButton( |
| 66 | + closeOnActivate: false, |
| 67 | + value: _selectedValues.contains(elem), |
| 68 | + onChanged: (value) { |
| 69 | + widget.onChanged?.call(elem.value); |
| 70 | + setState(() { |
| 71 | + if (value!) { |
| 72 | + _selectedValues.add(elem); |
| 73 | + } else { |
| 74 | + _selectedValues.remove(elem); |
| 75 | + } |
| 76 | + }); |
| 77 | + }, |
| 78 | + child: Container( |
| 79 | + width: _calculateWidth(), |
| 80 | + constraints: BoxConstraints( |
| 81 | + minWidth: 90.w, |
| 82 | + ), |
| 83 | + child: Text( |
| 84 | + elem.label, |
| 85 | + style: context.theme.textStyles.bodyMedium, |
| 86 | + overflow: TextOverflow.ellipsis, |
| 87 | + maxLines: 1, |
| 88 | + ), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ) |
| 92 | + .toList(), |
| 93 | + controller: _menuController, |
| 94 | + builder: ( |
| 95 | + BuildContext context, |
| 96 | + MenuController controller, |
| 97 | + Widget? child, |
| 98 | + ) => |
| 99 | + Container( |
| 100 | + constraints: BoxConstraints( |
| 101 | + minWidth: 90.w, |
| 102 | + maxWidth: 1.sw, |
| 103 | + ), |
| 104 | + decoration: BoxDecoration( |
| 105 | + color: context.theme.colorScheme.surface.getShade(100), |
| 106 | + borderRadius: BorderRadius.circular(4.r), |
| 107 | + border: Border.all( |
| 108 | + color: context.theme.colorScheme.onSurface.getShade(200), |
| 109 | + ), |
| 110 | + ), |
| 111 | + child: InkWell( |
| 112 | + onTap: () { |
| 113 | + if (controller.isOpen) { |
| 114 | + controller.close(); |
| 115 | + _animationController.reverse(); |
| 116 | + } else { |
| 117 | + controller.open(); |
| 118 | + _animationController.forward(); |
| 119 | + } |
| 120 | + }, |
| 121 | + child: Row( |
| 122 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 123 | + mainAxisSize: MainAxisSize.min, |
| 124 | + children: [ |
| 125 | + Flexible( |
| 126 | + child: Container( |
| 127 | + width: widget.width ?? 1.sw, |
| 128 | + padding: EdgeInsets.all(8.sp), |
| 129 | + child: Text( |
| 130 | + _selectedValues.isEmpty |
| 131 | + ? widget.label |
| 132 | + : _selectedValues.map((e) => e.label).join(', '), |
| 133 | + overflow: TextOverflow.ellipsis, |
| 134 | + maxLines: 1, |
| 135 | + style: context.theme.textStyles.labelMedium, |
| 136 | + ), |
| 137 | + ), |
| 138 | + ), |
| 139 | + AnimatedBuilder( |
| 140 | + animation: _animationController, |
| 141 | + builder: (context, child) => Padding( |
| 142 | + padding: EdgeInsets.all(8.sp), |
| 143 | + child: RotationTransition( |
| 144 | + turns: _iconTurns, |
| 145 | + child: Icon( |
| 146 | + Icons.expand_more, |
| 147 | + color: context.theme.customColors.textColor, |
| 148 | + ), |
| 149 | + ), |
| 150 | + ), |
| 151 | + ), |
| 152 | + ], |
| 153 | + ), |
| 154 | + ), |
| 155 | + ), |
| 156 | + ); |
| 157 | + |
| 158 | + double _calculateWidth() { |
| 159 | + final width = widget.width; |
| 160 | + if (width == null) return 1.sw * .761; |
| 161 | + if (width <= .3.sw) return width * .9; |
| 162 | + if (width <= .5.sw) return width * .93; |
| 163 | + if (width <= .8.sw) return width * .95; |
| 164 | + if (width < 1.sw) return width * .8; |
| 165 | + return width * .761; |
| 166 | + } |
| 167 | +} |
0 commit comments