Files
pipelines/app/lib/ui/adaptive/segmented_bar_overlay.dart

126 lines
3.6 KiB
Dart

import 'package:flutter/widgets.dart';
/// Floats [bar] over the top of [child], the way a scaffold with
/// `extendBodyBehindAppBar` does, for a bar that is not a scaffold's.
///
/// [child] fills the whole area and scrolls behind the bar; it learns the
/// bar's measured height (status-bar inset included) as its
/// [MediaQuery.paddingOf] top, so a list can pad its first item below the bar
/// at rest. [reserveSpace] instead gives reading content its own clipped
/// viewport below the bar. Taps the bar does not take reach [child].
class SegmentedBarOverlay extends StatelessWidget {
const SegmentedBarOverlay({
super.key,
required this.bar,
required this.child,
this.reserveSpace = false,
});
final Widget bar;
final Widget child;
/// Gives reading content its own viewport below the measured bar.
final bool reserveSpace;
static double reservedHeightOf(BuildContext context) =>
context
.dependOnInheritedWidgetOfExactType<_ReservedBarHeight>()
?.height ??
0;
@override
Widget build(BuildContext context) => CustomMultiChildLayout(
delegate: _OverlayLayout(reserveSpace),
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);
return MediaQuery(
data: media.copyWith(
padding: media.padding.copyWith(
top: reserveSpace ? 0 : barHeight,
),
),
child: _ReservedBarHeight(
height: reserveSpace ? barHeight : 0,
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(this.reserveSpace);
final bool reserveSpace;
@override
void performLayout(Size size) {
final bar = layoutChild(
_Slot.bar,
BoxConstraints(
maxWidth: size.width,
minWidth: size.width,
).copyWith(maxHeight: size.height),
);
positionChild(_Slot.bar, Offset.zero);
layoutChild(
_Slot.content,
_ContentConstraints(
Size(
size.width,
(size.height - (reserveSpace ? bar.height : 0)).clamp(0, size.height),
),
barHeight: bar.height,
),
);
positionChild(_Slot.content, Offset(0, reserveSpace ? bar.height : 0));
}
@override
bool shouldRelayout(_OverlayLayout oldDelegate) =>
reserveSpace != oldDelegate.reserveSpace;
}
class _ReservedBarHeight extends InheritedWidget {
const _ReservedBarHeight({required this.height, required super.child});
final double height;
@override
bool updateShouldNotify(_ReservedBarHeight oldWidget) =>
height != oldWidget.height;
}