sheet.dart 28.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 85 86 87 88 89 90 91 92 93 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 130 131 132 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 159 160 161 162 163 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
// ignore_for_file: always_put_control_body_on_new_line

import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:sheet/src/widgets/resizable_sheet.dart';
import 'package:sheet/src/widgets/scroll_to_top_status_handler.dart';

import '../sheet.dart';

/// How to size the content of a [Sheet].
///
/// This enum is used with [Sheet.fit] to control
/// how the [BoxConstraints] passed from the sheet to the sheet's child
/// are adjusted.
///
/// See also:
///
///  * [Sheet], the widget that uses this.
enum SheetFit {
  /// The constraints passed to the child from the sheet are loosened.
  ///
  /// For example, if the sheet has expand constraints with 600 high, this would allow the child of the sheet to have any
  /// height from zero to maximum available.
  loose,

  /// The constraints passed to the stack from its parent are tightened to the
  /// biggest size allowed.
  ///
  /// For example, if the sheet has loose constraints with a height in the
  /// range 0 to 600, then the child of the sheet would all be sized
  /// as 600 high.
  expand,
}

typedef SheetDecorationBuilder = Widget Function(
    BuildContext context, Widget child);

/// A material design bottom sheet.
///
/// There are two kinds of bottom sheets in material design:
///
///  * _Persistent_. A persistent bottom sheet shows information that
///    supplements the primary content of the app. A persistent bottom sheet
///    remains visible even when the user interacts with other parts of the app.
///    Persistent bottom sheets can be created and displayed with the
///    [Sheet] widget
///
///  * _Modal_. A modal bottom sheet is an alternative to a menu or a dialog and
///    prevents the user from interacting with the rest of the app. Modal bottom
///    sheets can be created and displayed with the [SheetRoute] route
///
/// The [Sheet] can be added inside a Stack above the content.
///
/// By default the bottom sheet inherits the values provided by the
/// material theme and prioritize the ones passed in the constructor.
/// Use [Sheet.raw] if you wish to remove the Material appearance and
/// build your own
///
/// See also:
///
///  * [SheetRoute], which can be used to display a modal bottom
///    sheet route.
///  * <https://material.io/design/components/sheets-bottom.html>
class Sheet extends StatelessWidget {
  /// Creates a material bottom sheet.
  const Sheet({
    super.key,
    required this.child,
    this.controller,
    this.physics,
    this.initialExtent,
    this.minExtent,
    this.maxExtent,
    this.minInteractionExtent = 20.0,
    this.backgroundColor,
    this.clipBehavior,
    this.shape,
    this.elevation,
    this.fit = SheetFit.loose,
    this.resizable = false,
    this.padding = EdgeInsets.zero,
    this.minResizableExtent,
  }) : decorationBuilder = null;

  /// Creates a bottom sheet with no default appearance.
  const Sheet.raw({
    super.key,
    required this.child,
    SheetDecorationBuilder? decorationBuilder,
    this.controller,
    this.physics,
    this.initialExtent,
    this.minExtent,
    this.maxExtent,
    this.minInteractionExtent = 20.0,
    this.fit = SheetFit.loose,
    this.resizable = false,
    this.padding = EdgeInsets.zero,
    this.minResizableExtent,
  })  : decorationBuilder = decorationBuilder ?? _emptyDecorationBuilder,
        shape = null,
        elevation = null,
        backgroundColor = null,
        clipBehavior = null;

  final Widget child;

  /// {@macro flutter.widgets.sheet.physics}
  final SheetPhysics? physics;

  /// {@macro flutter.widgets.sheet.controller}
  final SheetController? controller;

  /// Empty space to surround the [child].
  final EdgeInsets padding;

  /// The initial height to use when displaying the widget.
  ///
  /// This value will be clamped between [minExtent] and [maxExtent]
  ///
  /// The default value is `0`.
  final double? initialExtent;

  /// The minimum height to use when displaying the widget.
  ///
  /// The default value is `0`.
  final double? minExtent;

  /// The maximum height to use when displaying the widget.
  ///
  /// This value will be clamped to be as maximum the parent container's height
  ///
  /// The default value is `double.infinity`.
  final double? maxExtent;

  /// The height area of the minimum interaction zone to allow to
  /// drag up the sheet when it is closed
  ///
  /// The default value is `0`.
  final double minInteractionExtent;

