tracking_scroll_controller_test.dart 2.09 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 10
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
11
  testWidgets('TrackingScrollController saves offset', (WidgetTester tester) async {
12
    final TrackingScrollController controller = TrackingScrollController();
13
    const double listItemHeight = 100.0;
14 15

    await tester.pumpWidget(
16
      Directionality(
17
        textDirection: TextDirection.ltr,
18
        child: PageView.builder(
19
          itemBuilder: (BuildContext context, int index) {
20
            return ListView(
Ian Hickson's avatar
Ian Hickson committed
21
              controller: controller,
22
              children: List<Widget>.generate(
Ian Hickson's avatar
Ian Hickson committed
23
                10,
24
                (int i) => Container(
Ian Hickson's avatar
Ian Hickson committed
25
                  height: listItemHeight,
26
                  child: Text('Page$index-Item$i'),
Ian Hickson's avatar
Ian Hickson committed
27 28 29
                ),
              ).toList(),
            );
30 31 32 33 34 35 36 37 38 39 40
          },
        ),
      ),
    );

    expect(find.text('Page0-Item1'), findsOneWidget);
    expect(find.text('Page1-Item1'), findsNothing);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsNothing);

    controller.jumpTo(listItemHeight + 10);
41
    await tester.pumpAndSettle();
42

Ian Hickson's avatar
Ian Hickson committed
43
    await tester.fling(find.text('Page0-Item1'), const Offset(-100.0, 0.0), 10000.0);
44
    await tester.pumpAndSettle();
45 46 47 48 49 50

    expect(find.text('Page0-Item1'), findsNothing);
    expect(find.text('Page1-Item1'), findsOneWidget);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsNothing);

Ian Hickson's avatar
Ian Hickson committed
51
    await tester.fling(find.text('Page1-Item1'), const Offset(-100.0, 0.0), 10000.0);
52
    await tester.pumpAndSettle();
53 54 55 56 57 58

    expect(find.text('Page0-Item1'), findsNothing);
    expect(find.text('Page1-Item1'), findsNothing);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsOneWidget);

Ian Hickson's avatar
Ian Hickson committed
59
    await tester.pumpWidget(const Text('Another page', textDirection: TextDirection.ltr));
60 61 62 63

    expect(controller.initialScrollOffset, 0.0);
  });
}