set_state_1_test.dart 1.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

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

14
class InsideState extends State<Inside> {
15
  @override
16
  Widget build(BuildContext context) {
17
    return Listener(
18
      onPointerDown: _handlePointerDown,
Ian Hickson's avatar
Ian Hickson committed
19
      child: const Text('INSIDE', textDirection: TextDirection.ltr),
20 21 22
    );
  }

23
  void _handlePointerDown(PointerDownEvent event) {
24 25 26 27
    setState(() { });
  }
}

28
class Middle extends StatefulWidget {
29
  const Middle({
30
    Key? key,
31 32
    this.child,
  }) : super(key: key);
33

34
  final Inside? child;
35

36
  @override
37
  MiddleState createState() => MiddleState();
38 39 40
}

class MiddleState extends State<Middle> {
41
  @override
42
  Widget build(BuildContext context) {
43
    return Listener(
44
      onPointerDown: _handlePointerDown,
Ian Hickson's avatar
Ian Hickson committed
45
      child: widget.child,
46 47 48
    );
  }

49
  void _handlePointerDown(PointerDownEvent event) {
50 51
    setState(() { });
  }
52
}
53

54
class Outside extends StatefulWidget {
55
  const Outside({ Key? key }) : super(key: key);
56
  @override
57
  OutsideState createState() => OutsideState();
58 59
}

60
class OutsideState extends State<Outside> {
61
  @override
62
  Widget build(BuildContext context) {
63
    return const Middle(child: Inside());
64 65 66 67
  }
}

void main() {
68
  testWidgets('setState() smoke test', (WidgetTester tester) async {
69
    await tester.pumpWidget(const Outside());
70
    final Offset location = tester.getCenter(find.text('INSIDE'));
71
    final TestGesture gesture = await tester.startGesture(location);
72 73 74
    await tester.pump();
    await gesture.up();
    await tester.pump();
75
  });
Adam Barth's avatar
Adam Barth committed
76
}