import 'dart:math' as math; import 'package:flutter/material.dart'; import '../ui/adaptive/floating_bar_layout.dart'; import 'detail_pane_navigator.dart'; import 'list_detail_policy.dart'; import 'list_pane_divider.dart'; import 'list_pane_widths.dart'; import 'shell_bar.dart'; export 'list_detail_policy.dart'; /// 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, this.preferredWidth, required this.child, }); final bool split; /// The list width the operator dragged the divider to, null for the /// default policy; see [listDetailListWidth]. final double? preferredWidth; 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, preferredWidth, ) : 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 top bar (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 below the status /// bar ([ShellSystemInsets]). The shell's tab bar moves to the bottom of the /// list pane, so the list learns that bar's height as its bottom padding and /// the detail keeps its whole height: its own tabs (or the chat composer) sit /// at its bottom edge, above the system inset only. Pages the item opens stay /// in its pane ([DetailPaneNavigator]). class ListDetailPanes extends StatefulWidget { const ListDetailPanes({ super.key, required this.list, required this.detail, this.detailIdentity, this.listWidthOverride, this.compactListWidth, this.onListCompactChanged, }); final Widget list; /// Focus layouts may retain the list offstage at zero/compact width. final double? listWidthOverride; /// Optional compact rail reached by dragging below the normal list minimum. /// The grab handle remains available at the rail edge to expand it again. final double? compactListWidth; final ValueChanged? onListCompactChanged; final Widget detail; /// Which item [detail] shows; selecting another item closes any page the /// previous one opened in its pane. final Object? detailIdentity; @override State createState() => _ListDetailPanesState(); } class _ListDetailPanesState extends State { // Outside a shell (no [ListPaneWidthsScope]) the divider still drags; the // width lasts as long as the panes. ListPaneWidths? _ownWidths; @override void dispose() { _ownWidths?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final widths = ListPaneWidthsScope.maybeOf(context) ?? (_ownWidths ??= ListPaneWidths(store: MemoryListPaneWidthStore())); final key = ShellBarScope.maybeOf(context)?.destination.name ?? 'panes'; final media = MediaQuery.of(context); final split = ShellBar.available(context); return ShellPaneSplit( child: ListenableBuilder( listenable: widths, builder: (context, _) => LayoutBuilder( builder: (context, constraints) { final window = media.size.width; final available = constraints.maxWidth; final preferred = widths.widthFor(key); final resizable = widget.listWidthOverride == null || widget.listWidthOverride == widget.compactListWidth; final bounds = resizable ? listPaneWidthBounds( window, available, minimum: widget.compactListWidth ?? kListPaneMinWidth, ) : null; final compactThreshold = math.min( kListPaneMinWidth, listDetailListWidth(window, available) * .8, ); // In a tight split the shell's bar and list share the default // width policy. The rail can still collapse/expand there without // making the shell's controls wider than their list. final normalBounds = listPaneWidthBounds(window, available); final normalWidth = widget.compactListWidth != null && normalBounds != null && preferred != null ? preferred.clamp(compactThreshold, normalBounds.max).toDouble() : listDetailListWidth(window, available, preferred); final listWidth = (widget.listWidthOverride ?? normalWidth) .clamp(0.0, math.max(0.0, available - kDetailPaneMinWidth)) .toDouble(); final lineWidth = listWidth > 0 ? 1.0 : 0.0; final bodyWidth = math.max(0.0, available - lineWidth - listWidth); final retainedWidth = widget.listWidthOverride == null ? listWidth : math.max(normalWidth, kListPaneMinWidth); void resize(double value) { widths.preview(key, value); widget.onListCompactChanged?.call(value < compactThreshold); } Widget body = widget.detail; if (split) { // Beside the list the item is not under the shell's top bar (it // stays over the list pane): it keeps the system's own top // inset, so its bar sits just below the status bar at the top // edge. The shell's tab bar floats at the bottom of the list // pane, not over this one, so the item clears only the // system's bottom inset and its own tabs or composer sit at // its bottom edge. 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: DetailPaneNavigator( identity: widget.detailIdentity, child: body, ), ), ); // The list keeps the shell's bottom padding (the tab bar's // height), so its last rows scroll clear of the bar. } return Stack( children: [ Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SizedBox( width: listWidth, child: ClipRect( child: OverflowBox( alignment: AlignmentDirectional.topStart, minWidth: retainedWidth, maxWidth: retainedWidth, child: ExcludeFocus( excluding: listWidth == 0, child: ExcludeSemantics( excluding: listWidth == 0, child: SizedBox.expand( key: const ValueKey('list-detail-list'), child: Semantics( container: true, explicitChildNodes: true, identifier: 'list-detail-list', child: widget.list, ), ), ), ), ), ), ), if (lineWidth > 0) const ListPaneDividerLine(), SizedBox( width: bodyWidth, child: SizedBox.expand( key: const ValueKey('list-detail-body'), child: Semantics( container: true, explicitChildNodes: true, identifier: 'list-detail-body', child: body, ), ), ), ], ), if (bounds != null) PositionedDirectional( start: listWidth + 0.5 - kListPaneDividerGrabWidth / 2, top: 0, bottom: 0, width: kListPaneDividerGrabWidth, child: ListPaneDivider( width: listWidth, min: widget.compactListWidth ?? bounds.min, max: bounds.max, onChanged: resize, onChangeEnd: () => widths.commit(key), onReset: () { widths.reset(key); widget.onListCompactChanged?.call(false); }, ), ), ], ); }, ), ), ); } }