// The Filter component is controlled; its state must be managed by a parent.
class FormFilterExample extends StatefulComponent {
@override
State<FormFilterExample> createState() => _FormFilterExampleState();
}
class _FormFilterExampleState extends State<FormFilterExample> {
// Use a nullable type to represent the cleared (reset) state.
String? _selectedFramework;
@override
Component build(BuildContext context) {
return Filter<String>(
// The `name` is required for the underlying radio group.
name: 'state-management-example',
// The currently selected value, provided from the parent's state.
groupValue: _selectedFramework,
// This callback updates the parent's state when a new item is selected.
onValueChanged: (newValue) {
setState(() => _selectedFramework = newValue);
},
// This callback handles the native form reset event.
onReset: () {
setState(() => _selectedFramework = null);
},
// The `method` defaults to `form`, but is explicit here for clarity.
method: FilterMethod.form,
[
FilterItem(value: 'Riverpod', label: 'Riverpod'),
FilterItem(value: 'Bloc', label: 'Bloc'),
FilterItem(value: 'Signals', label: 'Signals'),
],
);
}
}