single_child_scroll_view.dart 24.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9
import 'dart:math' as math;

import 'package:flutter/rendering.dart';
10
import 'package:flutter/gestures.dart' show DragStartBehavior;
11 12 13

import 'basic.dart';
import 'framework.dart';
14
import 'primary_scroll_controller.dart';
15 16
import 'scroll_controller.dart';
import 'scroll_physics.dart';
17 18
import 'scrollable.dart';

19 20 21 22 23 24 25 26 27 28
/// A box in which a single widget can be scrolled.
///
/// This widget is useful when you have a single box that will normally be
/// entirely visible, for example a clock face in a time picker, but you need to
/// make sure it can be scrolled if the container gets too small in one axis
/// (the scroll direction).
///
/// It is also useful if you need to shrink-wrap in both axes (the main
/// scrolling direction as well as the cross axis), as one might see in a dialog
/// or pop-up menu. In that case, you might pair the [SingleChildScrollView]
29
/// with a [ListBody] child.
30 31 32 33
///
/// When you have a list of children and do not require cross-axis
/// shrink-wrapping behavior, for example a scrolling list that is always the
/// width of the screen, consider [ListView], which is vastly more efficient
34
/// that a [SingleChildScrollView] containing a [ListBody] or [Column] with
35 36
/// many children.
///
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/// ## Sample code: Using [SingleChildScrollView] with a [Column]
///
/// Sometimes a layout is designed around the flexible properties of a
/// [Column], but there is the concern that in some cases, there might not
/// be enough room to see the entire contents. This could be because some
/// devices have unusually small screens, or because the application can
/// be used in landscape mode where the aspect ratio isn't what was
/// originally envisioned, or because the application is being shown in a
/// small window in split-screen mode. In any case, as a result, it might
/// make sense to wrap the layout in a [SingleChildScrollView].
///
/// Simply doing so, however, usually results in a conflict between the [Column],
/// which typically tries to grow as big as it can, and the [SingleChildScrollView],
/// which provides its children with an infinite amount of space.
///
/// To resolve this apparent conflict, there are a couple of techniques, as
/// discussed below. These techniques should only be used when the content is
/// normally expected to fit on the screen, so that the lazy instantiation of
/// a sliver-based [ListView] or [CustomScrollView] is not expected to provide
/// any performance benefit. If the viewport is expected to usually contain
/// content beyond the dimensions of the screen, then [SingleChildScrollView]
/// would be very expensive.
///
/// ### Centering, spacing, or aligning fixed-height content
///
/// If the content has fixed (or intrinsic) dimensions but needs to be spaced out,
/// centered, or otherwise positioned using the [Flex] layout model of a [Column],
/// the following technique can be used to provide the [Column] with a minimum
/// dimension while allowing it to shrink-wrap the contents when there isn't enough
/// room to apply these spacing or alignment needs.
///
/// A [LayoutBuilder] is used to obtain the size of the viewport (implicitly via
/// the constraints that the [SingleChildScrollView] sees, since viewports
/// typically grow to fit their maximum height constraint). Then, inside the
/// scroll view, a [ConstrainedBox] is used to set the minimum height of the
/// [Column].
///
/// The [Column] has no [Expanded] children, so rather than take on the infinite
/// height from its [BoxConstraints.maxHeight], (the viewport provides no maximum height
/// constraint), it automatically tries to shrink to fit its children. It cannot
/// be smaller than its [BoxConstraints.minHeight], though, and It therefore
/// becomes the bigger of the minimum height provided by the
/// [ConstrainedBox] and the sum of the heights of the children.
///
/// If the children aren't enough to fit that minimum size, the [Column] ends up
/// with some remaining space to allocate as specified by its
/// [Column.mainAxisAlignment] argument.
///
85
/// {@tool dartpad --template=stateless_widget_material}
86 87
/// In this example, the children are spaced out equally, unless there's no more
/// room, in which case they stack vertically and scroll.
88 89 90 91 92
///
/// When using this technique, [Expanded] and [Flexible] are not useful, because
/// in both cases the "available space" is infinite (since this is in a viewport).
/// The next section describes a technique for providing a maximum height constraint.
///
93
/// ```dart
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
///  Widget build(BuildContext context) {
///    return DefaultTextStyle(
///      style: Theme.of(context).textTheme.bodyText2,
///      child: LayoutBuilder(
///        builder: (BuildContext context, BoxConstraints viewportConstraints) {
///          return SingleChildScrollView(
///            child: ConstrainedBox(
///              constraints: BoxConstraints(
///                minHeight: viewportConstraints.maxHeight,
///              ),
///              child: Column(
///                mainAxisSize: MainAxisSize.min,
///                mainAxisAlignment: MainAxisAlignment.spaceAround,
///                children: <Widget>[
///                  Container(
///                    // A fixed-height child.
///                    color: const Color(0xffeeee00), // Yellow
///                    height: 120.0,
///                    alignment: Alignment.center,
///                    child: const Text('Fixed Height Content'),
///                  ),
///                  Container(
///                    // Another fixed-height child.
///                    color: const Color(0xff008000), // Green
///                    height: 120.0,
///                    alignment: Alignment.center,
///                    child: const Text('Fixed Height Content'),
///                  ),
///                ],
///              ),
///            ),
///          );
///        },
///      ),
///    );
///  }
130 131 132
/// ```
/// {@end-tool}
///
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/// ### Expanding content to fit the viewport
///
/// The following example builds on the previous one. In addition to providing a
/// minimum dimension for the child [Column], an [IntrinsicHeight] widget is used
/// to force the column to be exactly as big as its contents. This constraint
/// combines with the [ConstrainedBox] constraints discussed previously to ensure
/// that the column becomes either as big as viewport, or as big as the contents,
/// whichever is biggest.
///
/// Both constraints must be used to get the desired effect. If only the
/// [IntrinsicHeight] was specified, then the column would not grow to fit the
/// entire viewport when its children were smaller than the whole screen. If only
/// the size of the viewport was used, then the [Column] would overflow if the
/// children were bigger than the viewport.
///
/// The widget that is to grow to fit the remaining space so provided is wrapped
/// in an [Expanded] widget.
///
/// This technique is quite expensive, as it more or less requires that the contents
/// of the viewport be laid out twice (once to find their intrinsic dimensions, and
/// once to actually lay them out). The number of widgets within the column should
/// therefore be kept small. Alternatively, subsets of the children that have known
/// dimensions can be wrapped in a [SizedBox] that has tight vertical constraints,
/// so that the intrinsic sizing algorithm can short-circuit the computation when it
/// reaches those parts of the subtree.
///
159
/// {@tool dartpad --template=stateless_widget_material}
160 161 162 163
/// In this example, the column becomes either as big as viewport, or as big as
/// the contents, whichever is biggest.
///
/// ```dart
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
///  Widget build(BuildContext context) {
///    return DefaultTextStyle(
///      style: Theme.of(context).textTheme.bodyText2,
///      child: LayoutBuilder(
///        builder: (BuildContext context, BoxConstraints viewportConstraints) {
///          return SingleChildScrollView(
///            child: ConstrainedBox(
///              constraints: BoxConstraints(
///                minHeight: viewportConstraints.maxHeight,
///              ),
///              child: IntrinsicHeight(
///                child: Column(
///                  children: <Widget>[
///                    Container(
///                      // A fixed-height child.
///                      color: const Color(0xffeeee00), // Yellow
///                      height: 120.0,
///                      alignment: Alignment.center,
///                      child: const Text('Fixed Height Content'),
///                    ),
///                    Expanded(
///                      // A flexible child that will grow to fit the viewport but
///                      // still be at least as big as necessary to fit its contents.
///                      child: Container(
///                        color: const Color(0xffee0000), // Red
///                        height: 120.0,
///                        alignment: Alignment.center,
///                        child: const Text('Flexible Content'),
///                      ),
///                    ),
///                  ],
///                ),
///              ),
///            ),
///          );
///        },
///      ),
///    );
///  }
203 204 205
/// ```
/// {@end-tool}
///
206 207
/// See also:
///
208 209 210 211
///  * [ListView], which handles multiple children in a scrolling list.
///  * [GridView], which handles multiple children in a scrolling grid.
///  * [PageView], for a scrollable that works page by page.
///  * [Scrollable], which handles arbitrary scrolling effects.
212
class SingleChildScrollView extends StatelessWidget {
213
  /// Creates a box in which a single widget can be scrolled.
214
  const SingleChildScrollView({
215
    Key key,
216 217
    this.scrollDirection = Axis.vertical,
    this.reverse = false,
218
    this.padding,
219
    bool primary,
220 221
    this.physics,
    this.controller,
222
    this.child,
223
    this.dragStartBehavior = DragStartBehavior.start,
224
    this.clipBehavior = Clip.hardEdge,
225
    this.restorationId,
226
  }) : assert(scrollDirection != null),
227
       assert(dragStartBehavior != null),
228
       assert(clipBehavior != null),
229 230 231 232
       assert(!(controller != null && primary == true),
          'Primary ScrollViews obtain their ScrollController via inheritance from a PrimaryScrollController widget. '
          'You cannot both set primary to true and pass an explicit controller.'
       ),
233
       primary = primary ?? controller == null && identical(scrollDirection, Axis.vertical),
234
       super(key: key);
235

236 237 238
  /// The axis along which the scroll view scrolls.
  ///
  /// Defaults to [Axis.vertical].
239 240
  final Axis scrollDirection;

241 242 243 244 245 246 247
  /// Whether the scroll view scrolls in the reading direction.
  ///
  /// For example, if the reading direction is left-to-right and
  /// [scrollDirection] is [Axis.horizontal], then the scroll view scrolls from
  /// left to right when [reverse] is false and from right to left when
  /// [reverse] is true.
  ///
248
  /// Similarly, if [scrollDirection] is [Axis.vertical], then the scroll view
249 250 251 252
  /// scrolls from top to bottom when [reverse] is false and from bottom to top
  /// when [reverse] is true.
  ///
  /// Defaults to false.
253 254
  final bool reverse;

255
  /// The amount of space by which to inset the child.
256
  final EdgeInsetsGeometry padding;
257

258 259 260 261
  /// An object that can be used to control the position to which this scroll
  /// view is scrolled.
  ///
  /// Must be null if [primary] is true.
262 263 264 265 266 267 268 269
  ///
  /// A [ScrollController] serves several purposes. It can be used to control
  /// the initial scroll position (see [ScrollController.initialScrollOffset]).
  /// It can be used to control whether the scroll view should automatically
  /// save and restore its scroll position in the [PageStorage] (see
  /// [ScrollController.keepScrollOffset]). It can be used to read the current
  /// scroll position (see [ScrollController.offset]), or change it (see
  /// [ScrollController.animateTo]).
270
  final ScrollController controller;
271

272 273 274 275 276 277
  /// Whether this is the primary scroll view associated with the parent
  /// [PrimaryScrollController].
  ///
  /// On iOS, this identifies the scroll view that will scroll to top in
  /// response to a tap in the status bar.
  ///
278
  /// Defaults to true when [scrollDirection] is vertical and [controller] is
279
  /// not specified.
280 281
  final bool primary;

282 283 284 285 286 287
  /// How the scroll view should respond to user input.
  ///
  /// For example, determines how the scroll view continues to animate after the
  /// user stops dragging the scroll view.
  ///
  /// Defaults to matching platform conventions.
288
  final ScrollPhysics physics;
289

290
  /// The widget that scrolls.
291 292
  ///
  /// {@macro flutter.widgets.child}
293
  final Widget child;
294

295 296 297
  /// {@macro flutter.widgets.scrollable.dragStartBehavior}
  final DragStartBehavior dragStartBehavior;

298 299 300 301 302
  /// {@macro flutter.widgets.Clip}
  ///
  /// Defaults to [Clip.hardEdge].
  final Clip clipBehavior;

303
  /// {@macro flutter.widgets.scrollable.restorationId}
304
  final String restorationId;
305

306
  AxisDirection _getDirection(BuildContext context) {
307
    return getAxisDirectionFromAxisReverseAndDirectionality(context, scrollDirection, reverse);
308 309 310 311
  }

