sliver_padding.dart 14.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5 6 7 8 9 10 11 12
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

import 'package:vector_math/vector_math_64.dart';

import 'debug.dart';
import 'object.dart';
import 'sliver.dart';

13
/// Insets a [RenderSliver] by applying [resolvedPadding] on each side.
Ian Hickson's avatar
Ian Hickson committed
14
///
15 16
/// A [RenderSliverEdgeInsetsPadding] subclass wraps the [SliverGeometry.layoutExtent]
/// of its child. Any incoming [SliverConstraints.overlap] is ignored and not
Ian Hickson's avatar
Ian Hickson committed
17 18
/// passed on to the child.
///
19
/// {@template flutter.rendering.RenderSliverEdgeInsetsPadding}
20 21 22
/// Applying padding in the main extent of the viewport to slivers that have scroll effects is likely to have
/// undesired effects. For example, For example, wrapping a [SliverPersistentHeader] with
/// `pinned:true` will cause only the appbar to stay pinned while the padding will scroll away.
23 24
/// {@endtemplate}
abstract class RenderSliverEdgeInsetsPadding extends RenderSliver with RenderObjectWithChildMixin<RenderSliver> {
Ian Hickson's avatar
Ian Hickson committed
25
  /// The amount to pad the child in each dimension.
Ian Hickson's avatar
Ian Hickson committed
26
  ///
27 28
  /// The offsets are specified in terms of visual edges, left, top, right, and
  /// bottom. These values are not affected by the [TextDirection].
Ian Hickson's avatar
Ian Hickson committed
29
  ///
30
  /// Must not be null or contain negative values when [performLayout] is called.
31
  EdgeInsets? get resolvedPadding;
Ian Hickson's avatar
Ian Hickson committed
32 33 34 35 36 37

  /// The padding in the scroll direction on the side nearest the 0.0 scroll direction.
  ///
  /// Only valid after layout has started, since before layout the render object
  /// doesn't know what direction it will be laid out in.
  double get beforePadding {
38
    assert(resolvedPadding != null);
Ian Hickson's avatar
Ian Hickson committed
39 40
    switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
      case AxisDirection.up:
41
        return resolvedPadding!.bottom;
Ian Hickson's avatar
Ian Hickson committed
42
      case AxisDirection.right:
43
        return resolvedPadding!.left;
Ian Hickson's avatar
Ian Hickson committed
44
      case AxisDirection.down:
45
        return resolvedPadding!.top;
Ian Hickson's avatar
Ian Hickson committed
46
      case AxisDirection.left:
47
        return resolvedPadding!.right;
Ian Hickson's avatar
Ian Hickson committed
48 49 50 51 52 53 54 55
    }
  }

  /// The padding in the scroll direction on the side furthest from the 0.0 scroll offset.
  ///
  /// Only valid after layout has started, since before layout the render object
  /// doesn't know what direction it will be laid out in.
  double get afterPadding {
56
    assert(resolvedPadding != null);
Ian Hickson's avatar
Ian Hickson committed
57 58
    switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
      case AxisDirection.up:
59
        return resolvedPadding!.top;
Ian Hickson's avatar
Ian Hickson committed
60
      case AxisDirection.right:
61
        return resolvedPadding!.right;
Ian Hickson's avatar
Ian Hickson committed
62
      case AxisDirection.down:
63
        return resolvedPadding!.bottom;
Ian Hickson's avatar
Ian Hickson committed
64
      case AxisDirection.left:
65
        return resolvedPadding!.left;
Ian Hickson's avatar
Ian Hickson committed
66 67 68
    }
  }

69 70 71
  /// The total padding in the [SliverConstraints.axisDirection]. (In other
  /// words, for a vertical downwards-growing list, the sum of the padding on
  /// the top and bottom.)
