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)); /// 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()); } /// Floats [bar] over the bottom of [child], 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; 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]). 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}); 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( bottom: barHeight > media.padding.bottom ? barHeight : media.padding.bottom, ), ), 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( minWidth: size.width, maxWidth: size.width, maxHeight: size.height, ), ); positionChild(_Slot.bar, Offset(0, size.height - bar.height)); layoutChild( _Slot.content, _ContentConstraints(size, barHeight: bar.height), ); positionChild(_Slot.content, Offset.zero); } @override bool shouldRelayout(_OverlayLayout oldDelegate) => false; }