state_setting_in_scrollables_test.dart 3 KB
Newer Older
1 2 3 4 5
// 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/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart' hide TypeMatcher;
7 8 9 10 11 12 13

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

class FooState extends State<Foo> {
14
  ScrollController scrollController = new ScrollController();
15 16 17 18 19

  @override
  Widget build(BuildContext context) {
    return new LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
Adam Barth's avatar
Adam Barth committed
20
        return new ScrollConfiguration(
21 22 23
          behavior: new FooScrollBehavior(),
          child: new ListView(
            controller: scrollController,
24 25 26
            children: <Widget>[
              new GestureDetector(
                onTap: () {
27
                  setState(() { /* this is needed to trigger the original bug this is regression-testing */ });
28
                  scrollController.animateTo(200.0, duration: const Duration(milliseconds: 500), curve: Curves.linear);
29 30
                },
                child: new DecoratedBox(
31 32
                  decoration: const BoxDecoration(backgroundColor: const Color(0)),
                  child: const SizedBox(
33 34 35 36 37
                    height: 200.0,
                  ),
                )
              ),
              new DecoratedBox(
38 39
                decoration: const BoxDecoration(backgroundColor: const Color(0)),
                child: const SizedBox(
40 41 42 43
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
44 45
                decoration: const BoxDecoration(backgroundColor: const Color(0)),
                child: const SizedBox(
46 47 48 49
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
50 51
                decoration: const BoxDecoration(backgroundColor: const Color(0)),
                child: const SizedBox(
52 53 54 55
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
56 57
                decoration: const BoxDecoration(backgroundColor: const Color(0)),
                child: const SizedBox(
58 59 60 61
                  height: 200.0,
                ),
              ),
              new DecoratedBox(
62 63
                decoration: const BoxDecoration(backgroundColor: const Color(0)),
                child: const SizedBox(
64 65 66 67 68 69 70 71 72 73 74
                  height: 200.0,
                ),
              ),
            ],
          )
        );
      }
    );
  }
}

Adam Barth's avatar
Adam Barth committed
75
class FooScrollBehavior extends ScrollBehavior {
76
  @override
77
  bool shouldNotify(FooScrollBehavior old) => true;
78 79 80
}

void main() {
81
  testWidgets('Can animate scroll after setState', (WidgetTester tester) async {
82
    await tester.pumpWidget(new Foo());
Adam Barth's avatar
Adam Barth committed
83
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 0.0);
84
    await tester.tap(find.byType(GestureDetector).first);
85
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
86
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 200.0);
87 88
  });
}