Ian Hickson's avatar
Ian Hickson committed
72 73 74 75
  ///
  /// Only valid after layout has started, since before layout the render object
  /// doesn't know what direction it will be laid out in.
  double get mainAxisPadding {
76
    assert(resolvedPadding != null);
77
    return resolvedPadding!.along(constraints.axis);
Ian Hickson's avatar
Ian Hickson committed
78 79 80 81 82 83 84 85 86
  }

  /// The total padding in the cross-axis direction. (In other words, for a
  /// vertical downwards-growing list, the sum of the padding on the left and
  /// right.)
  ///
  /// Only valid after layout has started, since before layout the render object
  /// doesn't know what direction it will be laid out in.
  double get crossAxisPadding {
87
    assert(resolvedPadding != null);
Ian Hickson's avatar
Ian Hickson committed
88 89
    switch (constraints.axis) {
      case Axis.horizontal:
90
        return resolvedPadding!.vertical;
Ian Hickson's avatar
Ian Hickson committed
91
      case Axis.vertical:
92
        return resolvedPadding!.horizontal;
Ian Hickson's avatar
Ian Hickson committed
93 94 95 96 97
    }
  }

  @override
  void setupParentData(RenderObject child) {
98
    if (child.parentData is! SliverPhysicalParentData) {
99
      child.parentData = SliverPhysicalParentData();
100
    }
Ian Hickson's avatar
Ian Hickson committed
101 102 103 104
  }

  @override
  void performLayout() {
105
    final SliverConstraints constraints = this.constraints;
106
    assert(resolvedPadding != null);
Ian Hickson's avatar
Ian Hickson committed
107 108 109 110 111
    final double beforePadding = this.beforePadding;
    final double afterPadding = this.afterPadding;
    final double mainAxisPadding = this.mainAxisPadding;
    final double crossAxisPadding = this.crossAxisPadding;
    if (child == null) {
112 113 114 115 116 117 118 119 120 121
      final double paintExtent = calculatePaintOffset(
          constraints,
          from: 0.0,
          to: mainAxisPadding,
      );
      final double cacheExtent = calculateCacheOffset(
          constraints,
          from: 0.0,
          to: mainAxisPadding,
      );
122
      geometry = SliverGeometry(
Ian Hickson's avatar
Ian Hickson committed
123
        scrollExtent: mainAxisPadding,
124
        paintExtent: math.min(paintExtent, constraints.remainingPaintExtent),
Ian Hickson's avatar
Ian Hickson committed
125
        maxPaintExtent: mainAxisPadding,
126
        cacheExtent: cacheExtent,
Ian Hickson's avatar
Ian Hickson committed
127 128 129
      );
      return;
    }
130 131 132 133 134 135 136 137 138
    final double beforePaddingPaintExtent = calculatePaintOffset(
      constraints,
      from: 0.0,
      to: beforePadding,
    );
    double overlap = constraints.overlap;
    if (overlap > 0) {
      overlap = math.max(0.0, constraints.overlap - beforePaddingPaintExtent);
    }
139
    child!.layout(
Ian Hickson's avatar
Ian Hickson committed
140 141
      constraints.copyWith(
        scrollOffset: math.max(0.0, constraints.scrollOffset - beforePadding),
142
        cacheOrigin: math.min(0.0, constraints.cacheOrigin + beforePadding),
143
        overlap: overlap,
Ian Hickson's avatar
Ian Hickson committed
144
        remainingPaintExtent: constraints.remainingPaintExtent - calculatePaintOffset(constraints, from: 0.0, to: beforePadding),
145
        remainingCacheExtent: constraints.remainingCacheExtent - calculateCacheOffset(constraints, from: 0.0, to: beforePadding),
146
        crossAxisExtent: math.max(0.0, constraints.crossAxisExtent - crossAxisPadding),
147
        precedingScrollExtent: beforePadding + constraints.precedingScrollExtent,
Ian Hickson's avatar
Ian Hickson committed
148 149 150
      ),
      parentUsesSize: true,
    );
151
    final SliverGeometry childLayoutGeometry = child!.geometry!;
152
    if (childLayoutGeometry.scrollOffsetCorrection != null) {
153
      geometry = SliverGeometry(
154 155 156 157
        scrollOffsetCorrection: childLayoutGeometry.scrollOffsetCorrection,
      );
      return;
    }
Ian Hickson's avatar
Ian Hickson committed
158 159 160 161 162 163
    final double afterPaddingPaintExtent = calculatePaintOffset(
      constraints,
      from: beforePadding + childLayoutGeometry.scrollExtent,
      to: mainAxisPadding + childLayoutGeometry.scrollExtent,
    );
    final double mainAxisPaddingPaintExtent = beforePaddingPaintExtent + afterPaddingPaintExtent;
164 165 166 167 168 169 170 171 172 173 174
    final double beforePaddingCacheExtent = calculateCacheOffset(
      constraints,
      from: 0.0,
      to: beforePadding,
    );
    final double afterPaddingCacheExtent = calculateCacheOffset(
      constraints,
      from: beforePadding + childLayoutGeometry.scrollExtent,
      to: mainAxisPadding + childLayoutGeometry.scrollExtent,
    );
    final double mainAxisPaddingCacheExtent = afterPaddingCacheExtent + beforePaddingCacheExtent;
175 176 177 178
    final double paintExtent = math.min(
      beforePaddingPaintExtent + math.max(childLayoutGeometry.paintExtent, childLayoutGeometry.layoutExtent + afterPaddingPaintExtent),
      constraints.remainingPaintExtent,
    );
179
    geometry = SliverGeometry(
180
      paintOrigin: childLayoutGeometry.paintOrigin,
Ian Hickson's avatar
Ian Hickson committed
181
      scrollExtent: mainAxisPadding + childLayoutGeometry.scrollExtent,
182 183
      paintExtent: paintExtent,
      layoutExtent: math.min(mainAxisPaddingPaintExtent + childLayoutGeometry.layoutExtent, paintExtent),
184
      cacheExtent: math.min(mainAxisPaddingCacheExtent + childLayoutGeometry.cacheExtent, constraints.remainingCacheExtent),
Ian Hickson's avatar
Ian Hickson committed
185 186 187 188 189
      maxPaintExtent: mainAxisPadding + childLayoutGeometry.maxPaintExtent,
      hitTestExtent: math.max(
        mainAxisPaddingPaintExtent + childLayoutGeometry.paintExtent,
        beforePaddingPaintExtent + childLayoutGeometry.hitTestExtent,
      ),
190
      hasVisualOverflow: childLayoutGeometry.hasVisualOverflow,
Ian Hickson's avatar
Ian Hickson committed
191 192
    );

193
    final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
Ian Hickson's avatar
Ian Hickson committed
194 195
    switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
      case AxisDirection.up:
196
        childParentData.paintOffset = Offset(resolvedPadding!.left, calculatePaintOffset(constraints, from: resolvedPadding!.bottom + childLayoutGeometry.scrollExtent, to: resolvedPadding!.bottom + childLayoutGeometry.scrollExtent + resolvedPadding!.top));
Ian Hickson's avatar
Ian Hickson committed
197 198
        break;
      case AxisDirection.right:
199
        childParentData.paintOffset = Offset(calculatePaintOffset(constraints, from: 0.0, to: resolvedPadding!.left), resolvedPadding!.top);
Ian Hickson's avatar
Ian Hickson committed
200 201
        break;
      case AxisDirection.down:
202
        childParentData.paintOffset = Offset(resolvedPadding!.left, calculatePaintOffset(constraints, from: 0.0, to: resolvedPadding!.top));
Ian Hickson's avatar
Ian Hickson committed
203 204
        break;
      case AxisDirection.left:
205
        childParentData.paintOffset = Offset(calculatePaintOffset(constraints, from: resolvedPadding!.right + childLayoutGeometry.scrollExtent, to: resolvedPadding!.right + childLayoutGeometry.scrollExtent + resolvedPadding!.left), resolvedPadding!.top);
Ian Hickson's avatar
Ian Hickson committed
206 207 208 209 210 211 212 213 214
        break;
    }
    assert(beforePadding == this.beforePadding);
    assert(afterPadding == this.afterPadding);
    assert(mainAxisPadding == this.mainAxisPadding);
    assert(crossAxisPadding == this.crossAxisPadding);
  }

  @override
