Flutter Material State Builder: Listen to material widget state changes.

Опубликовано: 24 Март 2026
на канале: Abel Does Flutter
175
3

There is an intrinsic complexity when we need to build a custom widget from a material widget such as ElevatedButton. For example, we need to specify a backgroundColor and an overlayColor for the ElevatedButton if we want to set a color when there is no action and another color when the button is pressed respectively. Even if this could be addressed by setting the color value using MaterialStateProperty there is no easy way to set the same colors for another widget that wants to react to the same states the button has.

In this video I show you how to use the MaterialStateBuilder widget from the package with same name I have created. The widget has a builder callback that offers the material state controller and the material states of a material widget, for example: ElevatedButton. The builder has a Widget return type. Therefore, we can return any Widget that has at least one material widget and then react to the material widget state changes for the whole widget not only the material one.

Link to plugin: https://pub.dev/packages/material_sta...