LCOV - code coverage report
Current view: top level - components/select - _foss_select_listbox.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 364 364
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : part of 'foss_select.dart';
       2              : 
       3              : /// How a row marks selection: a lone check on the picked row (single-select) or
       4              : /// a checkbox on every row (multi-select).
       5              : enum _SelectIndicator { checkmark, checkbox }
       6              : 
       7              : /// The shared trigger, anchored popup, roving highlight, type-ahead, and
       8              : /// dismiss handling for both [FossSelect] and [FossMultiSelect]. The two faces
       9              : /// differ only in the config they pass: the selection predicate, the pick
      10              : /// handler, whether a pick closes the popup, and the row indicator.
      11              : class _FossSelectField<T> extends StatefulWidget {
      12            8 :   const _FossSelectField({
      13              :     required this.items,
      14              :     required this.size,
      15              :     required this.enabled,
      16              :     required this.triggerLabel,
      17              :     required this.indicator,
      18              :     required this.isSelected,
      19              :     required this.onPick,
      20              :     required this.closeOnPick,
      21              :     this.style,
      22              :     this.label,
      23              :     this.placeholder,
      24              :     this.errorText,
      25              :     super.key,
      26              :   });
      27              : 
      28              :   final List<FossSelectItem<T>> items;
      29              :   final FossSelectSize size;
      30              :   final bool enabled;
      31              :   final String? triggerLabel;
      32              :   final _SelectIndicator indicator;
      33              :   final bool Function(T value) isSelected;
      34              :   final void Function(T value) onPick;
      35              :   final bool closeOnPick;
      36              :   final FossSelectStyle? style;
      37              :   final String? label;
      38              :   final String? placeholder;
      39              :   final String? errorText;
      40              : 
      41            8 :   @override
      42            8 :   State<_FossSelectField<T>> createState() => _FossSelectFieldState<T>();
      43              : }
      44              : 
      45              : class _FossSelectFieldState<T> extends State<_FossSelectField<T>>
      46              :     with SingleTickerProviderStateMixin, WidgetsBindingObserver {
      47              :   final WidgetStatesController _states = WidgetStatesController();
      48              :   final OverlayPortalController _portal = OverlayPortalController();
      49              :   final GlobalKey _anchorKey = GlobalKey();
      50              :   final FocusNode _triggerFocus = FocusNode(debugLabel: 'FossSelect trigger');
      51              :   final FocusNode _popupFocus = FocusNode(debugLabel: 'FossSelect popup');
      52              : 
      53              :   late final AnimationController _animation;
      54              :   late final CurvedAnimation _curve;
      55              :   late final Animation<double> _scale;
      56              : 
      57              :   final List<ScrollPosition> _scrollPositions = [];
      58              : 
      59              :   bool _open = false;
      60              :   int _highlight = -1;
      61              :   String _typed = '';
      62              :   DateTime _lastKeystroke = DateTime.fromMillisecondsSinceEpoch(0);
      63              : 
      64            8 :   @override
      65              :   void initState() {
      66            8 :     super.initState();
      67           16 :     WidgetsBinding.instance.addObserver(this);
      68           16 :     _animation = AnimationController(vsync: this);
      69           24 :     _curve = CurvedAnimation(parent: _animation, curve: Curves.easeOut);
      70           32 :     _scale = Tween<double>(begin: _openScale, end: 1).animate(_curve);
      71              :   }
      72              : 
      73            8 :   @override
      74              :   void dispose() {
      75           16 :     WidgetsBinding.instance.removeObserver(this);
      76            8 :     _detachScrollDismiss();
      77           16 :     _animation.dispose();
      78           16 :     _curve.dispose();
      79           16 :     _states.dispose();
      80           16 :     _triggerFocus.dispose();
      81           16 :     _popupFocus.dispose();
      82            8 :     super.dispose();
      83              :   }
      84              : 
      85              :   // The Android system back closes the popup rather than popping the route.
      86            1 :   @override
      87              :   Future<bool> didPopRoute() async {
      88            1 :     if (!_open) return false;
      89            1 :     _close();
      90              :     return true;
      91              :   }
      92              : 
      93            8 :   bool get _reduceMotion =>
      94           24 :       MediaQuery.maybeOf(context)?.disableAnimations ?? false;
      95              : 
      96           40 :   Duration get _duration => context.fossTheme.motion.overlay;
      97              : 
      98           24 :   void _toggle() => _open ? _close() : _openPopup();
      99              : 
     100            8 :   void _openPopup() {
     101           24 :     if (!widget.enabled || _open) return;
     102           16 :     setState(() {
     103            8 :       _open = true;
     104           16 :       _highlight = _initialHighlight();
     105            8 :       _typed = '';
     106              :     });
     107           16 :     _portal.show();
     108            8 :     _attachScrollDismiss();
     109           16 :     _popupFocus.requestFocus();
     110           32 :     _animation.duration = _reduceMotion ? Duration.zero : _duration;
     111           24 :     _animation.forward(from: _reduceMotion ? 1 : 0);
     112              :   }
     113              : 
     114              :   // Close the popup when any enclosing scrollable moves, so it never floats
     115              :   // detached from its trigger. Every ancestor is tracked, not just the nearest,
     116              :   // so a page swipe dismisses it even when the page has its own scroll view.
     117            8 :   void _attachScrollDismiss() {
     118              :     for (
     119           16 :       var scrollable = Scrollable.maybeOf(context);
     120              :       scrollable != null;
     121            2 :       scrollable = Scrollable.maybeOf(scrollable.context)
     122              :     ) {
     123            3 :       scrollable.position.addListener(_close);
     124            3 :       _scrollPositions.add(scrollable.position);
     125              :     }
     126              :   }
     127              : 
     128            8 :   void _detachScrollDismiss() {
     129            9 :     for (final position in _scrollPositions) {
     130            2 :       position.removeListener(_close);
     131              :     }
     132           16 :     _scrollPositions.clear();
     133              :   }
     134              : 
     135            5 :   void _close() {
     136            5 :     if (!_open) return;
     137            5 :     _detachScrollDismiss();
     138           15 :     setState(() => _open = false);
     139           10 :     _triggerFocus.requestFocus();
     140            5 :     if (_reduceMotion) {
     141            2 :       _animation.value = 0;
     142            2 :       _portal.hide();
     143              :       return;
     144              :     }
     145           15 :     _animation.duration = _duration;
     146            5 :     unawaited(
     147           20 :       _animation.reverse().whenComplete(() {
     148           20 :         if (mounted && _animation.status == AnimationStatus.dismissed) {
     149           10 :           _portal.hide();
     150              :         }
     151              :       }),
     152              :     );
     153              :   }
     154              : 
     155            8 :   int _initialHighlight() {
     156           24 :     final selected = widget.items.indexWhere(
     157           48 :       (i) => i.enabled && widget.isSelected(i.value),
     158              :     );
     159           16 :     if (selected != -1) return selected;
     160           40 :     return widget.items.indexWhere((i) => i.enabled);
     161              :   }
     162              : 
     163            3 :   void _pick(FossSelectItem<T> item) {
     164            3 :     if (!item.enabled) return;
     165           12 :     widget.onPick(item.value);
     166            6 :     if (widget.closeOnPick) {
     167            2 :       _close();
     168              :     } else {
     169            6 :       setState(() => _highlight = widget.items.indexOf(item));
     170              :     }
     171              :   }
     172              : 
     173            1 :   void _moveHighlight(int delta) {
     174            3 :     final count = widget.items.length;
     175            1 :     if (count == 0) return;
     176            1 :     var next = _highlight;
     177            1 :     for (var step = 0; step < count; step++) {
     178            2 :       next = (next + delta) % count;
     179            4 :       if (widget.items[next].enabled) {
     180            3 :         setState(() => _highlight = next);
     181              :         return;
     182              :       }
     183              :     }
     184              :   }
     185              : 
     186            1 :   void _typeahead(String char) {
     187            1 :     final now = DateTime.now();
     188            5 :     _typed = now.difference(_lastKeystroke).inMilliseconds > 700
     189              :         ? char
     190            2 :         : _typed + char;
     191            1 :     _lastKeystroke = now;
     192            2 :     final query = _typed.toLowerCase();
     193            3 :     final match = widget.items.indexWhere(
     194            5 :       (i) => i.enabled && i.label.toLowerCase().startsWith(query),
     195              :     );
     196            5 :     if (match != -1) setState(() => _highlight = match);
     197              :   }
     198              : 
     199            5 :   KeyEventResult _onKey(FocusNode node, KeyEvent event) {
     200            5 :     if (event is KeyUpEvent) return KeyEventResult.ignored;
     201            5 :     final key = event.logicalKey;
     202            5 :     if (key == LogicalKeyboardKey.escape) {
     203            5 :       _close();
     204              :       return KeyEventResult.handled;
     205              :     }
     206            2 :     if (key == LogicalKeyboardKey.arrowDown) {
     207            1 :       _moveHighlight(1);
     208              :       return KeyEventResult.handled;
     209              :     }
     210            2 :     if (key == LogicalKeyboardKey.arrowUp) {
     211            2 :       _moveHighlight(-1);
     212              :       return KeyEventResult.handled;
     213              :     }
     214            3 :     if (key == LogicalKeyboardKey.enter || key == LogicalKeyboardKey.space) {
     215           14 :       if (_highlight >= 0 && _highlight < widget.items.length) {
     216           10 :         _pick(widget.items[_highlight]);
     217              :       }
     218              :       return KeyEventResult.handled;
     219              :     }
     220            1 :     final char = event.character;
     221            4 :     if (char != null && char.trim().isNotEmpty && char.length == 1) {
     222            1 :       _typeahead(char);
     223              :       return KeyEventResult.handled;
     224              :     }
     225              :     return KeyEventResult.ignored;
     226              :   }
     227              : 
     228            8 :   @override
     229              :   Widget build(BuildContext context) {
     230            8 :     final theme = context.fossTheme;
     231           48 :     final v = _apply(_resolve(theme, widget.size), widget.style);
     232           16 :     final hasError = widget.errorText != null;
     233              : 
     234            8 :     final trigger = _trigger(theme, v, hasError: hasError);
     235              : 
     236            8 :     final field = Column(
     237              :       mainAxisSize: MainAxisSize.min,
     238              :       crossAxisAlignment: CrossAxisAlignment.start,
     239            8 :       children: [
     240           17 :         if (widget.label case final label?) ...[
     241            1 :           Text(
     242              :             label,
     243            4 :             style: theme.typography.base.medium.copyWith(
     244            2 :               color: theme.colors.foreground,
     245              :             ),
     246              :           ),
     247            3 :           SizedBox(height: theme.spacing(2)),
     248              :         ],
     249            8 :         OverlayPortal(
     250            8 :           controller: _portal,
     251           16 :           overlayChildBuilder: (context) => _buildOverlay(context, theme, v),
     252              :           child: trigger,
     253              :         ),
     254              :       ],
     255              :     );
     256              : 
     257              :     return field;
     258              :   }
     259              : 
     260            8 :   Widget _trigger(
     261              :     FossThemeData theme,
     262              :     _SelectVisuals v, {
     263              :     required bool hasError,
     264              :   }) {
     265           16 :     final label = widget.triggerLabel;
     266           16 :     final text = label ?? widget.placeholder ?? '';
     267           12 :     final textColor = label != null ? v.foreground : v.placeholderColor;
     268              : 
     269            8 :     Widget box = ListenableBuilder(
     270            8 :       listenable: _states,
     271           16 :       builder: (_, _) => _triggerBox(
     272              :         theme,
     273              :         v,
     274              :         text: text,
     275              :         textColor: textColor,
     276              :         hasError: hasError,
     277              :       ),
     278              :     );
     279              : 
     280            8 :     box = ConstrainedBox(
     281              :       constraints: const BoxConstraints(
     282              :         minHeight: _minTapTarget,
     283              :         minWidth: _minWidth,
     284              :       ),
     285            8 :       child: Align(heightFactor: 1, child: box),
     286              :     );
     287              : 
     288           16 :     if (!widget.enabled) {
     289            4 :       box = Opacity(opacity: _disabledOpacity, child: box);
     290              :     }
     291              : 
     292            8 :     return Semantics(
     293              :       button: true,
     294           16 :       enabled: widget.enabled,
     295           16 :       label: widget.label,
     296           16 :       value: label ?? widget.placeholder,
     297           16 :       hint: widget.errorText,
     298            8 :       expanded: _open,
     299            8 :       child: FocusableActionDetector(
     300            8 :         focusNode: _triggerFocus,
     301           16 :         enabled: widget.enabled,
     302           16 :         mouseCursor: widget.enabled
     303              :             ? SystemMouseCursors.click
     304              :             : SystemMouseCursors.basic,
     305            5 :         onShowFocusHighlight: (value) =>
     306           10 :             _states.update(WidgetState.focused, value),
     307              :         // Arrow Down opens the closed trigger, alongside Enter and Space; once
     308              :         // open, arrow keys rove the popup instead.
     309              :         shortcuts: const {
     310              :           SingleActivator(LogicalKeyboardKey.arrowDown): ActivateIntent(),
     311              :         },
     312            8 :         actions: <Type, Action<Intent>>{
     313            8 :           ActivateIntent: CallbackAction<ActivateIntent>(
     314            2 :             onInvoke: (_) {
     315            2 :               _openPopup();
     316              :               return null;
     317              :             },
     318              :           ),
     319              :         },
     320            8 :         child: GestureDetector(
     321              :           behavior: HitTestBehavior.opaque,
     322           24 :           onTap: widget.enabled ? _toggle : null,
     323           16 :           child: KeyedSubtree(key: _anchorKey, child: box),
     324              :         ),
     325              :       ),
     326              :     );
     327              :   }
     328              : 
     329            8 :   Widget _triggerBox(
     330              :     FossThemeData theme,
     331              :     _SelectVisuals v, {
     332              :     required String text,
     333              :     required Color textColor,
     334              :     required bool hasError,
     335              :   }) {
     336            8 :     final colors = theme.colors;
     337            8 :     final dark = colors.isDark;
     338           24 :     final focused = _states.value.contains(WidgetState.focused);
     339            8 :     final atRest = !focused && !_open && !hasError;
     340              : 
     341            8 :     var borderColor = v.borderColor;
     342              :     Color? ringColor;
     343              :     if (hasError) {
     344            4 :       borderColor = colors.destructive.withValues(
     345              :         alpha: focused ? _errorBorderFocusedOpacity : _errorBorderOpacity,
     346              :       );
     347              :       if (focused) {
     348            2 :         ringColor = colors.destructive.withValues(
     349              :           alpha: dark ? _errorRingOpacityDark : _errorRingOpacityLight,
     350              :         );
     351              :       }
     352              :     } else if (focused) {
     353            5 :       borderColor = colors.ring;
     354           10 :       ringColor = colors.ring.withValues(alpha: _focusRingOpacity);
     355              :     }
     356              : 
     357            8 :     Widget content = Padding(
     358            8 :       padding: v.padding,
     359            8 :       child: Row(
     360            8 :         children: [
     361            8 :           Expanded(
     362            8 :             child: Text(
     363              :               text,
     364              :               maxLines: 1,
     365              :               overflow: TextOverflow.ellipsis,
     366           16 :               style: v.textStyle.copyWith(color: textColor),
     367              :             ),
     368              :           ),
     369           16 :           SizedBox(width: v.gap),
     370            8 :           FossGlyphIcon(
     371            8 :             ChevronUpDownGlyph(
     372           40 :               v.foreground.withValues(alpha: v.foreground.a * 0.8),
     373              :             ),
     374            8 :             size: v.iconSize,
     375              :           ),
     376              :         ],
     377              :       ),
     378              :     );
     379              : 
     380            8 :     content = ConstrainedBox(
     381           16 :       constraints: BoxConstraints(minHeight: v.minHeight),
     382            8 :       child: DecoratedBox(
     383            8 :         decoration: ShapeDecoration(
     384            8 :           color: v.background,
     385            8 :           shape: RoundedSuperellipseBorder(
     386            8 :             side: BorderSide(color: borderColor),
     387           16 :             borderRadius: BorderRadius.circular(v.borderRadius),
     388              :           ),
     389            8 :           shadows: atRest ? v.shadow : const [],
     390              :         ),
     391              :         child: content,
     392              :       ),
     393              :     );
     394              : 
     395              :     if (atRest) {
     396            8 :       content = CustomPaint(
     397            8 :         foregroundPainter: _RimPainter(
     398              :           color: dark ? _rimDark : _rimLight,
     399            8 :           radius: v.borderRadius,
     400              :           topLit: dark,
     401              :         ),
     402              :         child: content,
     403              :       );
     404              :     }
     405              : 
     406              :     if (ringColor != null) {
     407            5 :       content = CustomPaint(
     408            5 :         foregroundPainter: _RectRingPainter(
     409              :           color: ringColor,
     410            5 :           offsetColor: colors.background,
     411            5 :           radius: v.borderRadius,
     412              :         ),
     413              :         child: content,
     414              :       );
     415              :     }
     416              : 
     417              :     return content;
     418              :   }
     419              : 
     420            8 :   Widget _buildOverlay(
     421              :     BuildContext context,
     422              :     FossThemeData theme,
     423              :     _SelectVisuals v,
     424              :   ) {
     425            8 :     final anchor = _anchorRect(context);
     426              :     if (anchor == null) return const SizedBox.shrink();
     427              :     // Subtract any on-screen keyboard so the popup lays out against the visible
     428              :     // area, not behind the keyboard.
     429           16 :     final bottomInset = MediaQuery.viewInsetsOf(context).bottom;
     430              :     // Android back is handled in didPopRoute, so no Router is required here.
     431            8 :     return Stack(
     432            8 :       children: [
     433            8 :         Positioned.fill(
     434            8 :           child: GestureDetector(
     435              :             behavior: HitTestBehavior.opaque,
     436            8 :             onTap: _close,
     437              :           ),
     438              :         ),
     439            8 :         Positioned.fill(
     440            8 :           child: CustomSingleChildLayout(
     441            8 :             delegate: _PopupLayout(
     442              :               anchor: anchor,
     443              :               bottomInset: bottomInset,
     444            8 :               textDirection: Directionality.of(context),
     445              :             ),
     446            8 :             child: FadeTransition(
     447            8 :               opacity: _curve,
     448            8 :               child: ScaleTransition(
     449            8 :                 scale: _scale,
     450              :                 alignment: Alignment.topCenter,
     451            8 :                 child: _popup(theme, v),
     452              :               ),
     453              :             ),
     454              :           ),
     455              :         ),
     456              :       ],
     457              :     );
     458              :   }
     459              : 
     460            8 :   Widget _popup(FossThemeData theme, _SelectVisuals v) {
     461            8 :     final list = ListView(
     462              :       shrinkWrap: true,
     463           24 :       padding: EdgeInsets.all(theme.spacing(1)),
     464            8 :       children: [
     465           40 :         for (var i = 0; i < widget.items.length; i++)
     466            8 :           _SelectRow<T>(
     467           24 :             item: widget.items[i],
     468              :             theme: theme,
     469              :             visuals: v,
     470           56 :             selected: widget.isSelected(widget.items[i].value),
     471           16 :             highlighted: i == _highlight,
     472           16 :             indicator: widget.indicator,
     473            1 :             onEnter: () {
     474            5 :               if (_highlight != i) setState(() => _highlight = i);
     475              :             },
     476           10 :             onTap: () => _pick(widget.items[i]),
     477              :           ),
     478              :       ],
     479              :     );
     480              : 
     481           16 :     final dark = theme.colors.isDark;
     482           16 :     final radius = BorderRadius.circular(v.borderRadius);
     483            8 :     final surface = DecoratedBox(
     484            8 :       decoration: ShapeDecoration(
     485            8 :         color: v.popupColor,
     486            8 :         shape: RoundedSuperellipseBorder(
     487           16 :           side: BorderSide(color: v.popupBorderColor),
     488              :           borderRadius: radius,
     489              :         ),
     490            8 :         shadows: v.popupShadow,
     491              :       ),
     492              :       // Clip with the same superellipse as the border so a highlighted first or
     493              :       // last row never pokes past the corner.
     494            8 :       child: ClipPath(
     495            8 :         clipper: ShapeBorderClipper(
     496            8 :           shape: RoundedSuperellipseBorder(borderRadius: radius),
     497              :         ),
     498              :         child: list,
     499              :       ),
     500              :     );
     501              : 
     502            8 :     return Semantics(
     503              :       role: SemanticsRole.menu,
     504              :       container: true,
     505            8 :       child: Focus(
     506            8 :         focusNode: _popupFocus,
     507            8 :         onKeyEvent: _onKey,
     508            8 :         child: CustomPaint(
     509            8 :           foregroundPainter: _RimPainter(
     510              :             color: dark ? _rimDark : _rimLight,
     511            8 :             radius: v.borderRadius,
     512              :             topLit: dark,
     513              :           ),
     514              :           child: surface,
     515              :         ),
     516              :       ),
     517              :     );
     518              :   }
     519              : 
     520            8 :   Rect? _anchorRect(BuildContext overlayContext) {
     521           24 :     final anchor = _anchorKey.currentContext?.findRenderObject();
     522           24 :     final overlay = Overlay.of(overlayContext).context.findRenderObject();
     523           24 :     if (anchor is! RenderBox || overlay is! RenderBox || !anchor.attached) {
     524              :       return null;
     525              :     }
     526           24 :     return anchor.localToGlobal(Offset.zero, ancestor: overlay) & anchor.size;
     527              :   }
     528              : }
     529              : 
     530              : /// A single popup row: an indicator column, an optional icon, and the label.
     531              : class _SelectRow<T> extends StatelessWidget {
     532            8 :   const _SelectRow({
     533              :     required this.item,
     534              :     required this.theme,
     535              :     required this.visuals,
     536              :     required this.selected,
     537              :     required this.highlighted,
     538              :     required this.indicator,
     539              :     required this.onEnter,
     540              :     required this.onTap,
     541              :   });
     542              : 
     543              :   final FossSelectItem<T> item;
     544              :   final FossThemeData theme;
     545              :   final _SelectVisuals visuals;
     546              :   final bool selected;
     547              :   final bool highlighted;
     548              :   final _SelectIndicator indicator;
     549              :   final VoidCallback onEnter;
     550              :   final VoidCallback onTap;
     551              : 
     552            8 :   @override
     553              :   Widget build(BuildContext context) {
     554           16 :     final enabled = item.enabled;
     555           24 :     final fill = highlighted ? visuals.highlightColor : null;
     556            8 :     final textColor = highlighted
     557           16 :         ? visuals.highlightForeground
     558           16 :         : visuals.foreground;
     559              : 
     560            8 :     Widget row = Padding(
     561              :       // Asymmetric like the row spec: a wider end inset past the label, a
     562              :       // tighter start inset against the indicator column.
     563            8 :       padding: EdgeInsetsDirectional.only(
     564           24 :         start: theme.spacing(2),
     565           24 :         end: theme.spacing(4),
     566           24 :         top: theme.spacing(1),
     567           24 :         bottom: theme.spacing(1),
     568              :       ),
     569            8 :       child: Row(
     570            8 :         children: [
     571            8 :           SizedBox(
     572              :             width: _indicatorColumn,
     573              :             height: _indicatorColumn,
     574            8 :             child: _indicatorWidget(textColor),
     575              :           ),
     576           32 :           SizedBox(width: theme.spacing(2)),
     577           17 :           if (item.icon case final icon?) ...[
     578            1 :             IconTheme.merge(
     579            3 :               data: IconThemeData(color: textColor, size: visuals.iconSize),
     580              :               child: icon,
     581              :             ),
     582            4 :             SizedBox(width: theme.spacing(2)),
     583              :           ],
     584            8 :           Expanded(
     585            8 :             child: Text(
     586           16 :               item.label,
     587              :               maxLines: 1,
     588              :               overflow: TextOverflow.ellipsis,
     589           24 :               style: visuals.textStyle.copyWith(color: textColor),
     590              :             ),
     591              :           ),
     592              :         ],
     593              :       ),
     594              :     );
     595              : 
     596            8 :     row = ConstrainedBox(
     597              :       constraints: const BoxConstraints(minHeight: _rowMinHeight),
     598            8 :       child: DecoratedBox(
     599            8 :         decoration: ShapeDecoration(
     600              :           color: fill,
     601            8 :           shape: RoundedSuperellipseBorder(
     602           24 :             borderRadius: BorderRadius.circular(visuals.rowRadius),
     603              :           ),
     604              :         ),
     605            8 :         child: Align(heightFactor: 1, child: row),
     606              :       ),
     607              :     );
     608              : 
     609            1 :     if (!enabled) row = Opacity(opacity: _disabledOpacity, child: row);
     610              : 
     611              :     // One merged node per row, so assistive tech announces the option once with
     612              :     // its selected state, not the label and control as separate nodes.
     613           16 :     final isCheckbox = indicator == _SelectIndicator.checkbox;
     614            8 :     return MergeSemantics(
     615            8 :       child: Semantics(
     616              :         role: isCheckbox
     617              :             ? SemanticsRole.menuItemCheckbox
     618              :             : SemanticsRole.menuItem,
     619              :         button: true,
     620              :         // A menu-item-checkbox must be checkable (report checked state); a
     621              :         // plain menu item reports selection instead.
     622            2 :         checked: isCheckbox ? selected : null,
     623            7 :         selected: isCheckbox ? null : selected,
     624              :         enabled: enabled,
     625            8 :         child: MouseRegion(
     626              :           cursor: enabled ? SystemMouseCursors.click : SystemMouseCursors.basic,
     627            3 :           onEnter: enabled ? (_) => onEnter() : null,
     628            8 :           child: GestureDetector(
     629              :             behavior: HitTestBehavior.opaque,
     630            8 :             onTap: enabled ? onTap : null,
     631              :             child: row,
     632              :           ),
     633              :         ),
     634              :       ),
     635              :     );
     636              :   }
     637              : 
     638            8 :   Widget _indicatorWidget(Color color) {
     639            8 :     switch (indicator) {
     640            8 :       case _SelectIndicator.checkmark:
     641            7 :         if (!selected) return const SizedBox.shrink();
     642            2 :         return FossGlyphIcon(CheckGlyph(color));
     643            2 :       case _SelectIndicator.checkbox:
     644            4 :         final colors = theme.colors;
     645            2 :         return DecoratedBox(
     646            2 :           decoration: ShapeDecoration(
     647            3 :             color: selected ? colors.primary : null,
     648            2 :             shape: RoundedSuperellipseBorder(
     649            2 :               side: selected
     650              :                   ? BorderSide.none
     651            4 :                   : BorderSide(color: colors.input),
     652            8 :               borderRadius: BorderRadius.circular(theme.radii.sm),
     653              :             ),
     654              :           ),
     655            2 :           child: selected
     656            1 :               ? Padding(
     657              :                   padding: const EdgeInsets.all(2),
     658            3 :                   child: FossGlyphIcon(CheckGlyph(colors.primaryForeground)),
     659              :                 )
     660              :               : null,
     661              :         );
     662              :     }
     663              :   }
     664              : }
     665              : 
     666              : /// Positions the popup below the anchor, flipping above when there is no room,
     667              : /// and clamping its height to the viewport.
     668              : class _PopupLayout extends SingleChildLayoutDelegate {
     669            8 :   const _PopupLayout({
     670              :     required this.anchor,
     671              :     required this.bottomInset,
     672              :     required this.textDirection,
     673              :   });
     674              : 
     675              :   final Rect anchor;
     676              : 
     677              :   /// Height taken by the on-screen keyboard at the bottom of the overlay.
     678              :   final double bottomInset;
     679              : 
     680              :   /// The reading direction, so a popup narrower than its anchor hugs the start
     681              :   /// edge rather than the physical left.
     682              :   final TextDirection textDirection;
     683              : 
     684            8 :   @override
     685              :   BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
     686           24 :     final viewportBottom = constraints.maxHeight - bottomInset;
     687           32 :     final below = viewportBottom - anchor.bottom - _popupOffset;
     688           24 :     final above = anchor.top - _popupOffset;
     689           24 :     final maxHeight = (math.max(below, above) - _popupMargin).clamp(
     690              :       0.0,
     691            8 :       constraints.maxHeight,
     692              :     );
     693              :     // Match the anchor width, capped to the viewport minus a margin on each
     694              :     // side, so the popup keeps symmetric insets and never runs off the edge.
     695            8 :     final width = math.min(
     696           16 :       anchor.width,
     697           24 :       constraints.maxWidth - _popupMargin * 2,
     698              :     );
     699            8 :     return BoxConstraints(
     700              :       minWidth: width,
     701              :       maxWidth: width,
     702              :       maxHeight: maxHeight,
     703              :     );
     704              :   }
     705              : 
     706            8 :   @override
     707              :   Offset getPositionForChild(Size size, Size childSize) {
     708           56 :     final below = (size.height - bottomInset) - anchor.bottom - _popupOffset;
     709           16 :     final placeBelow = childSize.height <= below;
     710              :     final dy = placeBelow
     711           24 :         ? anchor.bottom + _popupOffset
     712            5 :         : anchor.top - _popupOffset - childSize.height;
     713              :     // Align the popup's start edge to the anchor's start edge: the anchor left
     714              :     // under LTR, the anchor right under RTL.
     715           16 :     final startX = textDirection == TextDirection.ltr
     716           14 :         ? anchor.left
     717            4 :         : anchor.right - childSize.width;
     718              :     final dx = startX
     719            8 :         .clamp(
     720              :           _popupMargin,
     721           40 :           math.max(_popupMargin, size.width - _popupMargin - childSize.width),
     722              :         )
     723            8 :         .toDouble();
     724            8 :     return Offset(dx, dy);
     725              :   }
     726              : 
     727            6 :   @override
     728              :   bool shouldRelayout(_PopupLayout oldDelegate) =>
     729           18 :       oldDelegate.anchor != anchor ||
     730           18 :       oldDelegate.bottomInset != bottomInset ||
     731           18 :       oldDelegate.textDirection != textDirection;
     732              : }
     733              : 
     734              : /// Paints a 1px rim inside the trigger, top-lit in dark mode, fading to nothing
     735              : /// by the center.
     736              : class _RimPainter extends CustomPainter {
     737            8 :   const _RimPainter({
     738              :     required this.color,
     739              :     required this.radius,
     740              :     required this.topLit,
     741              :   });
     742              : 
     743              :   final Color color;
     744              :   final double radius;
     745              :   final bool topLit;
     746              : 
     747            8 :   @override
     748              :   void paint(Canvas canvas, Size size) {
     749           16 :     final rect = (Offset.zero & size).deflate(0.5);
     750            8 :     final shader = LinearGradient(
     751            8 :       begin: topLit ? Alignment.topCenter : Alignment.bottomCenter,
     752              :       end: Alignment.center,
     753           32 :       colors: [color, color.withValues(alpha: 0)],
     754            8 :     ).createShader(rect);
     755            8 :     final paint = Paint()
     756            8 :       ..shader = shader
     757            8 :       ..style = PaintingStyle.stroke
     758            8 :       ..strokeWidth = 1;
     759            8 :     canvas.drawPath(
     760            8 :       RoundedSuperellipseBorder(
     761           16 :         borderRadius: BorderRadius.circular(radius),
     762            8 :       ).getOuterPath(rect),
     763              :       paint,
     764              :     );
     765              :   }
     766              : 
     767            6 :   @override
     768              :   bool shouldRepaint(_RimPainter old) =>
     769           54 :       old.color != color || old.topLit != topLit || old.radius != radius;
     770              : }
     771              : 
     772              : /// Paints the focus ring: a rounded rect outset just past the trigger edge with
     773              : /// a 1px gap filled by the surface, so the ring reads as detached.
     774              : class _RectRingPainter extends CustomPainter {
     775            5 :   const _RectRingPainter({
     776              :     required this.color,
     777              :     required this.offsetColor,
     778              :     required this.radius,
     779              :   });
     780              : 
     781              :   final Color color;
     782              :   final Color offsetColor;
     783              :   final double radius;
     784              : 
     785            5 :   @override
     786              :   void paint(Canvas canvas, Size size) {
     787            5 :     final box = Offset.zero & size;
     788              :     canvas
     789            5 :       ..drawPath(
     790            5 :         RoundedSuperellipseBorder(
     791           20 :           borderRadius: BorderRadius.circular(radius + _ringOffset / 2),
     792           15 :         ).getOuterPath(box.inflate(_ringOffset / 2)),
     793            5 :         Paint()
     794           10 :           ..color = offsetColor
     795            5 :           ..style = PaintingStyle.stroke
     796            5 :           ..strokeWidth = _ringOffset,
     797              :       )
     798            5 :       ..drawPath(
     799            5 :         RoundedSuperellipseBorder(
     800            5 :           borderRadius: BorderRadius.circular(
     801           20 :             radius + _ringOffset + _ringWidth / 2,
     802              :           ),
     803           20 :         ).getOuterPath(box.inflate(_ringOffset + _ringWidth / 2)),
     804            5 :         Paint()
     805           10 :           ..color = color
     806            5 :           ..style = PaintingStyle.stroke
     807            5 :           ..strokeWidth = _ringWidth,
     808              :       );
     809              :   }
     810              : 
     811            2 :   @override
     812              :   bool shouldRepaint(_RectRingPainter old) =>
     813            6 :       old.color != color ||
     814            6 :       old.offsetColor != offsetColor ||
     815            6 :       old.radius != radius;
     816              : }
        

Generated by: LCOV version 2.0-1