  @override
  Widget build(BuildContext context) {
312
    final AxisDirection axisDirection = _getDirection(context);
313
    Widget contents = child;
314
    if (padding != null)
315 316
      contents = Padding(padding: padding, child: contents);
    final ScrollController scrollController = primary
317 318
        ? PrimaryScrollController.of(context)
        : controller;
319
    final Scrollable scrollable = Scrollable(
320
      dragStartBehavior: dragStartBehavior,
321
      axisDirection: axisDirection,
322
      controller: scrollController,
323
      physics: physics,
324
      restorationId: restorationId,
325
      viewportBuilder: (BuildContext context, ViewportOffset offset) {
326
        return _SingleChildViewport(
327 328
          axisDirection: axisDirection,
          offset: offset,
329
          child: contents,
330
          clipBehavior: clipBehavior,
331 332
        );
      },
333
    );
334
    return primary && scrollController != null
335
      ? PrimaryScrollController.none(child: scrollable)
336
      : scrollable;
337 338 339 340
  }
}

class _SingleChildViewport extends SingleChildRenderObjectWidget {
341
  const _SingleChildViewport({
342
    Key key,
343
    this.axisDirection = AxisDirection.down,
344 345 346
    this.offset,
    Widget child,
    @required this.clipBehavior,
347
  }) : assert(axisDirection != null),
348
       assert(clipBehavior != null),
349
       super(key: key, child: child);
350 351 352

