261 lines
8.1 KiB
Dart
261 lines
8.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../motion/motion.dart';
|
|
|
|
/// The narrowest app that gets desktop popups: the width where a list shows
|
|
/// beside its open item (`kListDetailMinWidth`).
|
|
const double kDesktopPopupMinWidth = 600;
|
|
|
|
/// Whether popups here are desktop ones (a menu anchored to its button, a
|
|
/// dialog) rather than a phone's bottom sheets. It is the app's width that
|
|
/// decides, not the surface asking: content in a narrow floating window on a
|
|
/// wide app still gets menus and dialogs.
|
|
bool usesDesktopPopups(BuildContext context) {
|
|
final root = Navigator.maybeOf(context, rootNavigator: true)?.context;
|
|
return MediaQuery.sizeOf(root ?? context).width >= kDesktopPopupMinWidth;
|
|
}
|
|
|
|
/// Shows a short list of choices (a mode, a sort order, a pane to add, a
|
|
/// row's actions) built by [builder].
|
|
///
|
|
/// On a wide app it is a menu panel anchored to [anchor] (the button that
|
|
/// opened it): below it, or above when there is no room, kept inside the app.
|
|
/// When [anchor] is a region rather than a button (a pane), the panel opens
|
|
/// over its top left.
|
|
/// On a phone it is a bottom sheet. Either way it is on the root navigator,
|
|
/// so it is above every floating window. Escape, Back or a press outside
|
|
/// closes it; the content closes it with `Navigator.of(context).pop(value)`.
|
|
Future<T?> showAppChoiceMenu<T>(
|
|
BuildContext anchor, {
|
|
required WidgetBuilder builder,
|
|
double width = 320,
|
|
String? semanticLabel,
|
|
}) {
|
|
final navigator = Navigator.of(anchor, rootNavigator: true);
|
|
if (!usesDesktopPopups(anchor)) {
|
|
return showModalBottomSheet<T>(
|
|
context: anchor,
|
|
useRootNavigator: true,
|
|
showDragHandle: true,
|
|
isScrollControlled: true,
|
|
useSafeArea: true,
|
|
builder: builder,
|
|
);
|
|
}
|
|
final box = anchor.findRenderObject()! as RenderBox;
|
|
final overlay = navigator.overlay!.context.findRenderObject()! as RenderBox;
|
|
final rect = box.localToGlobal(Offset.zero, ancestor: overlay) & box.size;
|
|
return navigator.push<T>(
|
|
_AnchoredMenuRoute<T>(
|
|
anchor: rect,
|
|
width: width,
|
|
builder: builder,
|
|
label: semanticLabel ?? 'Menu',
|
|
themes: InheritedTheme.capture(from: anchor, to: navigator.context),
|
|
duration: Motion.of(anchor, Motion.quick),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// A choice row shared by the app's anchored choice menus. Keeping the row
|
|
/// here makes checked state and tap affordances consistent across selectors.
|
|
class AppChoiceMenuItem extends StatelessWidget {
|
|
const AppChoiceMenuItem({
|
|
super.key,
|
|
required this.child,
|
|
required this.selected,
|
|
required this.onTap,
|
|
this.contentPadding,
|
|
});
|
|
|
|
final Widget child;
|
|
final bool selected;
|
|
final VoidCallback? onTap;
|
|
final EdgeInsetsGeometry? contentPadding;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListTile(
|
|
enabled: onTap != null,
|
|
title: child,
|
|
contentPadding: contentPadding,
|
|
trailing: selected
|
|
? Icon(Icons.check, color: Theme.of(context).colorScheme.primary)
|
|
: null,
|
|
onTap: onTap,
|
|
);
|
|
}
|
|
|
|
/// Shows richer content (a chat's details, a file's details) built by
|
|
/// [builder]: a dialog on a wide app, a bottom sheet on a phone, on the root
|
|
/// navigator either way. The content sizes itself (a `Column` with
|
|
/// `MainAxisSize.min`); the dialog scrolls it when it is taller than the app.
|
|
Future<T?> showAppDetails<T>(
|
|
BuildContext context, {
|
|
required WidgetBuilder builder,
|
|
double maxWidth = 560,
|
|
}) {
|
|
if (!usesDesktopPopups(context)) {
|
|
return showModalBottomSheet<T>(
|
|
context: context,
|
|
useRootNavigator: true,
|
|
showDragHandle: true,
|
|
isScrollControlled: true,
|
|
useSafeArea: true,
|
|
builder: builder,
|
|
);
|
|
}
|
|
return showDialog<T>(
|
|
context: context,
|
|
useRootNavigator: true,
|
|
builder: (dialog) => Dialog(
|
|
clipBehavior: Clip.antiAlias,
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
maxWidth: maxWidth,
|
|
maxHeight: MediaQuery.sizeOf(dialog).height * 0.85,
|
|
),
|
|
// Content taller than the app scrolls inside the dialog.
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.only(top: 16),
|
|
child: builder(dialog),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
class _AnchoredMenuRoute<T> extends PopupRoute<T> {
|
|
_AnchoredMenuRoute({
|
|
required this.anchor,
|
|
required this.width,
|
|
required this.builder,
|
|
required this.label,
|
|
required this.themes,
|
|
required this.duration,
|
|
});
|
|
|
|
final Rect anchor;
|
|
final double width;
|
|
final WidgetBuilder builder;
|
|
final String label;
|
|
final CapturedThemes themes;
|
|
final Duration duration;
|
|
|
|
@override
|
|
Color? get barrierColor => null;
|
|
@override
|
|
bool get barrierDismissible => true;
|
|
@override
|
|
String? get barrierLabel => 'Dismiss';
|
|
@override
|
|
Duration get transitionDuration => duration;
|
|
|
|
@override
|
|
Widget buildPage(
|
|
BuildContext context,
|
|
Animation<double> animation,
|
|
Animation<double> secondaryAnimation,
|
|
) {
|
|
final colors = Theme.of(context).colorScheme;
|
|
return SafeArea(
|
|
child: CustomSingleChildLayout(
|
|
delegate: _AnchoredMenuLayout(anchor, width),
|
|
child: FadeTransition(
|
|
opacity: CurvedAnimation(parent: animation, curve: Motion.curve),
|
|
child: Semantics(
|
|
scopesRoute: true,
|
|
namesRoute: true,
|
|
explicitChildNodes: true,
|
|
label: label,
|
|
child: Material(
|
|
key: const ValueKey('app-choice-menu'),
|
|
elevation: 3,
|
|
// A step above a window's own surface, so the panel reads
|
|
// as raised when it opens over one.
|
|
color: colors.surfaceContainerHigh,
|
|
surfaceTintColor: Colors.transparent,
|
|
borderRadius: BorderRadius.circular(12),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: themes.wrap(Builder(builder: builder)),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Places the panel below its anchor, aligned to the anchor's leading edge,
|
|
/// or above it when it fits better there, and always inside the app.
|
|
class _AnchoredMenuLayout extends SingleChildLayoutDelegate {
|
|
_AnchoredMenuLayout(this.anchor, this.width);
|
|
|
|
final Rect anchor;
|
|
final double width;
|
|
|
|
static const double margin = 8;
|
|
static const double gap = 4;
|
|
|
|
/// Less room than this on both sides means the anchor is not a button but
|
|
/// a region (a pane's bar, a whole pane): the panel then opens over it,
|
|
/// from its top.
|
|
static const double minRoom = 200;
|
|
|
|
double _below(Size size) => size.height - anchor.bottom - gap - margin;
|
|
double _above() => anchor.top - gap - margin;
|
|
bool _over(Size size) => _below(size) < minRoom && _above() < minRoom;
|
|
|
|
@override
|
|
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
|
|
final available = constraints.biggest;
|
|
final below = _below(available);
|
|
final above = _above();
|
|
final double room;
|
|
if (_over(available)) {
|
|
room =
|
|
available.height -
|
|
anchor.top.clamp(margin, available.height) -
|
|
margin;
|
|
} else {
|
|
room = below >= minRoom || below >= above ? below : above;
|
|
}
|
|
final maxWidth = (available.width - 2 * margin).clamp(0.0, double.infinity);
|
|
return BoxConstraints(
|
|
minWidth: width.clamp(0.0, maxWidth),
|
|
maxWidth: width.clamp(0.0, maxWidth),
|
|
maxHeight: room.clamp(0.0, available.height - 2 * margin),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Offset getPositionForChild(Size size, Size childSize) {
|
|
final double top;
|
|
if (_over(size)) {
|
|
top = anchor.top + gap;
|
|
} else if (childSize.height <= _below(size)) {
|
|
top = anchor.bottom + gap;
|
|
} else {
|
|
top = anchor.top - gap - childSize.height;
|
|
}
|
|
final maxTop = (size.height - margin - childSize.height).clamp(
|
|
margin,
|
|
double.infinity,
|
|
);
|
|
final maxLeft = (size.width - margin - childSize.width).clamp(
|
|
margin,
|
|
double.infinity,
|
|
);
|
|
return Offset(
|
|
anchor.left.clamp(margin, maxLeft),
|
|
top.clamp(margin, maxTop),
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRelayout(_AnchoredMenuLayout oldDelegate) =>
|
|
oldDelegate.anchor != anchor || oldDelegate.width != width;
|
|
}
|