Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Elevated Button - How do I remove elevation with MaterialStateProperty.all?

Ok, with the new flutter updates I have figured out most things, but I can't seem to figure out what the elevated property wants. I have applied MaterialStateProperty.all and the property is asking for a positional argument but I don't know what it wants. I have tried double, elevatedValue and everything else predictable but I can not discern how to set this to zero. Can someone please provide a code example of the new Elevated Button with the elevation set to zero using MaterialStateProperty please?

like image 594
BigPictureDeepDive Avatar asked Jan 25 '26 22:01

BigPictureDeepDive


2 Answers

This is how you use new Elevated button

ElevatedButtonThemeData(
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled)) {
              return greyColor;
            }
            return selectedPrimaryColor; // Defer to the widget's default.
          },
        ),
        elevation: MaterialStateProperty.resolveWith<double>(  // As you said you dont need elevation. I'm returning 0 in both case
              (Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled)) {
              return 0;
            }
            return 0; // Defer to the widget's default.
          },
        ),
        foregroundColor: MaterialStateProperty.resolveWith<Color>(
          (Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled)) {
              return greyColor;
            }
            return selectedPrimaryColor; // Defer to the widget's default.
          },
        ),
      ),
    ),

Btw I extract this from themeData. So that I don't need to repeat the code again and again. You should also learn ThemeData class. Here is the docs.

like image 100
Nikhil Badyal Avatar answered Jan 28 '26 19:01

Nikhil Badyal


Another cleaner approach from the accepted answer is to use styleFrom method:

ElevatedButton(
  style: ElevatedButton.styleFrom(elevation: 0),
);
like image 27
Alexander Dischberg Avatar answered Jan 28 '26 18:01

Alexander Dischberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!