import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../ui/adaptive/floating_bar_layout.dart'; import '../ui/adaptive/host.dart'; import 'shell_bar.dart'; /// At least this wide, a destination keeps its list beside the open item /// (with the destination rail) instead of pushing the item as its own page. const double kListDetailMinWidth = 600; const double _bodyWindowTarget = 0.6; const double _bodyWindowMinimum = 0.5; const double _bodyAvailableMaximum = 0.75; /// Whether the window at [context] shows list and detail side by side. bool showsListBesideDetail(BuildContext context) => MediaQuery.sizeOf(context).width >= kListDetailMinWidth; /// The open item's width beside a list, for a [window] this wide and /// [available] width for both panes and their divider. double listDetailBodyWidth(double window, double available) { // Target 60% of the full window. Keep at least half for the body while // leaving a flexible share for native list controls on tighter windows. final requestedMinimum = window * _bodyWindowMinimum; final minimum = available > requestedMinimum ? requestedMinimum : available * _bodyWindowMinimum; final maximum = math.max(minimum, available * _bodyAvailableMaximum); return (window * _bodyWindowTarget).clamp(minimum, maximum); } /// The list pane's width beside an open item (see [listDetailBodyWidth]), /// one divider excluded. double listDetailListWidth(double window, double available) => math.max(0, available - 1 - listDetailBodyWidth(window, available)); /// The system's own insets (status bar, navigation bar) as the shell saw /// them, before its scaffold replaced them with its bars' heights. /// /// A Material [Scaffold] with an app bar and a bottom bar removes the top and /// bottom padding *and* view padding from its body, so a pane beside the list /// cannot recover the status-bar inset from its own [MediaQuery]. The shell /// publishes the insets here; outside a shell the view padding stands in. class ShellSystemInsets extends InheritedWidget { const ShellSystemInsets({ super.key, required this.padding, required super.child, }); final EdgeInsets padding; static EdgeInsets of(BuildContext context) => context .dependOnInheritedWidgetOfExactType() ?.padding ?? MediaQuery.viewPaddingOf(context); @override bool updateShouldNotify(ShellSystemInsets oldWidget) => padding != oldWidget.padding; } /// Narrows a shell bar to the list pane while [split] (the tab shows its list /// beside an open item), placing it at the pane's [alignment]; otherwise the /// bar keeps the whole width it is given. The bar keeps its place in the tree /// either way, so a split never remounts it. class ListPaneWidth extends StatelessWidget { const ListPaneWidth({ super.key, required this.split, required this.alignment, required this.child, }); final bool split; final AlignmentGeometry alignment; final Widget child; @override Widget build(BuildContext context) => LayoutBuilder( builder: (context, constraints) { final width = split ? listDetailListWidth( MediaQuery.sizeOf(context).width, constraints.maxWidth, ) : constraints.maxWidth; return Align( alignment: alignment, heightFactor: 1, child: SizedBox(width: width, child: child), ); }, ); } /// One body-priority policy for all destinations, derived from the full window /// and actual available space after navigation. No fixed pixel pane widths. /// /// Under the shell, the list keeps the shell's bars (both narrowed to the list /// pane through [ShellPaneSplit], see [ListPaneWidth]) and the open item owns /// the rest of the window: the detail starts at the top edge and reaches the /// bottom edge with only the system insets ([ShellSystemInsets]) as its /// padding, and its own tabs float at its bottom, as on a phone. class ListDetailPanes extends StatelessWidget { const ListDetailPanes({super.key, required this.list, required this.detail}); final Widget list; final Widget detail; @override Widget build(BuildContext context) { final divider = usesCupertinoHost(context) ? Container( width: 1, color: CupertinoColors.separator.resolveFrom(context), ) : const VerticalDivider(width: 1); final media = MediaQuery.of(context); final split = ShellBar.available(context); return ShellPaneSplit( child: LayoutBuilder( builder: (context, constraints) { final bodyWidth = listDetailBodyWidth( media.size.width, constraints.maxWidth, ); Widget body = detail; if (split) { // Beside the list the item is under neither of the shell's bars // (both stay over the list pane): it keeps only the system's own // insets, so its bar sits just below the status bar at the top // edge, and its tabs or composer own the pane's bottom edge, // floating just above the system inset. final system = ShellSystemInsets.of(context); body = MediaQuery( data: media.copyWith( padding: media.padding.copyWith( top: system.top, bottom: system.bottom, ), ), child: PageTabsEdge(edge: FloatingBarEdge.bottom, child: body), ); } return Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( child: SizedBox.expand( key: const ValueKey('list-detail-list'), child: Semantics( container: true, explicitChildNodes: true, identifier: 'list-detail-list', child: list, ), ), ), divider, SizedBox( width: bodyWidth, child: SizedBox.expand( key: const ValueKey('list-detail-body'), child: Semantics( container: true, explicitChildNodes: true, identifier: 'list-detail-body', child: body, ), ), ), ], ); }, ), ); } }