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

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

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

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

    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);
39
    await tester.pumpAndSettle();
40

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

    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
49
    await tester.fling(find.text('Page1-Item1'), const Offset(-100.0, 0.0), 10000.0);
50
    await tester.pumpAndSettle();
51 52 53 54 55 56

    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
57
    await tester.pumpWidget(const Text('Another page', textDirection: TextDirection.ltr));
58 59 60 61

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