scrollable_of_test.dart 2.4 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 15 16 17 18 19 20 21 22 23 24 25 26

  final Widget child;
  final ValueChanged<String> log;

  @override
  _ScrollPositionListenerState createState() => new _ScrollPositionListenerState();
}

class _ScrollPositionListenerState extends State<ScrollPositionListener> {
  ScrollPosition _position;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _position?.removeListener(listener);
    _position = Scrollable.of(context)?.position;
    _position?.addListener(listener);
Ian Hickson's avatar
Ian Hickson committed
27
    widget.log('didChangeDependencies ${_position?.pixels}');
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() {
Ian Hickson's avatar
Ian Hickson committed
40
    widget.log('listener ${_position?.pixels}');
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  }

}

void main() {
  testWidgets('Scrollable.of() dependent rebuilds when Scrollable position changes', (WidgetTester tester) async {
    String logValue;
    final ScrollController controller = new ScrollController();

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

    Widget buildFrame(ScrollPhysics physics) {
      return new SingleChildScrollView(
        controller: controller,
        physics: physics,
        child: new ScrollPositionListener(
          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
  });
}