Add adaptive popup helpers: anchored choice menus and details dialogs
Phase 2b.1 of docs/plans/floating-windows.md. showAppChoiceMenu is a menu panel anchored to its button on a wide app and a bottom sheet on a phone; showAppDetails is a dialog or a bottom sheet. Both are on the root navigator, and the app's width decides, so content in a narrow floating window still gets desktop popups. The probe window exercises both.
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
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.
|
||||
/// 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),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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,
|
||||
),
|
||||
child: Padding(
|
||||
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;
|
||||
|
||||
@override
|
||||
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
|
||||
final available = constraints.biggest;
|
||||
final below = available.height - anchor.bottom - gap - margin;
|
||||
final above = anchor.top - gap - margin;
|
||||
final height = (below >= 240 || below >= above ? below : above).clamp(
|
||||
0.0,
|
||||
available.height - 2 * margin,
|
||||
);
|
||||
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: height,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Offset getPositionForChild(Size size, Size childSize) {
|
||||
final below = size.height - anchor.bottom - gap - margin;
|
||||
final fitsBelow = childSize.height <= below;
|
||||
final top = fitsBelow
|
||||
? anchor.bottom + gap
|
||||
: (anchor.top - gap - childSize.height).clamp(
|
||||
margin,
|
||||
size.height - margin - childSize.height,
|
||||
);
|
||||
final left = anchor.left.clamp(
|
||||
margin,
|
||||
(size.width - margin - childSize.width).clamp(margin, double.infinity),
|
||||
);
|
||||
return Offset(left, top);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRelayout(_AnchoredMenuLayout oldDelegate) =>
|
||||
oldDelegate.anchor != anchor || oldDelegate.width != width;
|
||||
}
|
||||
Reference in New Issue
Block a user