  final AxisDirection axisDirection;
  final ViewportOffset offset;
353
  final Clip clipBehavior;
354 355 356

  @override
  _RenderSingleChildViewport createRenderObject(BuildContext context) {
357
    return _RenderSingleChildViewport(
358 359
      axisDirection: axisDirection,
      offset: offset,
360
      clipBehavior: clipBehavior,
361 362 363 364 365 366 367 368
    );
  }

  @override
  void updateRenderObject(BuildContext context, _RenderSingleChildViewport renderObject) {
    // Order dependency: The offset setter reads the axis direction.
    renderObject
      ..axisDirection = axisDirection
369 370
      ..offset = offset
      ..clipBehavior = clipBehavior;
371 372 373
  }
}

374
class _RenderSingleChildViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox> implements RenderAbstractViewport {
375
  _RenderSingleChildViewport({
376
    AxisDirection axisDirection = AxisDirection.down,
377
    @required ViewportOffset offset,
378
    double cacheExtent = RenderAbstractViewport.defaultCacheExtent,
379 380
    RenderBox child,
    @required Clip clipBehavior,
381 382
  }) : assert(axisDirection != null),
       assert(offset != null),
383
       assert(cacheExtent != null),
384
       assert(clipBehavior != null),
385
       _axisDirection = axisDirection,
386
       _offset = offset,
387 388
       _cacheExtent = cacheExtent,
       _clipBehavior = clipBehavior {
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    this.child = child;
  }