  /// If true, the content of the sheet will be resized to fit the
  /// available visible space.
  /// If false, the content of the sheet will keep the same size and
  /// be translated vertically
  ///
  /// The default value is `false`.
  final bool resizable;

  /// If resizable true, the minimum height that the sheet can be.
  /// The content of the sheet will be resized to fit the
  /// available visible space until this value, after that will be
  /// translated keeping this minimum height.
  ///
  /// The default value is `0`.
  final double? minResizableExtent;

  /// How to size the child in the sheet.
  ///
  /// The constraints passed into the [Sheet] from its parent are either
  /// loosened ([SheetFit.loose]) or tightened to their biggest size
  /// ([SheetFit.expand]).
  final SheetFit fit;

  /// The state from the closest instance of this class that encloses the given context.
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// ScrollableState scrollable = Scrollable.of(context);
  /// ```
  ///
  /// Calling this method will create a dependency on the closest [SheetScrollable]
  /// in the [context], if there is one.
  static SheetState? of(BuildContext context) {
    return SheetScrollable.of(context);
  }

  /// The bottom sheet's background color.
  ///
  /// Defines the bottom sheet's [Material.color].
  ///
  /// Defaults to null and falls back to [Material]'s default.
  final Color? backgroundColor;

  /// The z-coordinate at which to place this material relative to its parent.
  ///
  /// This controls the size of the shadow below the material.
  ///
  /// Defaults to 0. The value is non-negative.
  final double? elevation;

  /// The shape of the bottom sheet.
  ///
  /// Defines the bottom sheet's [Material.shape].
  ///
  /// Defaults to null and falls back to [Material]'s default.
  final ShapeBorder? shape;

  /// {@macro flutter.material.Material.clipBehavior}
  ///
  /// Defines the bottom sheet's [Material.clipBehavior].
  ///
  /// Use this property to enable clipping of content when the bottom sheet has
  /// a custom [shape] and the content can extend past this shape. For example,
  /// a bottom sheet with rounded corners and an edge-to-edge [Image] at the
  /// top.
  ///
  /// If this property is null then [BottomSheetThemeData.clipBehavior] of
  /// [ThemeData.bottomSheetTheme] is used. If that's null then the behavior
  /// will be [Clip.none].
  final Clip? clipBehavior;

  /// Wraps the child in a custom sheet decoration appearance
  /// If null, the sheet has material appareance
  ///
  /// The default value is null.
  final SheetDecorationBuilder? decorationBuilder;

  static Widget _emptyDecorationBuilder(BuildContext context, Widget child) {
    return child;
  }

  Widget decorationBuild(BuildContext context, Widget child) {
    final SheetDecorationBuilder decorationBuilder = this.decorationBuilder ??
        (BuildContext context, Widget child) {
          final BottomSheetThemeData bottomSheetTheme =
              Theme.of(context).bottomSheetTheme;
          final Color? color =
              backgroundColor ?? bottomSheetTheme.backgroundColor;
          final double elevation =
              this.elevation ?? bottomSheetTheme.elevation ?? 0;
          final ShapeBorder? shape = this.shape ?? bottomSheetTheme.shape;
          final Clip clipBehavior =
              this.clipBehavior ?? bottomSheetTheme.clipBehavior ?? Clip.none;

          return Material(
            color: color,
            elevation: elevation,
            shape: shape,
            clipBehavior: clipBehavior,
            child: child,
          );
        };
    return decorationBuilder(context, child);
  }

