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

5 6
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/widgets.dart';
9

10 11
ChangerState changer;

12
class Changer extends StatefulWidget {
13
  const Changer(this.child, { Key key }) : super(key: key);
14 15 16

  final Widget child;

17
  @override
18
  ChangerState createState() => ChangerState();
19 20 21
}

class ChangerState extends State<Changer> {
22
  bool _state = false;
23

24
  @override
25 26
  void initState() {
    super.initState();
27 28 29
    changer = this;
  }

30
  void test() { setState(() { _state = true; }); }
31

32
  @override
33
  Widget build(BuildContext context) => _state ? Wrapper(widget.child) : widget.child;
34 35
}

36
class Wrapper extends StatelessWidget {
37
  const Wrapper(this.child, { Key key }) : super(key: key);
38

39
  final Widget child;
40

41
  @override
42
  Widget build(BuildContext context) => child;
43 44
}

45
class Leaf extends StatefulWidget {
46
  const Leaf({ Key key }) : super(key: key);
47
  @override
48
  LeafState createState() => LeafState();
49 50 51
}

class LeafState extends State<Leaf> {
52
  @override
Ian Hickson's avatar
Ian Hickson committed
53
  Widget build(BuildContext context) => const Text('leaf', textDirection: TextDirection.ltr);
54 55 56
}

void main() {
57
  testWidgets('three-way setState() smoke test', (WidgetTester tester) async {
58 59
    await tester.pumpWidget(const Changer(Wrapper(Leaf())));
    await tester.pumpWidget(const Changer(Wrapper(Leaf())));
60
    changer.test();
61
    await tester.pump();
62 63
  });
}