Files
pipelines/app/lib/ui/adaptive/floating_bar_layout.dart
T

273 lines
9.3 KiB
Dart

import 'package:flutter/widgets.dart';
/// At least this wide, a floating tab bar is sized to its tabs and sits at the
/// trailing edge; narrower, it spans the width minus [kFloatingBarMargin].
const double kFloatingBarWideWidth = 600;
/// The gap between a floating tab bar and the edges (and bottom safe area).
const double kFloatingBarMargin = 16;
/// [padding] with the floating bar's clearance added at the bottom.
///
/// A scrollable under a floating bar (see [FloatingBarOverlay] and the shell)
/// reads the bar's height as [MediaQuery.paddingOf] bottom; adding it to the
/// list's own padding lets the content scroll behind the bar yet still bring
/// its last item clear of it at rest. Lists without explicit padding already
/// do this themselves.
EdgeInsetsGeometry withFloatingBarClearance(
BuildContext context,
EdgeInsetsGeometry padding,
) => padding.add(EdgeInsets.only(bottom: MediaQuery.paddingOf(context).bottom));
/// [padding] with the top bar's clearance added at the top.
///
/// A page whose top bar floats over its content (a `SegmentedBar` in a
/// scaffold with `extendBodyBehindAppBar`, a `SegmentedBarOverlay`, the
/// shell's bar) reads the bar's height as [MediaQuery.paddingOf] top. Adding
/// it to a list's own padding starts the first item below the bar at rest
/// while scrolled content passes behind the glass pieces. Lists without
/// explicit padding (`ListView`'s default) already do this themselves.
EdgeInsetsGeometry withTopBarClearance(
BuildContext context,
EdgeInsetsGeometry padding,
) => padding.add(EdgeInsets.only(top: MediaQuery.paddingOf(context).top));
/// [padding] with both the top bar's and the floating tab bar's clearance:
/// for a list that scrolls behind a bar at each edge.
EdgeInsetsGeometry withBarClearance(
BuildContext context,
EdgeInsetsGeometry padding,
) {
final media = MediaQuery.paddingOf(context);
return padding.add(EdgeInsets.only(top: media.top, bottom: media.bottom));
}
/// Empty space as tall as the top bar's clearance ([MediaQuery.paddingOf]
/// top), starting a scrolling column so its first item sits below the bar at
/// rest and scrolls behind it.
class TopBarClearance extends StatelessWidget {
const TopBarClearance({super.key});
@override
Widget build(BuildContext context) =>
SizedBox(height: MediaQuery.paddingOf(context).top);
}
/// [TopBarClearance] as the first sliver of a [CustomScrollView]: the first
/// item starts below the bar at rest and scrolls behind it.
///
/// The clearance stays pinned (it paints nothing and takes no taps), so a
/// pinned header further down (`PinnedHeaderSliver`, a sticky section title)
/// pins just below the bar rather than behind it.
class SliverTopBarClearance extends StatelessWidget {
const SliverTopBarClearance({super.key});
@override
Widget build(BuildContext context) =>
const PinnedHeaderSliver(child: TopBarClearance());
}
/// The top bar's height over the content at [context]: where a pinned
/// header must stop so it stays below the bar.
double topBarInset(BuildContext context) => MediaQuery.paddingOf(context).top;
/// Content that cannot scroll behind the top bar (a fixed header, a canvas
/// with its own overlay controls): it starts below the bar, and its subtree no
/// longer sees the bar's height as padding.
class BelowTopBar extends StatelessWidget {
const BelowTopBar({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) => Padding(
padding: EdgeInsets.only(top: MediaQuery.paddingOf(context).top),
child: MediaQuery.removePadding(
context: context,
removeTop: true,
child: child,
),
);
}
/// Content that must not run behind the floating bar (a [FloatingBarOverlay]'s
/// bar, such as a composer): it ends at the bar's top edge, so the viewport
/// shrinks as the bar grows, and its subtree no longer sees the bar's height
/// as padding.
class AboveFloatingBar extends StatelessWidget {
const AboveFloatingBar({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) => Padding(
padding: EdgeInsets.only(bottom: MediaQuery.paddingOf(context).bottom),
child: MediaQuery.removePadding(
context: context,
removeBottom: true,
child: child,
),
);
}
/// Empty space as tall as the floating bar's clearance
/// ([MediaQuery.paddingOf] bottom), ending a scrolling column so its last item
/// comes clear of the bar at rest.
class FloatingBarClearance extends StatelessWidget {
const FloatingBarClearance({super.key});
@override
Widget build(BuildContext context) =>
SizedBox(height: MediaQuery.paddingOf(context).bottom);
}
/// [FloatingBarClearance] as the last sliver of a [CustomScrollView].
class SliverFloatingBarClearance extends StatelessWidget {
const SliverFloatingBarClearance({super.key});
@override
Widget build(BuildContext context) =>
const SliverToBoxAdapter(child: FloatingBarClearance());
}
/// The edge a page's own floating tab bar sits at.
enum FloatingBarEdge {
/// Above the bottom safe area: a phone page, a page covering the shell,
/// or a detail pane beside a list (the shell's tab bar stays over the
/// list pane).
bottom,
/// Just below the page's top bar, for a surface whose bottom is taken.
top,
}
/// Where the pages' own tab bars float in this subtree ([FloatingBarEdge]).
///
/// Pages float their tabs at the bottom (the default), including a detail
/// pane beside a list, whose bottom the shell leaves free.
class PageTabsEdge extends InheritedWidget {
const PageTabsEdge({super.key, required this.edge, required super.child});
final FloatingBarEdge edge;
static FloatingBarEdge of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<PageTabsEdge>()?.edge ??
FloatingBarEdge.bottom;
@override
bool updateShouldNotify(PageTabsEdge oldWidget) => edge != oldWidget.edge;
}
/// Floats [bar] over [child] at [edge], the way a scaffold with `extendBody`
/// floats its bottom bar, for a bar that is not a scaffold's.
///
/// [child] fills the whole area and scrolls behind the bar. At the bottom it
/// learns the bar's measured height (bottom safe area and margins included)
/// as its [MediaQuery.paddingOf] bottom, so a list can pad its last item clear
/// of the bar at rest (see [withFloatingBarClearance]). At the top the bar
/// sits below the area's own top inset (the page's top bar) and the content's
/// top padding grows by the bar's height (see [withTopBarClearance]). Taps the
/// bar does not take, such as beside a bar narrower than the area, reach
/// [child].
class FloatingBarOverlay extends StatelessWidget {
const FloatingBarOverlay({
super.key,
required this.bar,
required this.child,
this.edge = FloatingBarEdge.bottom,
});
final Widget bar;
final Widget child;
final FloatingBarEdge edge;
@override
Widget build(BuildContext context) {
final top = edge == FloatingBarEdge.top;
final inset = top ? MediaQuery.paddingOf(context).top : 0.0;
return CustomMultiChildLayout(
delegate: _OverlayLayout(top: top, inset: inset),
children: [
// Painted first, so the bar draws (and takes taps) above it.
LayoutId(
id: _Slot.content,
child: LayoutBuilder(
builder: (context, constraints) {
final barHeight = constraints is _ContentConstraints
? constraints.barHeight
: 0.0;
final media = MediaQuery.of(context);
final padding = top
? media.padding.copyWith(top: inset + barHeight)
: media.padding.copyWith(
bottom: barHeight > media.padding.bottom
? barHeight
: media.padding.bottom,
);
return MediaQuery(
data: media.copyWith(padding: padding),
child: child,
);
},
),
),
LayoutId(id: _Slot.bar, child: bar),
],
);
}
}
enum _Slot { content, bar }
/// The content's tight constraints, carrying the bar's height so the content
/// rebuilds its inset when the bar grows or shrinks.
class _ContentConstraints extends BoxConstraints {
_ContentConstraints(Size size, {required this.barHeight})
: super(
minWidth: size.width,
maxWidth: size.width,
minHeight: size.height,
maxHeight: size.height,
);
final double barHeight;
@override
bool operator ==(Object other) =>
other is _ContentConstraints &&
super == other &&
other.barHeight == barHeight;
@override
int get hashCode => Object.hash(super.hashCode, barHeight);
}
class _OverlayLayout extends MultiChildLayoutDelegate {
_OverlayLayout({required this.top, required this.inset});
final bool top;
final double inset;
@override
void performLayout(Size size) {
final bar = layoutChild(
_Slot.bar,
BoxConstraints(
minWidth: size.width,
maxWidth: size.width,
maxHeight: size.height,
),
);
positionChild(_Slot.bar, Offset(0, top ? inset : size.height - bar.height));
layoutChild(
_Slot.content,
_ContentConstraints(size, barHeight: bar.height),
);
positionChild(_Slot.content, Offset.zero);
}
@override
bool shouldRelayout(_OverlayLayout oldDelegate) =>
oldDelegate.top != top || oldDelegate.inset != inset;
}