state_setting_in_scrollables_test.dart 2.9 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

class Foo extends StatefulWidget {
  @override
10
  FooState createState() => FooState();
11 12 13
}

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

  @override
  Widget build(BuildContext context) {
18
    return LayoutBuilder(
19
      builder: (BuildContext context, BoxConstraints constraints) {
20 21 22
        return ScrollConfiguration(
          behavior: FooScrollBehavior(),
          child: ListView(
23
            controller: scrollController,
24
            children: <Widget>[
25
              GestureDetector(
26
                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
                    height: 200.0,
                  ),
35
                ),
36
              ),
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
                  height: 200.0,
                ),
              ),
            ],
68
          ),
69 70 71 72 73 74
        );
      }
    );
  }
}

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(
83
      Directionality(
84
        textDirection: TextDirection.ltr,
85
        child: Foo(),
86 87
      ),
    );
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
  });
}