1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6import 'package:flutter_api_samples/material/toggle_buttons/toggle_buttons.0.dart' as example;
7import 'package:flutter_test/flutter_test.dart';
8
9void main() {
10 testWidgets('Single-select ToggleButtons', (WidgetTester tester) async {
11 TextButton findButton(String text) {
12 return tester.widget<TextButton>(find.widgetWithText(TextButton, text));
13 }
14
15 await tester.pumpWidget(const example.ToggleButtonsExampleApp());
16
17 TextButton firstButton = findButton('Apple');
18 TextButton secondButton = findButton('Banana');
19 TextButton thirdButton = findButton('Orange');
20
21 const Color selectedColor = Color(0xffef9a9a);
22 const Color unselectedColor = Color(0x00fef7ff);
23
24 /// First button is selected.
25 expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
26 expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
27 expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
28
29 /// Tap on second button.
30 await tester.tap(find.widgetWithText(TextButton, 'Banana'));
31 await tester.pumpAndSettle();
32
33 firstButton = findButton('Apple');
34 secondButton = findButton('Banana');
35 thirdButton = findButton('Orange');
36
37 /// Only second button is selected.
38 expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
39 expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
40 expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
41 });
42
43 testWidgets('Multi-select ToggleButtons', (WidgetTester tester) async {
44 TextButton findButton(String text) {
45 return tester.widget<TextButton>(find.widgetWithText(TextButton, text));
46 }
47
48 await tester.pumpWidget(const example.ToggleButtonsExampleApp());
49
50 TextButton firstButton = findButton('Tomatoes');
51 TextButton secondButton = findButton('Potatoes');
52 TextButton thirdButton = findButton('Carrots');
53
54 const Color selectedColor = Color(0xffa5d6a7);
55 const Color unselectedColor = Color(0x00fef7ff);
56
57 /// Second button is selected.
58 expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
59 expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
60 expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
61
62 /// Tap on other two buttons.
63 await tester.tap(find.widgetWithText(TextButton, 'Tomatoes'));
64 await tester.tap(find.widgetWithText(TextButton, 'Carrots'));
65 await tester.pumpAndSettle();
66
67 firstButton = findButton('Tomatoes');
68 secondButton = findButton('Potatoes');
69 thirdButton = findButton('Carrots');
70
71 /// All buttons are selected.
72 expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
73 expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
74 expect(thirdButton.style!.backgroundColor!.resolve(enabled), selectedColor);
75 });
76
77 testWidgets('Icon-only ToggleButtons', (WidgetTester tester) async {
78 TextButton findButton(IconData iconData) {
79 return tester.widget<TextButton>(find.widgetWithIcon(TextButton, iconData));
80 }
81
82 await tester.pumpWidget(const example.ToggleButtonsExampleApp());
83
84 TextButton firstButton = findButton(Icons.sunny);
85 TextButton secondButton = findButton(Icons.cloud);
86 TextButton thirdButton = findButton(Icons.ac_unit);
87
88 const Color selectedColor = Color(0xff90caf9);
89 const Color unselectedColor = Color(0x00fef7ff);
90
91 /// Third button is selected.
92 expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
93 expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
94 expect(thirdButton.style!.backgroundColor!.resolve(enabled), selectedColor);
95
96 /// Tap on the first button.
97 await tester.tap(find.widgetWithIcon(TextButton, Icons.sunny));
98 await tester.pumpAndSettle();
99
100 firstButton = findButton(Icons.sunny);
101 secondButton = findButton(Icons.cloud);
102 thirdButton = findButton(Icons.ac_unit);
103
104 /// First button os selected.
105 expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
106 expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
107 expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
108 });
109}
110
111Set<MaterialState> enabled = <MaterialState>{};
112

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com