  AxisDirection get axisDirection => _axisDirection;
  AxisDirection _axisDirection;
  set axisDirection(AxisDirection value) {
    assert(value != null);
    if (value == _axisDirection)
      return;
    _axisDirection = value;
    markNeedsLayout();
  }

  Axis get axis => axisDirectionToAxis(axisDirection);

  ViewportOffset get offset => _offset;
  ViewportOffset _offset;
  set offset(ViewportOffset value) {
    assert(value != null);
    if (value == _offset)
      return;
    if (attached)
411
      _offset.removeListener(_hasScrolled);
412 413
    _offset = value;
    if (attached)
414
      _offset.addListener(_hasScrolled);
415
    markNeedsLayout();
416 417
  }

418 419 420 421 422 423 424 425 426 427 428
  /// {@macro flutter.rendering.viewport.cacheExtent}
  double get cacheExtent => _cacheExtent;
  double _cacheExtent;
  set cacheExtent(double value) {
    assert(value != null);
    if (value == _cacheExtent)
      return;
    _cacheExtent = value;
    markNeedsLayout();
  }

429 430 431 432 433 434 435 436 437 438 439 440 441 442
  /// {@macro flutter.widgets.Clip}
  ///
  /// Defaults to [Clip.none], and must not be null.
  Clip get clipBehavior => _clipBehavior;
  Clip _clipBehavior = Clip.none;
  set clipBehavior(Clip value) {
    assert(value != null);
    if (value != _clipBehavior) {
      _clipBehavior = value;
      markNeedsPaint();
      markNeedsSemanticsUpdate();
    }
  }

443 444 445 446 447
  void _hasScrolled() {
    markNeedsPaint();
    markNeedsSemanticsUpdate();
  }

448 449 450 451 452
  @override
  void setupParentData(RenderObject child) {
    // We don't actually use the offset argument in BoxParentData, so let's
    // avoid allocating it at all.
    if (child.parentData is! ParentData)
453
      child.parentData = ParentData();
454 455 456 457 458
  }

