import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; /// A bar's actions in one row, each at its natural width, with a trailing /// [more] button that shows only when they do not all fit. /// /// When the actions are wider than [overflowWidth] (the room the bar leaves /// them beside a readable title), the [overflowable] ones leave the row from /// its trailing end until the rest and [more] fit in it, so /// every action left keeps its own (touch-target) width instead of being /// squeezed. [onHidden] hears which actions left (by index) after a layout /// that changes them, so the bar can list them in its More menu. Only when /// the actions that stay are wider than the row may be at all (a long model /// name, which cannot move) does the first of them shrink, as a [Flexible] /// would, so the row never overflows. class SegmentedActionRow extends MultiChildRenderObjectWidget { SegmentedActionRow({ super.key, required List actions, required Widget more, required this.overflowable, required this.overflowWidth, required this.onHidden, }) : assert(actions.length == overflowable.length), super(children: [...actions, more]); /// Whether each action may move into the More menu. final List overflowable; /// Past this width, [overflowable] actions move into the More menu. final double overflowWidth; final ValueChanged> onHidden; @override RenderSegmentedActionRow createRenderObject(BuildContext context) => RenderSegmentedActionRow( overflowable: overflowable, overflowWidth: overflowWidth, onHidden: onHidden, textDirection: Directionality.of(context), ); @override void updateRenderObject( BuildContext context, RenderSegmentedActionRow renderObject, ) { renderObject ..overflowable = overflowable ..overflowWidth = overflowWidth ..onHidden = onHidden ..textDirection = Directionality.of(context); } } class _ActionParentData extends ContainerBoxParentData { bool shown = true; } /// The render object of [SegmentedActionRow]. class RenderSegmentedActionRow extends RenderBox with ContainerRenderObjectMixin, RenderBoxContainerDefaultsMixin { RenderSegmentedActionRow({ required this._overflowable, required this._overflowWidth, required this.onHidden, required this._textDirection, }); List _overflowable; set overflowable(List value) { if (listEquals(value, _overflowable)) return; _overflowable = value; markNeedsLayout(); } double _overflowWidth; set overflowWidth(double value) { if (value == _overflowWidth) return; _overflowWidth = value; markNeedsLayout(); } ValueChanged> onHidden; TextDirection _textDirection; set textDirection(TextDirection value) { if (value == _textDirection) return; _textDirection = value; markNeedsLayout(); } /// What the last layout reported to [onHidden]; nothing is hidden at /// first, so a bar whose actions fit reports nothing. List _reported = const []; @override void setupParentData(RenderBox child) { if (child.parentData is! _ActionParentData) { child.parentData = _ActionParentData(); } } List get _children { final list = []; var child = firstChild; while (child != null) { list.add(child); child = childAfter(child); } return list; } /// Which actions show, whether More shows, and the most the first shown /// action may take (infinite when everything fits at its natural width). ({List shown, bool more, double firstMax}) _plan( List widths, double moreWidth, double maxWidth, ) { final count = widths.length; final shown = List.filled(count, true); var total = widths.fold(0.0, (sum, width) => sum + width); var more = false; final soft = math.min(_overflowWidth, maxWidth); if (total > soft && _overflowable.contains(true)) { more = true; for (var index = count - 1; index >= 0; index--) { if (total + moreWidth <= soft) break; if (!_overflowable[index]) continue; shown[index] = false; total -= widths[index]; } } final room = maxWidth - (more ? moreWidth : 0); final first = shown.indexOf(true); if (total <= room || first < 0) { return (shown: shown, more: more, firstMax: double.infinity); } return ( shown: shown, more: more, firstMax: math.max(0, room - (total - widths[first])), ); } Size _layout(BoxConstraints constraints, {required bool dry}) { final children = _children; final actions = children.sublist(0, children.length - 1); final moreChild = children.last; final loose = BoxConstraints(maxHeight: constraints.maxHeight); Size size(RenderBox child, BoxConstraints box) => dry ? child.getDryLayout(box) : (child..layout(box, parentUsesSize: true)).size; final sizes = [for (final child in actions) size(child, loose)]; final moreSize = size(moreChild, loose); final plan = _plan( [for (final s in sizes) s.width], moreSize.width, constraints.maxWidth, ); final first = plan.shown.indexOf(true); if (first >= 0 && plan.firstMax.isFinite) { sizes[first] = size( actions[first], loose.copyWith(maxWidth: plan.firstMax), ); } final placed = [ for (final (index, s) in sizes.indexed) if (plan.shown[index]) (actions[index], s), if (plan.more) (moreChild, moreSize), ]; final height = placed.fold( 0.0, (most, entry) => math.max(most, entry.$2.height), ); final width = placed.fold(0.0, (sum, entry) => sum + entry.$2.width); final result = constraints.constrain(Size(width, height)); if (dry) return result; for (final child in children) { (child.parentData! as _ActionParentData).shown = false; } var x = 0.0; for (final (child, s) in placed) { final data = child.parentData! as _ActionParentData; data.shown = true; final dx = _textDirection == TextDirection.ltr ? x : result.width - x - s.width; data.offset = Offset(dx, (result.height - s.height) / 2); x += s.width; } final hidden = [ for (final (index, shown) in plan.shown.indexed) if (!shown) index, ]; if (!listEquals(hidden, _reported)) { _reported = hidden; final report = onHidden; // The bar rebuilds its More menu from this, which it may not do in // the middle of a layout. SchedulerBinding.instance.addPostFrameCallback((_) => report(hidden)); } return result; } @override void performLayout() { size = _layout(constraints, dry: false); } @override Size computeDryLayout(covariant BoxConstraints constraints) => _layout(constraints, dry: true); Iterable get _actionChildren => _children.sublist(0, childCount - 1); @override double computeMinIntrinsicWidth(double height) { final children = _children; // At its narrowest: the actions that cannot move, and More. var width = children.last.getMinIntrinsicWidth(height); for (final (index, child) in _actionChildren.indexed) { if (!_overflowable[index]) width += child.getMinIntrinsicWidth(height); } return width; } @override double computeMaxIntrinsicWidth(double height) => _actionChildren.fold( 0.0, (sum, child) => sum + child.getMaxIntrinsicWidth(height), ); @override double computeMinIntrinsicHeight(double width) => _children.fold( 0.0, (most, child) => math.max(most, child.getMinIntrinsicHeight(width)), ); @override double computeMaxIntrinsicHeight(double width) => _children.fold( 0.0, (most, child) => math.max(most, child.getMaxIntrinsicHeight(width)), ); bool _shown(RenderBox child) => (child.parentData! as _ActionParentData).shown; @override void paint(PaintingContext context, Offset offset) { for (final child in _children) { if (!_shown(child)) continue; final data = child.parentData! as _ActionParentData; context.paintChild(child, offset + data.offset); } } @override bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { for (final child in _children.reversed) { if (!_shown(child)) continue; final data = child.parentData! as _ActionParentData; final hit = result.addWithPaintOffset( offset: data.offset, position: position, hitTest: (result, transformed) => child.hitTest(result, position: transformed), ); if (hit) return true; } return false; } @override void visitChildrenForSemantics(RenderObjectVisitor visitor) { for (final child in _children) { if (_shown(child)) visitor(child); } } }