set_state_3_test.dart 1.47 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.

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

8 9
ChangerState changer;

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

  final Widget child;

15
  @override
16
  ChangerState createState() => ChangerState();
17 18 19
}

class ChangerState extends State<Changer> {
20
  bool _state = false;
21

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

28
  void test() { setState(() { _state = true; }); }
29

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

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

37
  final Widget child;
38

39
  @override
40
  Widget build(BuildContext context) => child;
41 42
}

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

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

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