215 216
  bool hitTestChildren(SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
    if (child != null && child!.geometry!.hitTestExtent > 0.0) {
217
      final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
218 219 220
      result.addWithAxisOffset(
        mainAxisPosition: mainAxisPosition,
        crossAxisPosition: crossAxisPosition,
221 222
        mainAxisOffset: childMainAxisPosition(child!),
        crossAxisOffset: childCrossAxisPosition(child!),
223
        paintOffset: childParentData.paintOffset,
224
        hitTest: child!.hitTest,
225 226
      );
    }
Ian Hickson's avatar
Ian Hickson committed
227 228 229 230
    return false;
  }

  @override
231
  double childMainAxisPosition(RenderSliver child) {
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235
    assert(child == this.child);
    return calculatePaintOffset(constraints, from: 0.0, to: beforePadding);
  }

236 237 238
  @override
  double childCrossAxisPosition(RenderSliver child) {
    assert(child == this.child);
239
    assert(resolvedPadding != null);
240 241 242
    switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
      case AxisDirection.up:
      case AxisDirection.down:
243
        return resolvedPadding!.left;
244 245
      case AxisDirection.left:
      case AxisDirection.right:
246
        return resolvedPadding!.top;
247
    }
248 249
  }

250
  @override
251
  double? childScrollOffset(RenderObject child) {
252 253 254 255
    assert(child.parent == this);
    return beforePadding;
  }

