import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; /// One choice in an [AppOverflowMenu]. class AppMenuAction { const AppMenuAction({ required this.value, required this.label, this.key, this.destructive = false, }); final T value; final String label; /// Placed on the menu item (Material) or the sheet action (Cupertino), so a /// test finds the same item on both hosts. final Key? key; /// Shown in the destructive colour on Cupertino's action sheet. final bool destructive; } /// The app's one row/header overflow control: the host's "more" icon that /// opens the row's secondary actions. /// /// Material shows a [PopupMenuButton] with [Icons.more_vert]; Cupertino shows /// a [CupertinoButton] with [CupertinoIcons.ellipsis] that opens a /// [CupertinoActionSheet] (with Cancel). Every list row and the run header use /// this, so the overflow icon reads the same everywhere on a host. class AppOverflowMenu extends StatelessWidget { const AppOverflowMenu({ super.key, required this.actions, required this.onSelected, this.label = 'More actions', this.sheetTitle, this.enabled = true, this.cupertino, }); final List> actions; /// Called with the picked value once the menu or sheet has closed. final void Function(BuildContext context, T value) onSelected; /// Tooltip (Material) and accessible label (both hosts). final String label; /// The Cupertino sheet's title, usually the row's name. final String? sheetTitle; final bool enabled; /// Overrides the host detection (some features pick a style per platform). final bool? cupertino; @override Widget build(BuildContext context) { if (actions.isEmpty) return const SizedBox.shrink(); if (!(cupertino ?? usesCupertinoHost(context))) { return PopupMenuButton( icon: const Icon(Icons.more_vert), tooltip: label, enabled: enabled, onSelected: (value) => onSelected(context, value), itemBuilder: (_) => [ for (final action in actions) PopupMenuItem( key: action.key, value: action.value, child: Text(action.label), ), ], ); } // Its own node, so a row's merged label never swallows the button. return Semantics( container: true, button: true, enabled: enabled, label: label, excludeSemantics: true, child: CupertinoButton( padding: EdgeInsets.zero, minimumSize: const Size.square(kMinInteractiveDimensionCupertino), onPressed: enabled ? () => _showSheet(context) : null, child: const Icon(CupertinoIcons.ellipsis), ), ); } Future _showSheet(BuildContext context) async { final picked = await showCupertinoModalPopup( context: context, builder: (sheet) => CupertinoActionSheet( title: sheetTitle == null ? null : Text(sheetTitle!), actions: [ for (final action in actions) CupertinoActionSheetAction( key: action.key, isDestructiveAction: action.destructive, onPressed: () => Navigator.of(sheet).pop(action.value), child: Text(action.label), ), ], cancelButton: CupertinoActionSheetAction( isDefaultAction: true, onPressed: () => Navigator.of(sheet).pop(), child: const Text('Cancel'), ), ), ); if (picked != null && context.mounted) onSelected(context, picked); } }