  @override
  void attach(PipelineOwner owner) {
    super.attach(owner);
459
    _offset.addListener(_hasScrolled);
460 461 462 463
  }

  @override
  void detach() {
464
    _offset.removeListener(_hasScrolled);
465 466 467 468 469 470
    super.detach();
  }

  @override
  bool get isRepaintBoundary => true;

471
  double get _viewportExtent {
472 473 474 475
    assert(hasSize);
    switch (axis) {
      case Axis.horizontal:
        return size.width;
476 477
      case Axis.vertical:
        return size.height;
478
    }
479
    return null;
480 481 482 483 484 485 486 487 488 489 490
  }

  double get _minScrollExtent {
    assert(hasSize);
    return 0.0;
  }

  double get _maxScrollExtent {
    assert(hasSize);
    if (child == null)
      return 0.0;
491 492
    switch (axis) {
      case Axis.horizontal:
493
        return math.max(0.0, child.size.width - size.width);
494
      case Axis.vertical:
495
        return math.max(0.0, child.size.height - size.height);
496
    }
497
    return null;
498 499 500 501 502 503 504 505 506
  }

  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
    switch (axis) {
      case Axis.horizontal:
        return constraints.heightConstraints();
      case Axis.vertical:
        return constraints.widthConstraints();
    }
507
    return null;
508 509 510 511 512
  }

  @override
  double computeMinIntrinsicWidth(double height) {
    if (child != null)
513
      return child.getMinIntrinsicWidth(height);
514 515 516 517 518 519
    return 0.0;
  }

  @override
  double computeMaxIntrinsicWidth(double height) {
    if (child != null)
520
      return child.getMaxIntrinsicWidth(height);
521 522 523 524 525 526
    return 0.0;
  }

  @override
  double computeMinIntrinsicHeight(double width) {
    if (child != null)
527
      return child.getMinIntrinsicHeight(width);
528 529 530 531 532 533
    return 0.0;
  }

  @override
  double computeMaxIntrinsicHeight(double width) {
    if (child != null)
534
      return child.getMaxIntrinsicHeight(width);
535 536 537 538 539 540 541 542 543 544
    return 0.0;
  }

  // We don't override computeDistanceToActualBaseline(), because we
  // want the default behavior (returning null). Otherwise, as you
  // scroll, it would shift in its parent if the parent was baseline-aligned,
  // which makes no sense.

  @override
  void performLayout() {
545
    final BoxConstraints constraints = this.constraints;
546 547 548
    if (child == null) {
      size = constraints.smallest;
    } else {
549 550
      child.layout(_getInnerConstraints(constraints), parentUsesSize: true);
      size = constraints.constrain(child.size);
551 552
    }

553
    offset.applyViewportDimension(_viewportExtent);
554 555 556
    offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent);
  }

557 558 559
  Offset get _paintOffset => _paintOffsetForPosition(offset.pixels);

  Offset _paintOffsetForPosition(double position) {
560 561 562
    assert(axisDirection != null);
    switch (axisDirection) {
      case AxisDirection.up:
563
        return Offset(0.0, position - child.size.height + size.height);
564
      case AxisDirection.down:
565
        return Offset(0.0, -position);
566
      case AxisDirection.left:
567
        return Offset(position - child.size.width + size.width, 0.0);
568
      case AxisDirection.right:
569
        return Offset(-position, 0.0);
570
    }
571
    return null;
572 573 574 575
  }

  bool _shouldClipAtPaintOffset(Offset paintOffset) {
    assert(child != null);
576
    return paintOffset.dx < 0 ||
577
      paintOffset.dy < 0 ||
578 579
      paintOffset.dx + child.size.width > size.width ||
      paintOffset.dy + child.size.height > size.height;
580 581 582 583 584
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    if (child != null) {
585
      final Offset paintOffset = _paintOffset;
586 587

      void paintContents(PaintingContext context, Offset offset) {
588
        context.paintChild(child, offset + paintOffset);
589 590
      }

591 592
      if (_shouldClipAtPaintOffset(paintOffset) && clipBehavior != Clip.none) {
        context.pushClipRect(needsCompositing, offset, Offset.zero & size, paintContents, clipBehavior: clipBehavior);
593 594 595 596 597 598 599 600
      } else {
        paintContents(context, offset);
      }
    }
  }

  @override
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
601
    final Offset paintOffset = _paintOffset;
