diff --git a/example/lib/pages/components/dropdown_example.dart b/example/lib/pages/components/dropdown_example.dart index 0a0c6faa..87e5250d 100644 --- a/example/lib/pages/components/dropdown_example.dart +++ b/example/lib/pages/components/dropdown_example.dart @@ -33,8 +33,13 @@ class _DropdownExampleState extends State { name: "Dropdown", child: Column( crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.spaceEvenly, + mainAxisAlignment: MainAxisAlignment.end, children: [ + ZetaDropdown( + items: items, + value: selectedItem, + type: ZetaDropdownMenuType.checkbox, + ), Center( child: Column( mainAxisSize: MainAxisSize.min, diff --git a/lib/src/components/badges/indicator.dart b/lib/src/components/badges/indicator.dart index 24f2e10c..d760b66b 100644 --- a/lib/src/components/badges/indicator.dart +++ b/lib/src/components/badges/indicator.dart @@ -88,8 +88,7 @@ class ZetaIndicator extends StatelessWidget { @override Widget build(BuildContext context) { final zetaColors = Zeta.of(context).colors; - final Color backgroundColor = - color ?? (type == ZetaIndicatorType.icon ? zetaColors.blue : zetaColors.surfaceNegative); + final Color backgroundColor = (type == ZetaIndicatorType.icon ? zetaColors.blue : zetaColors.surfaceNegative); final Color foregroundColor = backgroundColor.onColor; final sizePixels = _getSizePixels(size, type); diff --git a/lib/src/components/dropdown/dropdown.dart b/lib/src/components/dropdown/dropdown.dart index c291d63d..ba279d51 100644 --- a/lib/src/components/dropdown/dropdown.dart +++ b/lib/src/components/dropdown/dropdown.dart @@ -104,11 +104,21 @@ class ZetaDropdown extends StatefulWidget { } } +/// Enum possible menu positions +enum MenuPosition { + /// IF Menu is rendered above + up, + + /// If menu is rendered below + down, +} + class _ZetaDropDownState extends State> { final OverlayPortalController _tooltipController = OverlayPortalController(); final _link = LayerLink(); final _menuKey = GlobalKey(); final _headerKey = GlobalKey(); + MenuPosition _menuPosition = MenuPosition.down; ZetaDropdownItem? _selectedItem; @@ -137,6 +147,12 @@ class _ZetaDropDownState extends State> { } } + /// Return position of header + Offset get _headerPos { + final headerBox = _headerKey.currentContext!.findRenderObject()! as RenderBox; + return headerBox.localToGlobal(Offset.zero); + } + void _setSelectedItem() { try { _selectedItem = widget.items.firstWhere((item) => item.value == widget.value); @@ -168,13 +184,16 @@ class _ZetaDropDownState extends State> { overlayChildBuilder: (BuildContext context) { return CompositedTransformFollower( link: _link, - targetAnchor: Alignment.bottomLeft, // Align overlay dropdown in its correct position + targetAnchor: _menuPosition == MenuPosition.up + ? Alignment.topLeft + : Alignment.bottomLeft, // Align overlay dropdown in its correct position + followerAnchor: _menuPosition == MenuPosition.up ? Alignment.bottomLeft : Alignment.topLeft, child: Align( - alignment: AlignmentDirectional.topStart, + alignment: + _menuPosition == MenuPosition.up ? AlignmentDirectional.bottomStart : AlignmentDirectional.topStart, child: TapRegion( onTapOutside: (event) { final headerBox = _headerKey.currentContext!.findRenderObject()! as RenderBox; - final headerPosition = headerBox.localToGlobal(Offset.zero); final inHeader = _isInHeader( headerPosition, @@ -218,6 +237,24 @@ class _ZetaDropDownState extends State> { double get _size => widget.size == ZetaDropdownSize.mini ? 120 : 320; void onTap() { + /// Version 1 : Calculate if overflow happens based on using calculations from sizes. + final height = MediaQuery.of(context).size.height; + final headerRenderBox = _headerKey.currentContext!.findRenderObject()! as RenderBox; + final dropdownItemHeight = headerRenderBox.size.height; + + /// Calculate if overflow can happen + final headerPosY = _headerPos.dy; + + if (headerPosY + (dropdownItemHeight * (widget.items.length + 1)) > height) { + setState(() { + _menuPosition = MenuPosition.up; + }); + } else { + setState(() { + _menuPosition = MenuPosition.down; + }); + } + _tooltipController.toggle(); }