Skip to content

Commit b7e553a

Browse files
authored
[material_ui] Add M3E IconButton example (#13085)
Adds an API example for the Material 3 Expressive `IconButton` support added in #12832, and links it from the `IconButton` docs. The example opts in with `IconButtonThemeData(variant: StyleVariant.material3Expressive)` and shows: * Sizes (`ButtonStyle.sizeVariant`): extra small through extra large * Widths (`ButtonStyle.iconButtonWidth`): narrow, standard, and wide * Shapes (`ButtonStyle.shapeVariant`): round and square, including the shape change when a toggle button is selected * Disabled buttons, unselected and selected It also makes this example the "Icon buttons" entry in the example gallery app (`example/lib/main.dart`), replacing the old `icon_button.3.dart`. | Example | Toggling buttons | | --- | --- | | <img src="https://gist.githubusercontent.com/QuncCccccc/aba2243afce554b515eec724e62e5650/raw/6ea704deaa054fdbeafb58b1836f97ac9751a401/icon_button_m3e_example.png" width="400" alt="Material 3 Expressive IconButton example"> | <img src="https://gist.githubusercontent.com/QuncCccccc/aba2243afce554b515eec724e62e5650/raw/6ea704deaa054fdbeafb58b1836f97ac9751a401/icon_button_m3e_toggle.gif" width="400" alt="Toggling Material 3 Expressive icon buttons"> | Part of flutter/flutter#184940 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 916d4c9 commit b7e553a

6 files changed

Lines changed: 546 additions & 6 deletions

File tree

Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// #region body
6+
import 'package:material_ui/material_ui.dart';
7+
8+
/// Flutter code sample for Material 3 Expressive [IconButton].
9+
10+
void main() {
11+
runApp(const IconButtonM3EExampleApp());
12+
}
13+
14+
class IconButtonM3EExampleApp extends StatelessWidget {
15+
const IconButtonM3EExampleApp({super.key});
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return MaterialApp(
20+
theme: ThemeData(
21+
iconButtonTheme: const IconButtonThemeData(
22+
variant: StyleVariant.material3Expressive,
23+
),
24+
),
25+
title: 'Material 3 Expressive IconButton',
26+
home: const Scaffold(body: SafeArea(child: IconButtonM3EExample())),
27+
);
28+
}
29+
}
30+
31+
class IconButtonM3EExample extends StatefulWidget {
32+
const IconButtonM3EExample({super.key});
33+
34+
@override
35+
State<IconButtonM3EExample> createState() => _IconButtonM3EExampleState();
36+
}
37+
38+
class _IconButtonM3EExampleState extends State<IconButtonM3EExample> {
39+
final Set<String> _selectedButtonIds = <String>{};
40+
41+
bool _isSelected(String id) {
42+
return _selectedButtonIds.contains(id);
43+
}
44+
45+
void _toggle(String id) {
46+
setState(() {
47+
if (_selectedButtonIds.contains(id)) {
48+
_selectedButtonIds.remove(id);
49+
} else {
50+
_selectedButtonIds.add(id);
51+
}
52+
});
53+
}
54+
55+
@override
56+
Widget build(BuildContext context) {
57+
return ListView(
58+
padding: const EdgeInsets.all(24.0),
59+
children: <Widget>[
60+
_Section(
61+
title: 'Sizes',
62+
child: Wrap(
63+
spacing: 16.0,
64+
runSpacing: 20.0,
65+
children: _sizeOptions.map((_SizeOption option) {
66+
final String id = 'size-${option.key}';
67+
return _IconButtonDemoTile(
68+
label: option.label,
69+
child: IconButton.filledTonal(
70+
key: ValueKey<String>('m3e-icon-button-${option.key}'),
71+
icon: Icon(option.icon),
72+
selectedIcon: Icon(option.selectedIcon),
73+
isSelected: _isSelected(id),
74+
onPressed: () {
75+
_toggle(id);
76+
},
77+
style: ButtonStyle(sizeVariant: option.sizeVariant),
78+
),
79+
);
80+
}).toList(),
81+
),
82+
),
83+
_Section(
84+
title: 'Widths',
85+
child: Wrap(
86+
spacing: 16.0,
87+
runSpacing: 20.0,
88+
children: _widthOptions.map((_WidthOption option) {
89+
final String id = 'width-${option.key}';
90+
return _IconButtonDemoTile(
91+
label: option.label,
92+
child: IconButton.filled(
93+
key: ValueKey<String>('m3e-icon-button-width-${option.key}'),
94+
icon: Icon(option.icon),
95+
selectedIcon: Icon(option.selectedIcon),
96+
isSelected: _isSelected(id),
97+
onPressed: () {
98+
_toggle(id);
99+
},
100+
style: ButtonStyle(
101+
iconButtonWidth: option.iconButtonWidth,
102+
sizeVariant: ButtonSizeVariant.medium,
103+
),
104+
),
105+
);
106+
}).toList(),
107+
),
108+
),
109+
_Section(
110+
title: 'Shapes',
111+
child: Wrap(
112+
spacing: 16.0,
113+
runSpacing: 20.0,
114+
children: _shapeOptions.map((_ShapeOption option) {
115+
final String id = 'shape-${option.key}';
116+
return _IconButtonDemoTile(
117+
label: option.label,
118+
child: IconButton.filledTonal(
119+
key: ValueKey<String>('m3e-icon-button-shape-${option.key}'),
120+
icon: Icon(option.icon),
121+
selectedIcon: Icon(option.selectedIcon),
122+
isSelected: _isSelected(id),
123+
onPressed: () {
124+
_toggle(id);
125+
},
126+
style: ButtonStyle(shapeVariant: option.shapeVariant),
127+
),
128+
);
129+
}).toList(),
130+
),
131+
),
132+
const _Section(
133+
title: 'Disabled',
134+
child: Wrap(
135+
spacing: 16.0,
136+
runSpacing: 20.0,
137+
children: <Widget>[
138+
// A null onPressed disables the button.
139+
_IconButtonDemoTile(
140+
label: 'Unselected',
141+
child: IconButton.filledTonal(
142+
key: ValueKey<String>('m3e-icon-button-disabled-unselected'),
143+
icon: Icon(Icons.favorite_border),
144+
selectedIcon: Icon(Icons.favorite),
145+
isSelected: false,
146+
onPressed: null,
147+
),
148+
),
149+
_IconButtonDemoTile(
150+
label: 'Selected',
151+
child: IconButton.filledTonal(
152+
key: ValueKey<String>('m3e-icon-button-disabled-selected'),
153+
icon: Icon(Icons.favorite_border),
154+
selectedIcon: Icon(Icons.favorite),
155+
isSelected: true,
156+
onPressed: null,
157+
),
158+
),
159+
],
160+
),
161+
),
162+
],
163+
);
164+
}
165+
}
166+
167+
class _Section extends StatelessWidget {
168+
const _Section({required this.title, required this.child});
169+
170+
final Widget child;
171+
final String title;
172+
173+
@override
174+
Widget build(BuildContext context) {
175+
return Padding(
176+
padding: const EdgeInsets.only(bottom: 28.0),
177+
child: Column(
178+
crossAxisAlignment: CrossAxisAlignment.start,
179+
children: <Widget>[
180+
Text(title, style: Theme.of(context).textTheme.titleMedium),
181+
const SizedBox(height: 12.0),
182+
child,
183+
],
184+
),
185+
);
186+
}
187+
}
188+
189+
class _IconButtonDemoTile extends StatelessWidget {
190+
const _IconButtonDemoTile({required this.label, required this.child});
191+
192+
final Widget child;
193+
final String label;
194+
195+
@override
196+
Widget build(BuildContext context) {
197+
return SizedBox(
198+
width: 152.0,
199+
child: Column(
200+
mainAxisSize: MainAxisSize.min,
201+
children: <Widget>[
202+
SizedBox(height: 148.0, child: Center(child: child)),
203+
const SizedBox(height: 8.0),
204+
Text(
205+
label,
206+
style: Theme.of(context).textTheme.labelLarge,
207+
textAlign: TextAlign.center,
208+
),
209+
],
210+
),
211+
);
212+
}
213+
}
214+
215+
class _SizeOption {
216+
const _SizeOption(
217+
this.label,
218+
this.key,
219+
this.sizeVariant,
220+
this.icon,
221+
this.selectedIcon,
222+
);
223+
224+
final IconData icon;
225+
final String key;
226+
final String label;
227+
final IconData selectedIcon;
228+
final ButtonSizeVariant sizeVariant;
229+
}
230+
231+
const List<_SizeOption> _sizeOptions = <_SizeOption>[
232+
_SizeOption(
233+
'Extra small',
234+
'x-small',
235+
ButtonSizeVariant.xSmall,
236+
Icons.add,
237+
Icons.add_circle,
238+
),
239+
_SizeOption(
240+
'Small',
241+
'small',
242+
ButtonSizeVariant.small,
243+
Icons.search,
244+
Icons.manage_search,
245+
),
246+
_SizeOption(
247+
'Medium',
248+
'medium',
249+
ButtonSizeVariant.medium,
250+
Icons.bookmark_border,
251+
Icons.bookmark,
252+
),
253+
_SizeOption(
254+
'Large',
255+
'large',
256+
ButtonSizeVariant.large,
257+
Icons.edit_outlined,
258+
Icons.edit,
259+
),
260+
_SizeOption(
261+
'Extra large',
262+
'x-large',
263+
ButtonSizeVariant.xLarge,
264+
Icons.star_border,
265+
Icons.star,
266+
),
267+
];
268+
269+
class _WidthOption {
270+
const _WidthOption(
271+
this.label,
272+
this.key,
273+
this.iconButtonWidth,
274+
this.icon,
275+
this.selectedIcon,
276+
);
277+
278+
final IconData icon;
279+
final IconButtonWidthVariant iconButtonWidth;
280+
final String key;
281+
final String label;
282+
final IconData selectedIcon;
283+
}
284+
285+
const List<_WidthOption> _widthOptions = <_WidthOption>[
286+
_WidthOption(
287+
'Narrow',
288+
'narrow',
289+
IconButtonWidthVariant.narrow,
290+
Icons.arrow_back,
291+
Icons.keyboard_double_arrow_left,
292+
),
293+
_WidthOption(
294+
'Standard',
295+
'standard',
296+
IconButtonWidthVariant.standard,
297+
Icons.horizontal_rule,
298+
Icons.remove_circle,
299+
),
300+
_WidthOption(
301+
'Wide',
302+
'wide',
303+
IconButtonWidthVariant.wide,
304+
Icons.arrow_forward,
305+
Icons.keyboard_double_arrow_right,
306+
),
307+
];
308+
309+
class _ShapeOption {
310+
const _ShapeOption(
311+
this.label,
312+
this.key,
313+
this.shapeVariant,
314+
this.icon,
315+
this.selectedIcon,
316+
);
317+
318+
final IconData icon;
319+
final String key;
320+
final String label;
321+
final IconData selectedIcon;
322+
final ButtonShapeVariant shapeVariant;
323+
}
324+
325+
const List<_ShapeOption> _shapeOptions = <_ShapeOption>[
326+
_ShapeOption(
327+
'Round',
328+
'round',
329+
ButtonShapeVariant.round,
330+
Icons.check_circle_outline,
331+
Icons.check_circle,
332+
),
333+
_ShapeOption(
334+
'Square',
335+
'square',
336+
ButtonShapeVariant.square,
337+
Icons.check_box_outline_blank,
338+
Icons.check_box,
339+
),
340+
];
341+
// #endregion body

‎packages/material_ui/example/lib/main.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'badge/badge.0.dart' as badge_0;
99
import 'button_style/button_style.0.dart' as button_style_0;
1010
import 'floating_action_button/floating_action_button.1.dart'
1111
as floating_action_button_1;
12-
import 'icon_button/icon_button.3.dart' as icon_button_3;
12+
import 'icon_button/icon_button.4.dart' as icon_button_4;
1313
import 'segmented_button/segmented_button.0.dart' as segmented_button_0;
1414
import 'card/card.2.dart' as card_2;
1515
import 'carousel/carousel.0.dart' as carousel_0;
@@ -76,10 +76,10 @@ class ExampleApp extends StatelessWidget {
7676
const floating_action_button_1.FloatingActionButtonExampleApp(),
7777
),
7878
_Example(
79-
filepath: 'icon_button/icon_button.3.dart',
79+
filepath: 'icon_button/icon_button.4.dart',
8080
title: 'Icon buttons',
8181
builder: (BuildContext context) =>
82-
const icon_button_3.IconButtonToggleApp(),
82+
const icon_button_4.IconButtonM3EExampleApp(),
8383
),
8484
_Example(
8585
filepath: 'segmented_button/segmented_button.0.dart',

0 commit comments

Comments
 (0)