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. Taps the bar does not take reach [child]. class SegmentedBarOverlay extends StatelessWidget { const SegmentedBarOverlay({ super.key, required this.bar, required this.child, }); final Widget bar; final Widget child; @override Widget build(BuildContext context) => CustomMultiChildLayout( delegate: _OverlayLayout(), 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: barHeight), ), 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 { @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, barHeight: bar.height), ); positionChild(_Slot.content, Offset.zero); } @override bool shouldRelayout(_OverlayLayout oldDelegate) => false; }