import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /// One choice of a [TerminalMenuButton]. class TerminalChoice { const TerminalChoice(this.value, this.label, {this.checked, this.key}); final T value; final String label; /// Non-null draws the choice as a checkable item. final bool? checked; final Key? key; } /// An icon button opening the host's menu: a Material popup menu, or a /// Cupertino action sheet with Cancel. class TerminalMenuButton extends StatelessWidget { const TerminalMenuButton({ super.key, required this.cupertino, required this.label, required this.icon, required this.cupertinoIcon, required this.choices, required this.onSelected, this.info, }); final bool cupertino; final String label; final IconData icon; final IconData cupertinoIcon; final List> choices; final ValueChanged onSelected; /// A note under the choices (the terminal's shortcuts), not selectable. final String? info; @override Widget build(BuildContext context) { if (!cupertino) { return PopupMenuButton( tooltip: label, icon: Icon(icon), onSelected: onSelected, itemBuilder: (_) => [ for (final choice in choices) choice.checked == null ? PopupMenuItem( key: choice.key, value: choice.value, child: Text(choice.label), ) : CheckedPopupMenuItem( key: choice.key, value: choice.value, checked: choice.checked!, child: Text(choice.label), ), if (info case final info?) ...[ const PopupMenuDivider(), PopupMenuItem( key: const ValueKey('terminal-menu-info'), enabled: false, child: Text(info), ), ], ], ); } return CupertinoButton( padding: const EdgeInsets.symmetric(horizontal: 8), onPressed: () => _sheet(context), child: Icon(cupertinoIcon, semanticLabel: label), ); } Future _sheet(BuildContext context) async { final picked = await showCupertinoModalPopup( context: context, builder: (sheet) => CupertinoActionSheet( title: Text(label), message: info == null ? null : Text(info!), actions: [ for (final choice in choices) CupertinoActionSheetAction( key: choice.key, isDefaultAction: choice.checked ?? false, onPressed: () => Navigator.of(sheet).pop(choice.value), child: Text( choice.checked ?? false ? '${choice.label} ✓' : choice.label, ), ), ], cancelButton: CupertinoActionSheetAction( onPressed: () => Navigator.of(sheet).pop(), child: const Text('Cancel'), ), ), ); if (picked != null) onSelected(picked); } }