Skip to content

Segmented Button

The SegmentedButton widget allows users to select one or multiple options from a set of options.

By default, the SegmentedButton allows for a single selection.

Set<String> _selected = {'Daily'};
SegmentedButton<String>(
segments: const {
'Daily': Text('Daily'),
'Weekly': Text('Weekly'),
'Monthly': Text('Monthly'),
},
selected: _selected,
onSelectionChanged: (newSelection) {
setState(() {
_selected = newSelection;
});
},
)

You can enable multi-select mode by setting multiSelect to true.

Set<String> _selected = {'Mon', 'Wed'};
SegmentedButton<String>(
segments: const {
'Mon': Text('M'),
'Tue': Text('T'),
'Wed': Text('W'),
'Thu': Text('T'),
'Fri': Text('F'),
},
selected: _selected,
multiSelect: true,
allowEmpty: true,
onSelectionChanged: (newSelection) {
setState(() {
_selected = newSelection;
});
},
)

Segments can contain any widget, including icons.

Widget _buildIconLabel(IconData icon, String label) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 16),
const SizedBox(width: 8),
Text(label),
],
);
}
Set<String> _selected = {'M'};
SegmentedButton<String>(
segments: {
'First': _buildIconLabel(PrimeIcons.chevronLeft, 'First'),
'Center': _buildIconLabel(PrimeIcons.minus, 'Center'),
'Last': _buildIconLabel(PrimeIcons.chevronRight, 'Last'),
},
selected: _selected,
onSelectionChanged: (newSelection) {
setState(() {
_selected = newSelection;
});
},
)

You can customize the font style by wrapping the text in a Builder and applying a specific style, such as PrimeTheme.of(context).textTheme.label for a monospace look.

Widget _buildMonoLabel(String label) {
return Builder(
builder: (context) {
final style = PrimeTheme.of(context).textTheme.label;
return Text(
label,
// Use textTheme.label properties but allow inheriting weight/color from parent
style: TextStyle(
fontFamily: style.fontFamily,
package: 'prime_flutter',
fontSize: style.fontSize,
height: style.height,
letterSpacing: style.letterSpacing,
),
);
},
);
}
Set<String> _selected = {'Code'};
SegmentedButton<String>(
segments: {
'Code': _buildMonoLabel('CODE'),
'Build': _buildMonoLabel('BUILD'),
'Test': _buildMonoLabel('TEST'),
},
selected: _selected,
onSelectionChanged: (newSelection) {
setState(() {
_selected = newSelection;
});
},
)
PropertyTypeDefaultDescription
segmentsMap<T, Widget>RequiredA map of widgets to display as segments.
selectedSet<T>RequiredThe set of currently selected values.
onSelectionChangedValueChanged<Set<T>>?nullCalled when the selection changes.
multiSelectboolfalseWhether multiple segments can be selected.
allowEmptyboolfalseWhether the selection can be empty.