|
| 1 | +import 'dart:math' as math; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/widgets.dart'; |
| 5 | +import 'package:flutter/rendering.dart'; |
| 6 | + |
| 7 | +class NegativeLeftOffset extends SingleChildRenderObjectWidget { |
| 8 | + NegativeLeftOffset({super.key, required this.leftOffset, super.child}) |
| 9 | + : assert(leftOffset.isNegative), |
| 10 | + _padding = EdgeInsets.only(left: leftOffset); |
| 11 | + |
| 12 | + final double leftOffset; |
| 13 | + final EdgeInsetsGeometry _padding; |
| 14 | + |
| 15 | + @override |
| 16 | + RenderNegativePadding createRenderObject(BuildContext context) { |
| 17 | + return RenderNegativePadding( |
| 18 | + padding: _padding, |
| 19 | + textDirection: Directionality.maybeOf(context)); |
| 20 | + } |
| 21 | + |
| 22 | + @override |
| 23 | + void updateRenderObject( |
| 24 | + BuildContext context, |
| 25 | + RenderNegativePadding renderObject, |
| 26 | + ) { |
| 27 | + renderObject |
| 28 | + ..padding = _padding |
| 29 | + ..textDirection = Directionality.maybeOf(context); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 34 | + super.debugFillProperties(properties); |
| 35 | + properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', _padding)); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class RenderNegativePadding extends RenderShiftedBox { |
| 40 | + RenderNegativePadding({ |
| 41 | + required EdgeInsetsGeometry padding, |
| 42 | + TextDirection? textDirection, |
| 43 | + RenderBox? child, |
| 44 | + }) : assert(padding.isNegative), |
| 45 | + _textDirection = textDirection, |
| 46 | + _padding = padding, |
| 47 | + super(child); |
| 48 | + |
| 49 | + EdgeInsets? _resolvedPaddingCache; |
| 50 | + EdgeInsets get _resolvedPadding { |
| 51 | + final EdgeInsets returnValue = _resolvedPaddingCache ??= padding.resolve(textDirection); |
| 52 | + return returnValue; |
| 53 | + } |
| 54 | + |
| 55 | + void _markNeedResolution() { |
| 56 | + _resolvedPaddingCache = null; |
| 57 | + markNeedsLayout(); |
| 58 | + } |
| 59 | + |
| 60 | + /// The amount to pad the child in each dimension. |
| 61 | + /// |
| 62 | + /// If this is set to an [EdgeInsetsDirectional] object, then [textDirection] |
| 63 | + /// must not be null. |
| 64 | + EdgeInsetsGeometry get padding => _padding; |
| 65 | + EdgeInsetsGeometry _padding; |
| 66 | + set padding(EdgeInsetsGeometry value) { |
| 67 | + assert(value.isNegative); |
| 68 | + if (_padding == value) { |
| 69 | + return; |
| 70 | + } |
| 71 | + _padding = value; |
| 72 | + _markNeedResolution(); |
| 73 | + } |
| 74 | + |
| 75 | + /// The text direction with which to resolve [padding]. |
| 76 | + /// |
| 77 | + /// This may be changed to null, but only after the [padding] has been changed |
| 78 | + /// to a value that does not depend on the direction. |
| 79 | + TextDirection? get textDirection => _textDirection; |
| 80 | + TextDirection? _textDirection; |
| 81 | + set textDirection(TextDirection? value) { |
| 82 | + if (_textDirection == value) { |
| 83 | + return; |
| 84 | + } |
| 85 | + _textDirection = value; |
| 86 | + _markNeedResolution(); |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + double computeMinIntrinsicWidth(double height) { |
| 91 | + final EdgeInsets padding = _resolvedPadding; |
| 92 | + if (child != null) { |
| 93 | + // Relies on double.infinity absorption. |
| 94 | + return child!.getMinIntrinsicWidth(math.max(0.0, height - padding.vertical)) + |
| 95 | + padding.horizontal; |
| 96 | + } |
| 97 | + return padding.horizontal; |
| 98 | + } |
| 99 | + |
| 100 | + @override |
| 101 | + double computeMaxIntrinsicWidth(double height) { |
| 102 | + final EdgeInsets padding = _resolvedPadding; |
| 103 | + if (child != null) { |
| 104 | + // Relies on double.infinity absorption. |
| 105 | + return child!.getMaxIntrinsicWidth(math.max(0.0, height - padding.vertical)) + |
| 106 | + padding.horizontal; |
| 107 | + } |
| 108 | + return padding.horizontal; |
| 109 | + } |
| 110 | + |
| 111 | + @override |
| 112 | + double computeMinIntrinsicHeight(double width) { |
| 113 | + final EdgeInsets padding = _resolvedPadding; |
| 114 | + if (child != null) { |
| 115 | + // Relies on double.infinity absorption. |
| 116 | + return child!.getMinIntrinsicHeight(math.max(0.0, width - padding.horizontal)) + |
| 117 | + padding.vertical; |
| 118 | + } |
| 119 | + return padding.vertical; |
| 120 | + } |
| 121 | + |
| 122 | + @override |
| 123 | + double computeMaxIntrinsicHeight(double width) { |
| 124 | + final EdgeInsets padding = _resolvedPadding; |
| 125 | + if (child != null) { |
| 126 | + // Relies on double.infinity absorption. |
| 127 | + return child!.getMaxIntrinsicHeight(math.max(0.0, width - padding.horizontal)) + |
| 128 | + padding.vertical; |
| 129 | + } |
| 130 | + return padding.vertical; |
| 131 | + } |
| 132 | + |
| 133 | + @override |
| 134 | + @protected |
| 135 | + Size computeDryLayout(covariant BoxConstraints constraints) { |
| 136 | + final EdgeInsets padding = _resolvedPadding; |
| 137 | + if (child == null) { |
| 138 | + return constraints.constrain(Size(padding.horizontal, padding.vertical)); |
| 139 | + } |
| 140 | + final BoxConstraints innerConstraints = constraints.deflate(padding); |
| 141 | + final Size childSize = child!.getDryLayout(innerConstraints); |
| 142 | + return constraints.constrain( |
| 143 | + Size(padding.horizontal + childSize.width, padding.vertical + childSize.height), |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + @override |
| 148 | + double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) { |
| 149 | + final RenderBox? child = this.child; |
| 150 | + if (child == null) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + final EdgeInsets padding = _resolvedPadding; |
| 154 | + final BoxConstraints innerConstraints = constraints.deflate(padding); |
| 155 | + final BaselineOffset result = |
| 156 | + BaselineOffset(child.getDryBaseline(innerConstraints, baseline)) + padding.top; |
| 157 | + return result.offset; |
| 158 | + } |
| 159 | + |
| 160 | + @override |
| 161 | + void performLayout() { |
| 162 | + final BoxConstraints constraints = this.constraints; |
| 163 | + final EdgeInsets padding = _resolvedPadding; |
| 164 | + if (child == null) { |
| 165 | + size = constraints.constrain(Size(padding.horizontal, padding.vertical)); |
| 166 | + return; |
| 167 | + } |
| 168 | + final BoxConstraints innerConstraints = constraints.deflate(padding); |
| 169 | + child!.layout(innerConstraints, parentUsesSize: true); |
| 170 | + final BoxParentData childParentData = child!.parentData! as BoxParentData; |
| 171 | + childParentData.offset = Offset(padding.left, padding.top); |
| 172 | + size = constraints.constrain( |
| 173 | + Size(padding.horizontal + child!.size.width, padding.vertical + child!.size.height), |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + @override |
| 178 | + void debugPaintSize(PaintingContext context, Offset offset) { |
| 179 | + super.debugPaintSize(context, offset); |
| 180 | + assert(() { |
| 181 | + final Rect outerRect = offset & size; |
| 182 | + debugPaintPadding( |
| 183 | + context.canvas, |
| 184 | + outerRect, |
| 185 | + child != null ? _resolvedPaddingCache!.deflateRect(outerRect) : null, |
| 186 | + ); |
| 187 | + return true; |
| 188 | + }()); |
| 189 | + } |
| 190 | + |
| 191 | + @override |
| 192 | + void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 193 | + super.debugFillProperties(properties); |
| 194 | + properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding)); |
| 195 | + properties.add(EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null)); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +extension on EdgeInsetsGeometry { |
| 200 | + bool get isNegative => !isNonNegative; |
| 201 | +} |
0 commit comments