602 603 604 605
    transform.translate(paintOffset.dx, paintOffset.dy);
  }

  @override
606
  Rect describeApproximatePaintClip(RenderObject child) {
607
    if (child != null && _shouldClipAtPaintOffset(_paintOffset))
608
      return Offset.zero & size;
609 610 611 612
    return null;
  }

  @override
613
  bool hitTestChildren(BoxHitTestResult result, { Offset position }) {
614
    if (child != null) {
615 616 617
      return result.addWithPaintOffset(
        offset: _paintOffset,
        position: position,
618
        hitTest: (BoxHitTestResult result, Offset transformed) {
619
          assert(transformed == position + -_paintOffset);
620
          return child.hitTest(result, position: transformed);
621 622
        },
      );
623 624 625
    }
    return false;
  }
626 627

  @override
628
  RevealedOffset getOffsetToReveal(RenderObject target, double alignment, { Rect rect }) {
629
    rect ??= target.paintBounds;
630
    if (target is! RenderBox)
631
      return RevealedOffset(offset: offset.pixels, rect: rect);
632

633
    final RenderBox targetBox = target as RenderBox;
634
    final Matrix4 transform = targetBox.getTransformTo(child);
635
    final Rect bounds = MatrixUtils.transformRect(transform, rect);
636
    final Size contentSize = child.size;
637

638 639 640
    double leadingScrollOffset;
    double targetMainAxisExtent;
    double mainAxisExtent;
641 642 643 644

    assert(axisDirection != null);
    switch (axisDirection) {
      case AxisDirection.up:
645 646 647
        mainAxisExtent = size.height;
        leadingScrollOffset = contentSize.height - bounds.bottom;
        targetMainAxisExtent = bounds.height;
648 649
        break;
      case AxisDirection.right:
650 651 652
        mainAxisExtent = size.width;
        leadingScrollOffset = bounds.left;
        targetMainAxisExtent = bounds.width;
653 654
        break;
      case AxisDirection.down:
655 656 657
        mainAxisExtent = size.height;
        leadingScrollOffset = bounds.top;
        targetMainAxisExtent = bounds.height;
658 659
        break;
      case AxisDirection.left:
660 661 662
        mainAxisExtent = size.width;
        leadingScrollOffset = contentSize.width - bounds.right;
        targetMainAxisExtent = bounds.width;
663 664 665
        break;
    }

666 667
    final double targetOffset = leadingScrollOffset - (mainAxisExtent - targetMainAxisExtent) * alignment;
    final Rect targetRect = bounds.shift(_paintOffsetForPosition(targetOffset));
668
    return RevealedOffset(offset: targetOffset, rect: targetRect);
669
  }
670 671

  @override
672
  void showOnScreen({
673 674
    RenderObject descendant,
    Rect rect,
675 676 677
    Duration duration = Duration.zero,
    Curve curve = Curves.ease,
  }) {
678 679 680 681 682 683 684 685 686
    if (!offset.allowImplicitScrolling) {
      return super.showOnScreen(
        descendant: descendant,
        rect: rect,
        duration: duration,
        curve: curve,
      );
    }

687
    final Rect newRect = RenderViewportBase.showInViewport(
688 689 690 691 692 693 694 695 696 697 698 699
      descendant: descendant,
      viewport: this,
      offset: offset,
      rect: rect,
      duration: duration,
      curve: curve,
    );
    super.showOnScreen(
      rect: newRect,
      duration: duration,
      curve: curve,
    );
700
  }
701 702 703 704 705 706

  @override
  Rect describeSemanticsClip(RenderObject child) {
    assert(axis != null);
    switch (axis) {
      case Axis.vertical:
707
        return Rect.fromLTRB(
708 709 710 711 712 713
          semanticBounds.left,
          semanticBounds.top - cacheExtent,
          semanticBounds.right,
          semanticBounds.bottom + cacheExtent,
        );
      case Axis.horizontal:
714
        return Rect.fromLTRB(
715 716 717 718 719 720
          semanticBounds.left - cacheExtent,
          semanticBounds.top,
          semanticBounds.right + cacheExtent,
          semanticBounds.bottom,
        );
    }
721
    return null;
722
  }
723
}