slivers_appbar_scrolling_test.dart 6.34 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
Ian Hickson's avatar
Ian Hickson committed
8 9

void verifyPaintPosition(GlobalKey key, Offset ideal) {
10
  final RenderObject target = key.currentContext!.findRenderObject()!;
Dan Field's avatar
Dan Field committed
11
  expect(target.parent, isA<RenderViewport>());
12
  final SliverPhysicalParentData parentData = target.parentData! as SliverPhysicalParentData;
13
  final Offset actual = parentData.paintOffset;
Ian Hickson's avatar
Ian Hickson committed
14 15 16 17 18 19 20
  expect(actual, ideal);
}

void main() {
  testWidgets('Sliver appbars - scrolling', (WidgetTester tester) async {
    GlobalKey key1, key2, key3, key4, key5;
    await tester.pumpWidget(
21
      Directionality(
22
        textDirection: TextDirection.ltr,
23
        child: CustomScrollView(
24
          slivers: <Widget>[
25 26 27 28 29
            BigSliver(key: key1 = GlobalKey()),
            SliverPersistentHeader(key: key2 = GlobalKey(), delegate: TestDelegate()),
            SliverPersistentHeader(key: key3 = GlobalKey(), delegate: TestDelegate()),
            BigSliver(key: key4 = GlobalKey()),
            BigSliver(key: key5 = GlobalKey()),
30 31
          ],
        ),
Ian Hickson's avatar
Ian Hickson committed
32 33
      ),
    );
34
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
35
    final double max = RenderBigSliver.height * 3.0 + TestDelegate().maxExtent * 2.0 - 600.0; // 600 is the height of the test viewport
Ian Hickson's avatar
Ian Hickson committed
36 37 38 39 40
    assert(max < 10000.0);
    expect(max, 1450.0);
    expect(position.pixels, 0.0);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
41
    position.animateTo(10000.0, curve: Curves.linear, duration: const Duration(minutes: 1));
42
    await tester.pumpAndSettle(const Duration(milliseconds: 10));
Ian Hickson's avatar
Ian Hickson committed
43 44 45
    expect(position.pixels, max);
    expect(position.minScrollExtent, 0.0);
    expect(position.maxScrollExtent, max);
46 47 48 49
    verifyPaintPosition(key1, Offset.zero);
    verifyPaintPosition(key2, Offset.zero);
    verifyPaintPosition(key3, Offset.zero);
    verifyPaintPosition(key4, Offset.zero);
50
    verifyPaintPosition(key5, const Offset(0.0, 50.0));
Ian Hickson's avatar
Ian Hickson committed
51 52 53
  });

  testWidgets('Sliver appbars - scrolling off screen', (WidgetTester tester) async {
54 55
    final GlobalKey key = GlobalKey();
    final TestDelegate delegate = TestDelegate();
Ian Hickson's avatar
Ian Hickson committed
56
    await tester.pumpWidget(
57
      Directionality(
58
        textDirection: TextDirection.ltr,
59
        child: CustomScrollView(
60 61
          slivers: <Widget>[
            const BigSliver(),
62
            SliverPersistentHeader(key: key, delegate: delegate),
63 64 65 66
            const BigSliver(),
            const BigSliver(),
          ],
        ),
Ian Hickson's avatar
Ian Hickson committed
67 68
      ),
    );
69
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
70
    position.animateTo(RenderBigSliver.height + delegate.maxExtent - 5.0, curve: Curves.linear, duration: const Duration(minutes: 1));
71
    await tester.pumpAndSettle(const Duration(milliseconds: 1000));
72
    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container));
73
    final Rect rect = Rect.fromPoints(box.localToGlobal(Offset.zero), box.localToGlobal(box.size.bottomRight(Offset.zero)));
Dan Field's avatar
Dan Field committed
74
    expect(rect, equals(const Rect.fromLTWH(0.0, -195.0, 800.0, 200.0)));
Ian Hickson's avatar
Ian Hickson committed
75
  });
76 77

  testWidgets('Sliver appbars - scrolling - overscroll gap is below header', (WidgetTester tester) async {
78 79 80 81 82 83 84
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          physics: const BouncingScrollPhysics(),
          slivers: <Widget>[
            SliverPersistentHeader(delegate: TestDelegate()),
85
            SliverList(
86
              delegate: SliverChildListDelegate(<Widget>[
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
                const SizedBox(
                  height: 300.0,
                  child: Text('X'),
                ),
              ]),
            ),
          ],
        ),
      ),
    );

    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 200.0));

    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
    position.jumpTo(-50.0);
    await tester.pump();

    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 250.0));
  });

  testWidgets('Sliver appbars const child delegate - scrolling - overscroll gap is below header', (WidgetTester tester) async {
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          physics: const BouncingScrollPhysics(),
          slivers: <Widget>[
            SliverPersistentHeader(delegate: TestDelegate()),
            const SliverList(
              delegate: SliverChildListDelegate.fixed(<Widget>[
119
                SizedBox(
120
                  height: 300.0,
121
                  child: Text('X'),
122 123 124 125 126
                ),
              ]),
            ),
          ],
        ),
127 128 129
      ),
    );

130 131
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 200.0));
132

133
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
134 135 136
    position.jumpTo(-50.0);
    await tester.pump();

137 138
    expect(tester.getTopLeft(find.byType(Container)), Offset.zero);
    expect(tester.getTopLeft(find.text('X')), const Offset(0.0, 250.0));
139
  });
Ian Hickson's avatar
Ian Hickson committed
140 141
}

142
class TestDelegate extends SliverPersistentHeaderDelegate {
Ian Hickson's avatar
Ian Hickson committed
143 144 145 146
  @override
  double get maxExtent => 200.0;

  @override
147 148 149 150
  double get minExtent => 200.0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
151
    return Container(height: maxExtent);
Ian Hickson's avatar
Ian Hickson committed
152 153 154 155 156 157 158 159 160
  }

  @override
  bool shouldRebuild(TestDelegate oldDelegate) => false;
}


class RenderBigSliver extends RenderSliver {
  static const double height = 550.0;
161
  double get paintExtent => (height - constraints.scrollOffset).clamp(0.0, constraints.remainingPaintExtent);
Ian Hickson's avatar
Ian Hickson committed
162 163 164

  @override
  void performLayout() {
165
    geometry = SliverGeometry(
Ian Hickson's avatar
Ian Hickson committed
166 167 168 169 170 171 172 173
      scrollExtent: height,
      paintExtent: paintExtent,
      maxPaintExtent: height,
    );
  }
}

class BigSliver extends LeafRenderObjectWidget {
174
  const BigSliver({ Key? key }) : super(key: key);
Ian Hickson's avatar
Ian Hickson committed
175 176
  @override
  RenderBigSliver createRenderObject(BuildContext context) {
177
    return RenderBigSliver();
Ian Hickson's avatar
Ian Hickson committed
178 179
  }
}