state_setting_in_scrollables_test.dart 3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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

class Foo extends StatefulWidget {
9
  const Foo({ Key? key }) : super(key: key);
10
  @override
11
  FooState createState() => FooState();
12 13 14
}

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

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

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

void main() {
82
  testWidgets('Can animate scroll after setState', (WidgetTester tester) async {
83
    await tester.pumpWidget(
84
      const Directionality(
85
        textDirection: TextDirection.ltr,
86
        child: Foo(),
87 88
      ),
    );
Adam Barth's avatar
Adam Barth committed
89
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 0.0);
90
    await tester.tap(find.byType(GestureDetector).first);
91
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
92
    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 200.0);
93 94
  });
}