  @override
  Widget build(BuildContext context) {
    final SheetController? effectiveController =
        controller ?? DefaultSheetController.of(context);
    final double? initialExtent =
        this.initialExtent?.clamp(minExtent ?? 0, maxExtent ?? double.infinity);
    return SheetScrollable(
      initialExtent: initialExtent,
      minInteractionExtent: minInteractionExtent,
      physics: physics,
      controller: effectiveController,
      axisDirection: AxisDirection.down,
      scrollBehavior: SheetBehavior(),
      viewportBuilder: (BuildContext context, ViewportOffset offset) {
        return _DefaultSheetScrollController(
          child: ScrollToTopStatusBarHandler(
            child: SheetViewport(
              clipBehavior: Clip.antiAlias,
              axisDirection: AxisDirection.down,
              offset: offset,
              minExtent: minExtent,
              maxExtent: maxExtent,
              fit: fit,
              child: Padding(
                padding: padding,
                child: ResizableSheetChild(
                  resizable: resizable,
                  offset: offset,
                  minExtent: minResizableExtent ?? 0,
                  child: Builder(
                    key: const Key('_sheet_child'),
                    builder: (BuildContext context) {
                      return decorationBuild(context, child);
                    },
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}

class _DefaultSheetScrollController extends StatelessWidget {
  const _DefaultSheetScrollController({required this.child});

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return PrimaryScrollController(
      controller: SheetScrollable.of(context)!.position.scrollController,
      child: child,
    );
  }
}

/// A [ScrollController] suitable for use in a [SheetPosition] created
/// by a [Sheet].
///
/// If a [Sheet] contains content that is exceeds the height
/// of its container, this controller will allow the sheet to both be dragged to
/// fill the container and then scroll the child content.
///
/// See also:
///
///  * [SheetPosition], which manages the positioning logic for
///    this controller.
class SheetController extends ScrollController {
  SheetController({super.debugLabel}) : super(initialScrollOffset: 0);

  final ProxyAnimation _animation = ProxyAnimation();
  Animation<double> get animation => _animation;

  void updateAnimation() {
    if (hasClients) {
      final ScrollPosition position = positions.first;
      if (position is SheetPosition) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          _animation.parent = position.animation;
        });

        return;
      }
    }
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _animation.parent = kAlwaysDismissedAnimation;
    });
    return;
  }

  @override
  SheetPosition get position {
    return super.position as SheetPosition;
  }

  @override
  void attach(ScrollPosition position) {
    super.attach(position);
    updateAnimation();
  }

  @override
  void detach(ScrollPosition position) {
    super.detach(position);
    updateAnimation();
  }

  @override
  SheetPosition createScrollPosition(
    ScrollPhysics physics,
    ScrollContext context,
    ScrollPosition? oldPosition,
  ) {
    final double? initialPixels =
        (context is SheetContext) ? context.initialExtent : null;
    return SheetPosition(
      physics: physics,
      context: context as SheetContext,
      oldPosition: oldPosition,
      initialPixels: initialPixels ?? 0.0,
    );
  }

  Future<void> relativeAnimateTo(double offset,
      {required Duration duration, required Curve curve}) async {
    assert(positions.isNotEmpty,
        'ScrollController not attached to any scroll views.');
    await Future.wait<void>(<Future<void>>[
      for (final ScrollPosition position in positions)
        (position as SheetPosition)
            .relativeAnimateTo(offset, duration: duration, curve: curve),
    ]);
  }

  void relativeJumpTo(double offset) {
    assert(positions.isNotEmpty,
        'ScrollController not attached to any scroll views.');
    for (final ScrollPosition position in positions) {
      (position as SheetPosition).relativeJumpTo(offset);
    }
  }
}

/// A scroll position that manages scroll activities for
/// [_SheetScrollController].
///
/// This class is a concrete subclass of [ScrollPosition] logic that handles a
/// single [ScrollContext], such as a [Scrollable]. An instance of this class
/// manages [ScrollActivity] instances, which changes the
/// [SheetPosition.currentExtent] or visible content offset in the
/// [Scrollable]'s [Viewport]
///
/// See also:
///
///  * [_SheetScrollController], which uses this as its [ScrollPosition].
class SheetPosition extends ScrollPositionWithSingleContext {
  SheetPosition({
    required super.physics,
    required SheetContext super.context,
    super.initialPixels = 0.0,
    super.keepScrollOffset = true,
    super.oldPosition,
    super.debugLabel,
  });

  late final SheetPrimaryScrollController _scrollController =
      SheetPrimaryScrollController(sheetContext: context as SheetContext);

  SheetPrimaryScrollController get scrollController => _scrollController;

  Future<void> relativeAnimateTo(double to,
      {required Duration duration, required Curve curve}) {
    assert(to >= 0 && to <= 1);
    return super.animateTo(
      pixelsFromRelativeOffset(to, minScrollExtent, maxScrollExtent),
      duration: duration,
      curve: curve,
    );
  }

  @override
  Future<void> animateTo(double to,
      {required Duration duration, required Curve curve}) {
    return super.animateTo(to.clamp(minScrollExtent, maxScrollExtent),
        duration: duration, curve: curve);
  }

  void relativeJumpTo(double to) {
    assert(to >= 0 && to <= 1);
    final value =
        pixelsFromRelativeOffset(to, minScrollExtent, maxScrollExtent);
    return super.jumpTo(value);
  }

  @override
  void jumpTo(double value) {
    final double pixels = value.clamp(minScrollExtent, maxScrollExtent);
    return super.jumpTo(pixels);
  }

  bool _preventingDrag = false;
  bool get preventingDrag => _preventingDrag;
  void preventDrag() {
    _preventingDrag = true;
  }

  void stopPreventingDrag() {
    _preventingDrag = false;
  }

  late final AnimationController _controller =
      AnimationController(vsync: context.vsync);

  Animation<double> get animation => _controller;

  @override
  double setPixels(double newPixels) {
    _controller.value = relativeOffsetFromPixels(
      newPixels,
      minScrollExtent,
      maxScrollExtent,
    );
    return super.setPixels(newPixels);
  }

  @override
  void forcePixels(double value) {
    _controller.value = relativeOffsetFromPixels(
      value,
      minScrollExtent,
      maxScrollExtent,
    );
    super.forcePixels(value);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    _controller.dispose();
    super.dispose();
  }

  @override
  bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {
    // Clamp initial extent to maxScrollExtent
    if (!hasContentDimensions) {
      correctPixels(pixels.clamp(minScrollExtent, maxScrollExtent));
      _controller.value = relativeOffsetFromPixels(
        pixels,
        minScrollExtent,
        maxScrollExtent,
      );
    }
    return super.applyContentDimensions(minScrollExtent, maxScrollExtent);
  }

  static double relativeOffsetFromPixels(
    double pixels,
    double minScrollExtent,
    double maxScrollExtent,
  ) {
    if (minScrollExtent == maxScrollExtent) return 1;
    final value =
        ((pixels - minScrollExtent) / (maxScrollExtent - minScrollExtent))
            .clamp(0.0, 1.0);
    return value;
  }

  static double pixelsFromRelativeOffset(
    double offset,
    double minScrollExtent,
    double maxScrollExtent,
  ) {
    return minScrollExtent + offset * (maxScrollExtent - minScrollExtent);
  }
}

class SheetViewport extends SingleChildRenderObjectWidget {
  const SheetViewport({
    super.key,
    this.axisDirection = AxisDirection.down,
    required this.offset,
    this.minExtent,
    this.maxExtent,
    super.child,
    required this.fit,
    required this.clipBehavior,
  });

  final AxisDirection axisDirection;
  final ViewportOffset offset;
  final Clip clipBehavior;
  final double? minExtent;
  final double? maxExtent;
  final SheetFit fit;

  @override
  RenderSheetViewport createRenderObject(BuildContext context) {
    return RenderSheetViewport(
      axisDirection: axisDirection,
      offset: offset,
      clipBehavior: clipBehavior,
      minExtent: minExtent,
      maxExtent: maxExtent,
      fit: fit,
    );
  }

  @override
  void updateRenderObject(
      BuildContext context, RenderSheetViewport renderObject) {
    // Order dependency: The offset setter reads the axis direction.
    renderObject
      ..axisDirection = axisDirection
      ..offset = offset
      ..clipBehavior = clipBehavior
      ..minExtent = minExtent
      ..maxExtent = maxExtent
      ..fit = fit;
  }
}

class RenderSheetViewport extends RenderBox
    with RenderObjectWithChildMixin<RenderBox>
    implements RenderAbstractViewport {
  RenderSheetViewport({
    AxisDirection axisDirection = AxisDirection.down,
    required ViewportOffset offset,
    double cacheExtent = RenderAbstractViewport.defaultCacheExtent,
    RenderBox? child,
    required Clip clipBehavior,
    SheetFit fit = SheetFit.expand,
    double? minExtent,
    double? maxExtent,
  })  : _axisDirection = axisDirection,
        _offset = offset,
        _fit = fit,
        _minExtent = minExtent,
        _maxExtent = maxExtent,
        _cacheExtent = cacheExtent,
        _clipBehavior = clipBehavior {
    this.child = child;
  }

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

  Axis get axis => axisDirectionToAxis(axisDirection);

  ViewportOffset get offset => _offset;
  ViewportOffset _offset;
  set offset(ViewportOffset value) {
    if (value == _offset) return;
    if (attached) _offset.removeListener(_hasDragged);
    _offset = value;
    if (attached) _offset.addListener(_hasDragged);
    markNeedsLayout();
  }

  /// {@macro flutter.rendering.RenderViewportBase.cacheExtent}
  double get cacheExtent => _cacheExtent;
  double _cacheExtent;
  set cacheExtent(double value) {
    if (value == _cacheExtent) return;
    _cacheExtent = value;
    markNeedsLayout();
  }

  /// {@macro flutter.rendering.RenderViewportBase.cacheExtent}
  SheetFit get fit => _fit;
  SheetFit _fit;
  set fit(SheetFit value) {
    if (value == _fit) return;
    _fit = value;
    markNeedsLayout();
  }

  /// {@macro flutter.material.Material.clipBehavior}
  ///
  /// Defaults to [Clip.none], and must not be null.
  Clip get clipBehavior => _clipBehavior;
  Clip _clipBehavior = Clip.none;
  set clipBehavior(Clip value) {
    if (value != _clipBehavior) {
      _clipBehavior = value;
      markNeedsPaint();
      markNeedsSemanticsUpdate();
    }
  }

  void _hasDragged() {
    if (!_isOverflow && offset.pixels > child!.size.height) {
      _childExtentBeforeOverflow ??= child!.size.height;
      _isOverflow = true;
      markNeedsLayout();
    } else if (isOverflow && offset.pixels < _childExtentBeforeOverflow!) {
      _childExtentBeforeOverflow = null;
      _isOverflow = false;
      markNeedsLayout();
    }
    markNeedsPaint();
    markNeedsSemanticsUpdate();
  }

  @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) child.parentData = ParentData();
  }

  @override
  void attach(PipelineOwner owner) {
    super.attach(owner);
    _offset.addListener(_hasDragged);
  }

  @override
  void detach() {
    _offset.removeListener(_hasDragged);
    super.detach();
  }

  @override
  bool get isRepaintBoundary => true;

  double? get minExtent => _minExtent;
  double? _minExtent;
  set minExtent(double? value) {
    if (value != _minExtent) {
      _minExtent = value;
      markNeedsLayout();
    }
  }

  double? get maxExtent => _maxExtent;
  double? _maxExtent;
  set maxExtent(double? value) {
    if (value != _maxExtent) {
      _maxExtent = value;
      markNeedsLayout();
    }
  }

  double get _viewportExtent {
    assert(hasSize);
    switch (axis) {
      case Axis.horizontal:
        return size.width;
      case Axis.vertical:
        return size.height;
    }
  }

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

  double get _maxScrollExtent {
    assert(hasSize);
    if (_childExtentBeforeOverflow != null) return _childExtentBeforeOverflow!;
    if (child == null) return 0.0;
    switch (axis) {
      case Axis.horizontal:
        return math.max(0.0, child!.size.width - size.width);
      case Axis.vertical:
        return math.max(0.0, child!.size.height);
    }
  }

  double? _childExtentBeforeOverflow;
  bool _isOverflow = false;
  bool get isOverflow => _isOverflow;

  //BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
  //  switch (axis) {
  //    case Axis.horizontal:
  //      return constraints.heightConstraints();
  //    case Axis.vertical:
  //      return constraints.widthConstraints();
  //  }
  //}

  @override
  double computeMinIntrinsicWidth(double height) {
    return constraints.maxHeight;
  }

  @override
  double computeMaxIntrinsicWidth(double height) {
    return constraints.maxWidth;
  }

  @override
  double computeMinIntrinsicHeight(double width) {
    return constraints.maxHeight;
  }

  @override
  double computeMaxIntrinsicHeight(double width) {
    return constraints.maxHeight;
  }

  // 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
  Size computeDryLayout(BoxConstraints constraints) {
    if (child == null) {
      return constraints.smallest;
    }
    return constraints.biggest;
  }

  @override
  void performLayout() {
    final BoxConstraints constraints = this.constraints;
    if (child == null) {
      size = constraints.smallest;
    } else {
      final bool expand = fit == SheetFit.expand;
      final double maxExtent = this.maxExtent ?? constraints.maxHeight;
      double maxHeight = maxExtent.clamp(0, constraints.maxHeight);
      double minHeight = expand ? maxHeight : 0;

      if (isOverflow) {
        final double overflowHeight =
            _childExtentBeforeOverflow! + offset.pixels;
        maxHeight = overflowHeight;
        minHeight = overflowHeight;
      }

      final BoxConstraints childConstraints = BoxConstraints(
        minHeight: minHeight,
        maxHeight: maxHeight,
        minWidth: constraints.minWidth,
        maxWidth: constraints.maxWidth,
      );
      child!.layout(childConstraints, parentUsesSize: true);
      size = constraints.biggest;
    }

    offset.applyViewportDimension(_viewportExtent);
    offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent);
  }

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

