import 'package:flutter/material.dart'; import '../../models/themes/quill_icon_theme.dart'; class QuillDropdownButton extends StatefulWidget { const QuillDropdownButton({ required this.initialValue, required this.items, required this.rawitemsmap, required this.onSelected, this.height = 40, this.fillColor, this.hoverElevation = 1, this.highlightElevation = 1, this.iconTheme, Key? key, }) : super(key: key); final double height; final Color? fillColor; final double hoverElevation; final double highlightElevation; final T initialValue; final List> items; final Map rawitemsmap; final ValueChanged onSelected; final QuillIconTheme? iconTheme; @override _QuillDropdownButtonState createState() => _QuillDropdownButtonState(); } // ignore: deprecated_member_use_from_same_package class _QuillDropdownButtonState extends State> { String _currentValue = ''; @override void initState() { super.initState(); _currentValue = widget.rawitemsmap.keys.elementAt(widget.initialValue as int); } @override Widget build(BuildContext context) { return ConstrainedBox( constraints: BoxConstraints.tightFor(height: widget.height), child: RawMaterialButton( visualDensity: VisualDensity.compact, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(widget.iconTheme?.borderRadius ?? 2)), fillColor: widget.fillColor, elevation: 0, hoverElevation: widget.hoverElevation, highlightElevation: widget.hoverElevation, onPressed: _showMenu, child: _buildContent(context), ), ); } void _showMenu() { final popupMenuTheme = PopupMenuTheme.of(context); final button = context.findRenderObject() as RenderBox; final overlay = Overlay.of(context)!.context.findRenderObject() as RenderBox; final position = RelativeRect.fromRect( Rect.fromPoints( button.localToGlobal(Offset.zero, ancestor: overlay), button.localToGlobal(button.size.bottomLeft(Offset.zero), ancestor: overlay), ), Offset.zero & overlay.size, ); showMenu( context: context, elevation: 4, // widget.elevation ?? popupMenuTheme.elevation, initialValue: widget.initialValue, items: widget.items, position: position, shape: popupMenuTheme.shape, // widget.shape ?? popupMenuTheme.shape, color: popupMenuTheme.color, // widget.color ?? popupMenuTheme.color, // captureInheritedThemes: widget.captureInheritedThemes, ).then((newValue) { if (!mounted) return null; if (newValue == null) { // if (widget.onCanceled != null) widget.onCanceled(); return null; } setState(() { _currentValue = widget.rawitemsmap.entries .firstWhere((element) => element.value == newValue, orElse: () => widget.rawitemsmap.entries.first) .key; widget.onSelected(newValue); }); }); } Widget _buildContent(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 10), child: Row( mainAxisSize: MainAxisSize.min, children: [ Text(_currentValue.toString()), SizedBox(width: 6), const Icon(Icons.arrow_drop_down, size: 17) ], ), ); } }