`ToggleButtons`: Add interactive example (#100124) · flutter/flutter@4ffc41a · GitHub
Skip to content

Commit 4ffc41a

Browse files
authored
ToggleButtons: Add interactive example (#100124)
1 parent 99769b0 commit 4ffc41a

3 files changed

Lines changed: 282 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/toggle_buttons/toggle_buttons.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
11+
testWidgets('Single-select ToggleButtons', (WidgetTester tester) async {
12+
TextButton findButton(String text) {
13+
return tester.widget<TextButton>(find.widgetWithText(TextButton, text));
14+
}
15+
await tester.pumpWidget(
16+
const MaterialApp(
17+
home: Scaffold(
18+
body: example.MyApp(),
19+
),
20+
),
21+
);
22+
23+
TextButton firstButton = findButton('Apple');
24+
TextButton secondButton = findButton('Banana');
25+
TextButton thirdButton = findButton('Orange');
26+
27+
/// First button is selected.
28+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xffef9a9a));
29+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
30+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
31+
32+
/// Tap on second button.
33+
await tester.tap(find.widgetWithText(TextButton, 'Banana'));
34+
await tester.pumpAndSettle();
35+
36+
firstButton = findButton('Apple');
37+
secondButton = findButton('Banana');
38+
thirdButton = findButton('Orange');
39+
40+
/// Only second button is selected.
41+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
42+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffef9a9a));
43+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
44+
});
45+
46+
testWidgets('Multi-select ToggleButtons', (WidgetTester tester) async {
47+
TextButton findButton(String text) {
48+
return tester.widget<TextButton>(find.widgetWithText(TextButton, text));
49+
}
50+
await tester.pumpWidget(
51+
const MaterialApp(
52+
home: Scaffold(
53+
body: example.MyApp(),
54+
),
55+
),
56+
);
57+
58+
TextButton firstButton = findButton('Tomatoes');
59+
TextButton secondButton = findButton('Potatoes');
60+
TextButton thirdButton = findButton('Carrots');
61+
62+
/// Second button is selected.
63+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
64+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7));
65+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
66+
67+
/// Tap on other two buttons.
68+
await tester.tap(find.widgetWithText(TextButton, 'Tomatoes'));
69+
await tester.tap(find.widgetWithText(TextButton, 'Carrots'));
70+
await tester.pumpAndSettle();
71+
72+
firstButton = findButton('Tomatoes');
73+
secondButton = findButton('Potatoes');
74+
thirdButton = findButton('Carrots');
75+
76+
/// All buttons are selected.
77+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7));
78+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7));
79+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7));
80+
});
81+
82+
testWidgets('Icon-only ToggleButtons', (WidgetTester tester) async {
83+
TextButton findButton(IconData iconData) {
84+
return tester.widget<TextButton>(find.widgetWithIcon(TextButton, iconData));
85+
}
86+
await tester.pumpWidget(
87+
const MaterialApp(
88+
home: Scaffold(
89+
body: example.MyApp(),
90+
),
91+
),
92+
);
93+
94+
TextButton firstButton = findButton(Icons.sunny);
95+
TextButton secondButton = findButton(Icons.cloud);
96+
TextButton thirdButton = findButton(Icons.ac_unit);
97+
98+
/// Third button is selected.
99+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
100+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
101+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0xff90caf9));
102+
103+
/// Tap on the first button.
104+
await tester.tap(find.widgetWithIcon(TextButton, Icons.sunny));
105+
await tester.pumpAndSettle();
106+
107+
firstButton = findButton(Icons.sunny);
108+
secondButton = findButton(Icons.cloud);
109+
thirdButton = findButton(Icons.ac_unit);
110+
111+
/// First button os selected.
112+
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xff90caf9));
113+
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
114+
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff));
115+
});
116+
}
117+
118+
Set<MaterialState> enabled = <MaterialState>{ };

‎packages/flutter/lib/src/material/toggle_buttons.dart‎

Lines changed: 6 additions & 0 deletions

0 commit comments

Comments
 (0)