import 'package:flutter/material.dart'; /// One action in an [showAppActionSheet] sheet. class AppSheetAction { const AppSheetAction({ required this.label, this.icon, this.onSelected, this.destructive = false, this.enabled = true, this.selected = false, this.choices = const [], this.key, }); final String label; final IconData? icon; /// Runs once the sheet has closed. Null (with no [choices]) disables it. final VoidCallback? onSelected; /// Shown last, after a divider, in the theme's error colour. final bool destructive; final bool enabled; /// Marks the current choice in a [choices] list. final bool selected; /// Choices offered on a second sheet (a sort order) instead of [onSelected]. final List choices; /// Placed on the row so tests can find it. final Key? key; } /// A phone's menu: a modal bottom sheet with a header naming what the actions /// apply to ([title], [subtitle]) and one full-width row per action, each with /// its icon. Destructive actions come last, after a divider, in the error /// colour. The chosen action runs after the sheet has closed. Future showAppActionSheet( BuildContext context, { String? title, String? subtitle, required List actions, }) async { final chosen = await showModalBottomSheet( context: context, useRootNavigator: true, showDragHandle: true, isScrollControlled: true, useSafeArea: true, builder: (sheet) => _ActionSheet(title: title, subtitle: subtitle, actions: actions), ); if (chosen == null) return; if (chosen.choices.isNotEmpty) { if (!context.mounted) return; return showAppActionSheet( context, title: chosen.label, actions: chosen.choices, ); } chosen.onSelected?.call(); } class _ActionSheet extends StatelessWidget { const _ActionSheet({ required this.title, required this.subtitle, required this.actions, }); final String? title; final String? subtitle; final List actions; @override Widget build(BuildContext context) { final theme = Theme.of(context); final regular = [ for (final action in actions) if (!action.destructive) action, ]; final destructive = [ for (final action in actions) if (action.destructive) action, ]; final hasTitle = title != null && title!.trim().isNotEmpty; return SafeArea( top: false, child: SingleChildScrollView( padding: const EdgeInsets.only(bottom: 8), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (hasTitle) ...[ Padding( padding: const EdgeInsets.fromLTRB(24, 0, 24, 12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title!, style: theme.textTheme.titleMedium, maxLines: 2, overflow: TextOverflow.ellipsis, ), if (subtitle != null && subtitle!.trim().isNotEmpty) ...[ const SizedBox(height: 2), Text( subtitle!, style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurfaceVariant, ), maxLines: 1, overflow: TextOverflow.ellipsis, ), ], ], ), ), const Divider(height: 1), ], const SizedBox(height: 8), for (final action in regular) _ActionRow(action: action), if (destructive.isNotEmpty) ...[ if (regular.isNotEmpty) const Divider(height: 17, indent: 24, endIndent: 24), for (final action in destructive) _ActionRow(action: action), ], ], ), ), ); } } class _ActionRow extends StatelessWidget { const _ActionRow({required this.action}); final AppSheetAction action; @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; final enabled = action.enabled && (action.onSelected != null || action.choices.isNotEmpty); final color = action.destructive ? scheme.error : null; return ListTile( key: action.key, enabled: enabled, minTileHeight: 56, contentPadding: const EdgeInsets.symmetric(horizontal: 24), leading: action.icon == null ? null : Icon(action.icon, color: enabled ? color : null), iconColor: scheme.onSurfaceVariant, title: Text( action.label, style: color == null || !enabled ? null : TextStyle(color: color), ), trailing: action.choices.isNotEmpty ? const Icon(Icons.chevron_right) : action.selected ? Icon(Icons.check, color: scheme.primary) : null, selected: action.selected, onTap: enabled ? () => Navigator.of(context).pop(action) : null, ); } }