state_setting_in_scrollables_test.dart 3.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// 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' hide TypeMatcher;
import 'package:flutter/widgets.dart';

class Foo extends StatefulWidget {
  @override
  FooState createState() => new FooState();
}

class FooState extends State<Foo> {
  final GlobalKey blockKey = new GlobalKey();
  GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();

  @override
  Widget build(BuildContext context) {
    return new LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return new ScrollConfiguration(
          delegate: new FooScrollConfiguration(),
          child: new Block(
            scrollableKey: scrollableKey,
            children: <Widget>[
              new GestureDetector(
                onTap: () {
                  setState(() {});
                  scrollableKey.currentState.scrollBy(200.0, duration: const Duration(milliseconds: 500));
                },
                child: new DecoratedBox(
                  decoration: new BoxDecoration(backgroundColor: const Color(0)),
                  child: new SizedBox(
                    height: 200.0,
                  ),
                )
              ),
              new DecoratedBox(
                decoration: new BoxDecoration(backgroundColor: const Color(0)),
                child: new SizedBox(
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
                decoration: new BoxDecoration(backgroundColor: const Color(0)),
                child: new SizedBox(
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
                decoration: new BoxDecoration(backgroundColor: const Color(0)),
                child: new SizedBox(
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
                decoration: new BoxDecoration(backgroundColor: const Color(0)),
                child: new SizedBox(
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
                decoration: new BoxDecoration(backgroundColor: const Color(0)),
                child: new SizedBox(
                  height: 200.0,
                ),
              ),
            ],
          )
        );
      }
    );
  }
}

class FooScrollConfiguration extends ScrollConfigurationDelegate {
  @override
  TargetPlatform get platform => defaultTargetPlatform;

  @override
  ExtentScrollBehavior createScrollBehavior() =>
      new OverscrollWhenScrollableBehavior(platform: platform);

  @override
  bool updateShouldNotify(FooScrollConfiguration old) => true;
}

void main() {
  testWidgets('https://github.com/flutter/flutter/issues/5630', (WidgetTester tester) async {
    await tester.pumpWidget(new Foo());
    expect(tester.state/*<ScrollableState>*/(find.byType(Scrollable)).scrollOffset, 0.0);
    await tester.tap(find.byType(GestureDetector).first);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    expect(tester.state/*<ScrollableState>*/(find.byType(Scrollable)).scrollOffset, 200.0);
  });
}