Use proper phone sheets for chat and Files menus
On phones the chat's More menu, chat list rows and Files entries open a Material bottom sheet headed by the chat or file, with full-width icon rows and destructive actions last in the error colour; desktop menus show icons, a minimum width and destructive actions after a divider. The chat menu drops Run command and Open chats, keeps a single terminal entry and lists Close pane last.
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
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<AppSheetAction> 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<void> showAppActionSheet(
|
||||
BuildContext context, {
|
||||
String? title,
|
||||
String? subtitle,
|
||||
required List<AppSheetAction> actions,
|
||||
}) async {
|
||||
final chosen = await showModalBottomSheet<AppSheetAction>(
|
||||
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<AppSheetAction> 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import 'app_action_sheet.dart';
|
||||
import 'app_menu_anchor.dart';
|
||||
import 'app_popups.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// The key of a [SegmentedBar]'s More button, shown when its actions do not
|
||||
@@ -27,11 +29,16 @@ class SegmentedOverflowEntry {
|
||||
this.onPressed,
|
||||
this.choices = const [],
|
||||
this.shortcut,
|
||||
this.destructive = false,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final IconData icon;
|
||||
|
||||
/// Closes or removes something (Close pane): listed last on a phone sheet,
|
||||
/// after a divider, in the error colour.
|
||||
final bool destructive;
|
||||
|
||||
/// What choosing the entry does; null (with no [choices]) disables it.
|
||||
final VoidCallback? onPressed;
|
||||
|
||||
@@ -68,16 +75,55 @@ class SegmentedOverflowAction extends StatelessWidget
|
||||
}
|
||||
|
||||
/// The bar's More button: the host's overflow icon at the platform's touch
|
||||
/// target, opening a Material menu of [entries].
|
||||
/// target, opening a Material menu of [entries] on a wide app and a bottom
|
||||
/// sheet headed by [sheetTitle] on a phone.
|
||||
class SegmentedMoreButton extends StatelessWidget {
|
||||
const SegmentedMoreButton({super.key, required this.entries});
|
||||
const SegmentedMoreButton({
|
||||
super.key,
|
||||
required this.entries,
|
||||
this.sheetTitle,
|
||||
this.sheetSubtitle,
|
||||
});
|
||||
|
||||
final List<SegmentedOverflowEntry> entries;
|
||||
|
||||
/// The phone sheet's header: what the actions apply to (a chat).
|
||||
final String? sheetTitle;
|
||||
final String? sheetSubtitle;
|
||||
|
||||
static const String label = 'More actions';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!usesDesktopPopups(context)) {
|
||||
return IconButton(
|
||||
key: segmentedMoreKey,
|
||||
tooltip: label,
|
||||
icon: const Icon(Icons.more_vert),
|
||||
onPressed: () => showAppActionSheet(
|
||||
context,
|
||||
title: sheetTitle,
|
||||
subtitle: sheetSubtitle,
|
||||
actions: [
|
||||
for (final entry in entries)
|
||||
AppSheetAction(
|
||||
label: entry.label,
|
||||
icon: entry.icon,
|
||||
onSelected: entry.onPressed,
|
||||
destructive: entry.destructive,
|
||||
choices: [
|
||||
for (final choice in entry.choices)
|
||||
AppSheetAction(
|
||||
label: choice.label,
|
||||
selected: choice.selected,
|
||||
onSelected: choice.onSelected,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return AppMenuAnchor(
|
||||
menuChildren: [
|
||||
for (final entry in entries)
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../adaptive/app_action_sheet.dart';
|
||||
import '../adaptive/app_popups.dart';
|
||||
|
||||
/// One choice in an [AppOverflowMenu].
|
||||
class AppMenuAction<T> {
|
||||
const AppMenuAction({
|
||||
required this.value,
|
||||
required this.label,
|
||||
this.icon,
|
||||
this.key,
|
||||
this.destructive = false,
|
||||
this.enabled = true,
|
||||
@@ -13,22 +17,25 @@ class AppMenuAction<T> {
|
||||
final T value;
|
||||
final String label;
|
||||
|
||||
/// The action's glyph, beside its label in the menu and the phone sheet.
|
||||
final IconData? icon;
|
||||
|
||||
/// Placed on the menu item so tests can find the same item.
|
||||
final Key? key;
|
||||
|
||||
/// A disabled item stays listed but cannot be chosen.
|
||||
final bool enabled;
|
||||
|
||||
// Kept for callers that distinguish destructive actions in their data model;
|
||||
// Material PopupMenuItem does not apply a separate action-sheet treatment.
|
||||
/// Listed last, after a divider, in the theme's error colour.
|
||||
final bool destructive;
|
||||
}
|
||||
|
||||
/// The app's one row/header overflow control: the host's "more" icon that
|
||||
/// opens the row's secondary actions.
|
||||
///
|
||||
/// Shows a [PopupMenuButton] with [Icons.more_vert]. Every list row and the run
|
||||
/// header use this, so overflow actions are anchored to their button.
|
||||
/// On a wide app it is a menu anchored to its button, with each action's icon;
|
||||
/// on a phone it opens a bottom sheet headed by [sheetTitle], with full-width
|
||||
/// rows (see [showAppActionSheet]). Destructive actions come last either way.
|
||||
class AppOverflowMenu<T> extends StatelessWidget {
|
||||
const AppOverflowMenu({
|
||||
super.key,
|
||||
@@ -37,6 +44,7 @@ class AppOverflowMenu<T> extends StatelessWidget {
|
||||
required this.onSelected,
|
||||
this.label = 'More actions',
|
||||
this.sheetTitle,
|
||||
this.sheetSubtitle,
|
||||
this.enabled = true,
|
||||
this.icon = const Icon(Icons.more_vert),
|
||||
});
|
||||
@@ -53,20 +61,60 @@ class AppOverflowMenu<T> extends StatelessWidget {
|
||||
/// Tooltip and accessible label.
|
||||
final String label;
|
||||
|
||||
/// Retained for source compatibility; Material menus do not show a title.
|
||||
/// The phone sheet's header: what the actions apply to (a chat, a file).
|
||||
final String? sheetTitle;
|
||||
|
||||
/// A second header line under [sheetTitle] (a file's size, a folder).
|
||||
final String? sheetSubtitle;
|
||||
|
||||
final bool enabled;
|
||||
|
||||
/// The button glyph. Most callers use the default overflow icon; controls
|
||||
/// whose action has a distinct meaning can provide a matching Material icon.
|
||||
final Widget icon;
|
||||
|
||||
List<AppMenuAction<T>> _ordered() {
|
||||
final all = actionsBuilder?.call() ?? actions;
|
||||
return [
|
||||
for (final action in all)
|
||||
if (!action.destructive) action,
|
||||
for (final action in all)
|
||||
if (action.destructive) action,
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (actionsBuilder == null && actions.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
if (!usesDesktopPopups(context)) {
|
||||
return IconButton(
|
||||
icon: icon,
|
||||
tooltip: label,
|
||||
onPressed: !enabled
|
||||
? null
|
||||
: () => showAppActionSheet(
|
||||
context,
|
||||
title: sheetTitle,
|
||||
subtitle: sheetSubtitle,
|
||||
actions: [
|
||||
for (final action in _ordered())
|
||||
AppSheetAction(
|
||||
key: action.key,
|
||||
label: action.label,
|
||||
icon: action.icon,
|
||||
destructive: action.destructive,
|
||||
enabled: action.enabled,
|
||||
onSelected: () {
|
||||
if (context.mounted) onSelected(context, action.value);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Localizations.override(
|
||||
context: context,
|
||||
delegates: const [DefaultMaterialLocalizations.delegate],
|
||||
@@ -76,18 +124,60 @@ class AppOverflowMenu<T> extends StatelessWidget {
|
||||
icon: icon,
|
||||
tooltip: label,
|
||||
enabled: enabled,
|
||||
constraints: const BoxConstraints(minWidth: 220),
|
||||
onSelected: (value) => onSelected(context, value),
|
||||
itemBuilder: (_) => [
|
||||
for (final action in actionsBuilder?.call() ?? actions)
|
||||
PopupMenuItem<T>(
|
||||
key: action.key,
|
||||
value: action.value,
|
||||
enabled: action.enabled,
|
||||
child: Text(action.label),
|
||||
),
|
||||
],
|
||||
itemBuilder: (_) {
|
||||
final ordered = _ordered();
|
||||
final firstDestructive = ordered.indexWhere((a) => a.destructive);
|
||||
return [
|
||||
for (final (index, action) in ordered.indexed) ...[
|
||||
if (index == firstDestructive && index > 0)
|
||||
const PopupMenuDivider(),
|
||||
PopupMenuItem<T>(
|
||||
key: action.key,
|
||||
value: action.value,
|
||||
enabled: action.enabled,
|
||||
child: _MenuRow(
|
||||
label: action.label,
|
||||
icon: action.icon,
|
||||
color: action.destructive && action.enabled
|
||||
? scheme.error
|
||||
: null,
|
||||
),
|
||||
),
|
||||
],
|
||||
];
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MenuRow extends StatelessWidget {
|
||||
const _MenuRow({required this.label, this.icon, this.color});
|
||||
|
||||
final String label;
|
||||
final IconData? icon;
|
||||
final Color? color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final text = Text(
|
||||
label,
|
||||
style: color == null ? null : TextStyle(color: color),
|
||||
);
|
||||
if (icon == null) return text;
|
||||
return Row(
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 20,
|
||||
color: color ?? Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Flexible(child: text),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user