scrollable_of_test.dart 3.12 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

class ScrollPositionListener extends StatefulWidget {
9
  const ScrollPositionListener({ Key key, this.child, this.log}) : super(key: key);
10 11 12 13 14

  final Widget child;
  final ValueChanged<String> log;

  @override
15
  _ScrollPositionListenerState createState() => _ScrollPositionListenerState();
16 17 18 19 20 21 22 23 24 25 26
}

class _ScrollPositionListenerState extends State<ScrollPositionListener> {
  ScrollPosition _position;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _position?.removeListener(listener);
    _position = Scrollable.of(context)?.position;
    _position?.addListener(listener);
27
    widget.log('didChangeDependencies ${_position?.pixels?.toStringAsFixed(1)}');
28 29 30 31 32 33 34 35 36 37 38 39
  }

  @override
  void dispose() {
    _position?.removeListener(listener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => widget.child;

  void listener() {
40
    widget.log('listener ${_position?.pixels?.toStringAsFixed(1)}');
41 42 43 44 45 46 47
  }

}

void main() {
  testWidgets('Scrollable.of() dependent rebuilds when Scrollable position changes', (WidgetTester tester) async {
    String logValue;
48
    final ScrollController controller = ScrollController();
49 50 51 52 53

    // Changing the SingleChildScrollView's physics causes the
    // ScrollController's ScrollPosition to be rebuilt.

    Widget buildFrame(ScrollPhysics physics) {
54
      return SingleChildScrollView(
55 56
        controller: controller,
        physics: physics,
57
        child: ScrollPositionListener(
58 59 60 61 62 63 64
          log: (String s) { logValue = s; },
          child: const SizedBox(height: 400.0),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(null));
Ian Hickson's avatar
Ian Hickson committed
65
    expect(logValue, 'didChangeDependencies 0.0');
66 67

    controller.jumpTo(100.0);
Ian Hickson's avatar
Ian Hickson committed
68
    expect(logValue, 'listener 100.0');
69 70

    await tester.pumpWidget(buildFrame(const ClampingScrollPhysics()));
Ian Hickson's avatar
Ian Hickson committed
71
    expect(logValue, 'didChangeDependencies 100.0');
72 73

    controller.jumpTo(200.0);
Ian Hickson's avatar
Ian Hickson committed
74
    expect(logValue, 'listener 200.0');
75 76

    controller.jumpTo(300.0);
Ian Hickson's avatar
Ian Hickson committed
77
    expect(logValue, 'listener 300.0');
78 79

    await tester.pumpWidget(buildFrame(const BouncingScrollPhysics()));
Ian Hickson's avatar
Ian Hickson committed
80
    expect(logValue, 'didChangeDependencies 300.0');
81 82

    controller.jumpTo(400.0);
Ian Hickson's avatar
Ian Hickson committed
83
    expect(logValue, 'listener 400.0');
84
  });
85 86 87 88 89 90 91 92 93

  testWidgets('Scrollable.of() is possible using ScrollNotification context', (WidgetTester tester) async {
    ScrollNotification notification;

    await tester.pumpWidget(NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification value) {
        notification = value;
        return false;
      },
94
      child: const SingleChildScrollView(
95 96
        child: SizedBox(height: 1200.0),
      ),
97 98 99 100 101 102 103 104
    ));

    await tester.startGesture(const Offset(100.0, 100.0));
    await tester.pump(const Duration(seconds: 1));

    final StatefulElement scrollableElement = find.byType(Scrollable).evaluate().first;
    expect(Scrollable.of(notification.context), equals(scrollableElement.state));
  });
105
}