updateMaterialState method
- MaterialState key, {
- ValueChanged<
bool> ? onChanged,
Callback factory which accepts a MaterialState 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 MaterialStates.
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(
MaterialState.pressed,
onChanged: (bool val) {
if (val) {
widget.onPressed?.call();
}
},
),
),
);
}
}
Implementation
@protected
ValueChanged<bool> updateMaterialState(MaterialState key, {ValueChanged<bool>? onChanged}) {
return (bool value) {
if (materialStates.contains(key) == value) {
return;
}
setMaterialState(key, value);
onChanged?.call(value);
};
}