state_setting_in_scrollables_test.dart 3.02 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({ super.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
        return ScrollConfiguration(
22
          behavior: const FooScrollBehavior(),
23
          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 78
  const FooScrollBehavior();

79
  @override
80
  bool shouldNotify(FooScrollBehavior old) => true;
81 82 83
}

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