updateMaterialState method
- WidgetState key, {
- ValueChanged<
bool> ? onChanged,
Callback factory which accepts a WidgetState value and returns a closure to mutate materialStates and call setState.
Accepts an optional second named parameter, onChanged, which allows
arbitrary functionality to be wired through the MaterialStateMixin.
If supplied, the onChanged function is only called when child widgets
report events that make changes to the current set of WidgetStates.
This example shows how to use the updateMaterialState callback factory
in other widgets, including the optional
link
onChanged callback.
class MyWidget extends StatefulWidget {
const MyWidget({super.key, this.onPressed});
/// Something important this widget must do when pressed.
final VoidCallback? onPressed;
@override
State<MyWidget> createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> with MaterialStateMixin<MyWidget> {
@override
Widget build(BuildContext context) {
return ColoredBox(
color: isPressed ? Colors.black : Colors.white,
child: InkWell(
onHighlightChanged: updateMaterialState(
WidgetState.pressed,
onChanged: (bool val) {
if (val) {
widget.onPressed?.call();
}
},
),
),
);
}
}
Implementation
@protected
ValueChanged<bool> updateMaterialState(WidgetState key, {ValueChanged<bool>? onChanged}) {
return (bool value) {
if (materialStates.contains(key) == value) {
return;
}
setMaterialState(key, value);
onChanged?.call(value);
};
}