test_widgets.dart 1.4 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 9
const BoxDecoration kBoxDecorationA = BoxDecoration(
  color: Color(0xFFFF0000),
10 11
);

12 13
const BoxDecoration kBoxDecorationB = BoxDecoration(
  color: Color(0xFF00FF00),
14 15
);

16 17
const BoxDecoration kBoxDecorationC = BoxDecoration(
  color: Color(0xFF0000FF),
18 19
);

20
class TestBuildCounter extends StatelessWidget {
21
  const TestBuildCounter({ super.key });
22

23 24
  static int buildCount = 0;

25
  @override
26
  Widget build(BuildContext context) {
27
    buildCount += 1;
28
    return const DecoratedBox(decoration: kBoxDecorationA);
29 30 31 32
  }
}


33
class FlipWidget extends StatefulWidget {
34
  const FlipWidget({ super.key, required this.left, required this.right });
35 36 37 38

  final Widget left;
  final Widget right;

39
  @override
40
  FlipWidgetState createState() => FlipWidgetState();
41 42
}

43
class FlipWidgetState extends State<FlipWidget> {
44 45 46 47 48 49 50 51
  bool _showLeft = true;

  void flip() {
    setState(() {
      _showLeft = !_showLeft;
    });
  }

52
  @override
53
  Widget build(BuildContext context) {
54
    return _showLeft ? widget.left : widget.right;
55 56 57
  }
}

58
void flipStatefulWidget(WidgetTester tester, { bool skipOffstage = true }) {
59
  tester.state<FlipWidgetState>(find.byType(FlipWidget, skipOffstage: skipOffstage)).flip();
60
}