  Offset _paintOffsetForPosition(double position) {
    switch (axisDirection) {
      case AxisDirection.up:
        return Offset(0.0, position - child!.size.height + size.height);
      case AxisDirection.down:
        return Offset(0.0, -position + size.height);
      case AxisDirection.left:
        return Offset(position - child!.size.width + size.width, 0.0);
      case AxisDirection.right:
        return Offset(-position, 0.0);
    }
  }

  bool _shouldClipAtPaintOffset(Offset paintOffset) {
    assert(child != null);
    return paintOffset.dx < 0 ||
        paintOffset.dy < 0 ||
        paintOffset.dx + child!.size.width > size.width ||
        paintOffset.dy + child!.size.height > size.height;
  }

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

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

      if (_shouldClipAtPaintOffset(paintOffset) && clipBehavior != Clip.none) {
        _clipRectLayer.layer = context.pushClipRect(
          needsCompositing,
          offset,
          Offset.zero & size,
          paintContents,
          clipBehavior: clipBehavior,
          oldLayer: _clipRectLayer.layer,
        );
      } else {
        _clipRectLayer.layer = null;
        paintContents(context, offset);
      }
    }
  }

  final LayerHandle<ClipRectLayer> _clipRectLayer =
      LayerHandle<ClipRectLayer>();

  @override
  void dispose() {
    _clipRectLayer.layer = null;
    super.dispose();
  }

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

  @override
  Rect? describeApproximatePaintClip(RenderObject? child) {
    if (child != null && _shouldClipAtPaintOffset(_paintOffset)) {
      return Offset.zero & size;
    }
    return null;
  }

  @override
  bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
    if (child != null) {
      return result.addWithPaintOffset(
        offset: _paintOffset,
        position: position,
        hitTest: (BoxHitTestResult result, Offset? transformed) {
          assert(transformed == position + -_paintOffset);
          return child!.hitTest(result, position: transformed!);
        },
      );
    }
    return false;
  }

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

    final RenderBox targetBox = target;
    final Matrix4 transform = targetBox.getTransformTo(child);
    final Rect bounds = MatrixUtils.transformRect(transform, rect);
    final Size contentSize = child!.size;

    final double leadingScrollOffset;
    final double targetMainAxisExtent;
    final double mainAxisExtent;

    switch (axisDirection) {
      case AxisDirection.up:
        mainAxisExtent = size.height;
        leadingScrollOffset = contentSize.height - bounds.bottom;
        targetMainAxisExtent = bounds.height;
        break;
      case AxisDirection.right:
        mainAxisExtent = size.width;
        leadingScrollOffset = bounds.left;
        targetMainAxisExtent = bounds.width;
        break;
      case AxisDirection.down:
        mainAxisExtent = size.height;
        leadingScrollOffset = bounds.top;
        targetMainAxisExtent = bounds.height;
        break;
      case AxisDirection.left:
        mainAxisExtent = size.width;
        leadingScrollOffset = contentSize.width - bounds.right;
        targetMainAxisExtent = bounds.width;
        break;
    }

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

  @override
  void showOnScreen({
    RenderObject? descendant,
    Rect? rect,
    Duration duration = Duration.zero,
    Curve curve = Curves.ease,
  }) {
    return;
    // TODO(jaime): check showOnScreen method behaves when keyboard appears on
    // the screen
    // if (!offset.allowImplicitScrolling) {
    //   return super.showOnScreen(
    //     descendant: descendant,
    //     rect: rect,
    //     duration: duration,
    //     curve: curve,
    //   );
    // }
    //
    // final Rect? newRect = RenderViewportBase.showInViewport(
    //   descendant: descendant,
    //   viewport: this,
    //   offset: offset,
    //   rect: rect,
    //   duration: duration,
    //   curve: curve,
    // );
    // super.showOnScreen(
    //   rect: newRect,
    //   duration: duration,
    //   curve: curve,
    // );
  }

  @override
  Rect describeSemanticsClip(RenderObject child) {
    switch (axis) {
      case Axis.vertical:
        return Rect.fromLTRB(
          semanticBounds.left,
          semanticBounds.top - cacheExtent,
          semanticBounds.right,
          semanticBounds.bottom + cacheExtent,
        );
      case Axis.horizontal:
        return Rect.fromLTRB(
          semanticBounds.left - cacheExtent,
          semanticBounds.top,
          semanticBounds.right + cacheExtent,
          semanticBounds.bottom,
        );
    }
  }
}