state_setting_in_scrollables_test.dart 2.93 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';
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: const DecoratedBox(
31 32
                  decoration: BoxDecoration(color: Color(0)),
                  child: SizedBox(
33 34 35 36
                    height: 200.0,
                  ),
                )
              ),
37
              const DecoratedBox(
38 39
                decoration: BoxDecoration(color: Color(0)),
                child: SizedBox(
40 41 42
                  height: 200.0,
                ),
              ),
43
              const DecoratedBox(
44 45
                decoration: BoxDecoration(color: Color(0)),
                child: SizedBox(
46 47 48
                  height: 200.0,
                ),
              ),
49
              const DecoratedBox(
50 51
                decoration: BoxDecoration(color: Color(0)),
                child: SizedBox(
52 53 54
                  height: 200.0,
                ),
              ),
55
              const DecoratedBox(
56 57
                decoration: BoxDecoration(color: Color(0)),
                child: SizedBox(
58 59 60
                  height: 200.0,
                ),
              ),
61
              const DecoratedBox(
62 63
                decoration: BoxDecoration(color: Color(0)),
                child: 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 83 84 85 86 87
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Foo(),
      ),
    );
Adam Barth's avatar
Adam Barth committed
88
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 0.0);
89
    await tester.tap(find.byType(GestureDetector).first);
90
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
91
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 200.0);
92 93
  });
}