Toggle group
Use a toggle group for a small set of choices whose state stays visible. A layout switch is a good fit because both alternatives can stay in view.
Choose a layout
#Switch between List and Grid. The caption names the active layout.
components/toggle-group/appliance/choose_layout.dart
import 'package:flutter/widgets.dart';
import 'package:appliance_ui/appliance_ui.dart';
class ChooseLayout extends StatefulWidget {
const ChooseLayout({super.key});
@override
State<ChooseLayout> createState() => _ChooseLayoutState();
}
class _ChooseLayoutState extends State<ChooseLayout> {
Set<String> layout = {'List'};
@override
Widget build(BuildContext context) => Column(
mainAxisSize: MainAxisSize.min,
children: [
ApplianceToggleGroup<String>(
semanticLabel: 'Layout',
options: const [
ApplianceToggleOption(value: 'List', label: 'List'),
ApplianceToggleOption(value: 'Grid', label: 'Grid'),
],
selected: layout,
onSelectionChanged: (next) => setState(() => layout = next),
),
const SizedBox(height: 12),
Text('Layout: ${layout.single}'),
],
);
}
Parameters
ApplianceToggleGroup API ↗semanticLabel- Names the group.
options- List and Grid, in reading order.
selected- Current layout.
onSelectionChanged- Receives the new selection.
Shared behaviour
- Keyboard and focus. Tab into the group. Try the horizontal arrows to move focus and Space to choose the focused option.
- Screen readers. Read the choices in turn to hear their names and selection state. The caption also names the active layout.
- Direction and type. The options follow reading order in RTL. Try both labels at 200% text in the narrow frame.