Ian Hickson's avatar
Ian Hickson committed
256 257 258
  @override
  void applyPaintTransform(RenderObject child, Matrix4 transform) {
    assert(child == this.child);
259
    final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData;
Ian Hickson's avatar
Ian Hickson committed
260 261 262 263 264
    childParentData.applyPaintTransform(transform);
  }

  @override
  void paint(PaintingContext context, Offset offset) {
265
    if (child != null && child!.geometry!.visible) {
266
      final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
267
      context.paintChild(child!, offset + childParentData.paintOffset);
Ian Hickson's avatar
Ian Hickson committed
268 269 270 271 272 273 274 275
    }
  }

  @override
  void debugPaint(PaintingContext context, Offset offset) {
    super.debugPaint(context, offset);
    assert(() {
      if (debugPaintSizeEnabled) {
276
        final Size parentSize = getAbsoluteSize();
Ian Hickson's avatar
Ian Hickson committed
277
        final Rect outerRect = offset & parentSize;
278
        Rect? innerRect;
Ian Hickson's avatar
Ian Hickson committed
279
        if (child != null) {
280
          final Size childSize = child!.getAbsoluteSize();
281
          final SliverPhysicalParentData childParentData = child!.parentData! as SliverPhysicalParentData;
Ian Hickson's avatar
Ian Hickson committed
282
          innerRect = (offset + childParentData.paintOffset) & childSize;
283 284 285 286
          assert(innerRect.top >= outerRect.top);
          assert(innerRect.left >= outerRect.left);
          assert(innerRect.right <= outerRect.right);
          assert(innerRect.bottom <= outerRect.bottom);
Ian Hickson's avatar
Ian Hickson committed
287 288 289 290
        }
        debugPaintPadding(context.canvas, outerRect, innerRect);
      }
      return true;
291
    }());
Ian Hickson's avatar
Ian Hickson committed
292
  }
293 294 295 296 297 298 299 300
}

/// Insets a [RenderSliver], applying padding on each side.
///
/// A [RenderSliverPadding] object wraps the [SliverGeometry.layoutExtent] of
/// its child. Any incoming [SliverConstraints.overlap] is ignored and not
/// passed on to the child.
///
301
/// {@macro flutter.rendering.RenderSliverEdgeInsetsPadding}
302 303 304 305 306
class RenderSliverPadding extends RenderSliverEdgeInsetsPadding {
  /// Creates a render object that insets its child in a viewport.
  ///
  /// The [padding] argument must not be null and must have non-negative insets.
  RenderSliverPadding({
307 308 309
    required EdgeInsetsGeometry padding,
    TextDirection? textDirection,
    RenderSliver? child,
310
  }) : assert(padding.isNonNegative),
311 312 313 314 315 316
       _padding = padding,
       _textDirection = textDirection {
    this.child = child;
  }

  @override
317 318
  EdgeInsets? get resolvedPadding => _resolvedPadding;
  EdgeInsets? _resolvedPadding;
319 320

  void _resolve() {
321
    if (resolvedPadding != null) {
322
      return;
323
    }
324
    _resolvedPadding = padding.resolve(textDirection);
325
    assert(resolvedPadding!.isNonNegative);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  }

  void _markNeedsResolution() {
    _resolvedPadding = null;
    markNeedsLayout();
  }

  /// The amount to pad the child in each dimension.
  ///
  /// If this is set to an [EdgeInsetsDirectional] object, then [textDirection]
  /// must not be null.
  EdgeInsetsGeometry get padding => _padding;
  EdgeInsetsGeometry _padding;
  set padding(EdgeInsetsGeometry value) {
    assert(padding.isNonNegative);
341
    if (_padding == value) {
342
      return;
343
    }
344 345 346 347 348 349 350 351
    _padding = value;
    _markNeedsResolution();
  }

  /// The text direction with which to resolve [padding].
  ///
  /// This may be changed to null, but only after the [padding] has been changed
  /// to a value that does not depend on the direction.
352 353 354
  TextDirection? get textDirection => _textDirection;
  TextDirection? _textDirection;
  set textDirection(TextDirection? value) {
355
    if (_textDirection == value) {
356
      return;
357
    }
358 359 360 361 362 363 364 365 366
    _textDirection = value;
    _markNeedsResolution();
  }

  @override
  void performLayout() {
    _resolve();
    super.performLayout();
  }
367 368

  @override
369 370
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
371 372
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding));
    properties.add(EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
373
  }
Ian Hickson's avatar
Ian Hickson committed
374
}