state_setting_in_scrollables_test.dart 3.11 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 16 17 18 19 20 21
  final ScrollController scrollController = ScrollController();

  @override
  void dispose() {
    scrollController.dispose();
    super.dispose();
  }
22 23 24

  @override
  Widget build(BuildContext context) {
25
    return LayoutBuilder(
26
      builder: (BuildContext context, BoxConstraints constraints) {
27
        return ScrollConfiguration(
28
          behavior: const FooScrollBehavior(),
29
          child: ListView(
30
            controller: scrollController,
31
            children: <Widget>[
32
              GestureDetector(
33
                onTap: () {
34
                  setState(() { /* this is needed to trigger the original bug this is regression-testing */ });
35
                  scrollController.animateTo(200.0, duration: const Duration(milliseconds: 500), curve: Curves.linear);
36
                },
37
                child: const DecoratedBox(
38
                  decoration: BoxDecoration(color: Color(0x00000000)),
39
                  child: SizedBox(
40 41
                    height: 200.0,
                  ),
42
                ),
43
              ),
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
                  height: 200.0,
                ),
              ),
68
              const DecoratedBox(
69
                decoration: BoxDecoration(color: Color(0x00000000)),
70
                child: SizedBox(
71 72 73 74
                  height: 200.0,
                ),
              ),
            ],
75
          ),
76
        );
77
      },
78 79 80 81
    );
  }
}

Adam Barth's avatar
Adam Barth committed
82
class FooScrollBehavior extends ScrollBehavior {
83 84
  const FooScrollBehavior();

85
  @override
86
  bool shouldNotify(FooScrollBehavior old) => true;
87 88 89
}

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