Home > Software design >  Flutter how to avoid MaterialStateProperty
Flutter how to avoid MaterialStateProperty

Time:09-14

Recently I wanted to use some Widgets like TextButton and I figured out I have to use MaterialStateProperty wrapper for a lot of properties of the TextButton widget.

      child: TextButton(
          style: ButtonStyle(
            padding: MaterialStateProperty.all(EdgeInsets.only(
              left: 10,
              right: 10,
              top: 5,
              bottom: 5,
            )),
            elevation: MaterialStateProperty.all(2),
            shadowColor: MaterialStateProperty.all<Color>(Colors.black),
            shape: MaterialStateProperty.all(RoundedRectangleBorder(
                side: BorderSide(color: Color.fromRGBO(85, 63, 48, 1.0), width: 1, strokeAlign: StrokeAlign.outside), borderRadius: BorderRadius.circular(2))),
            backgroundColor: MaterialStateProperty.all(Color.fromRGBO(241, 206, 181, 1.0)),
          ),

I wonder why that is? Was that introduced with a new update? I find it very annoying and noisy but can't figure out a way to use the normal Widgets anymore.

CodePudding user response:

Instead of using ButtonStyle you could use TextButton.styleFrom like this:

TextButton(
  style: TextButton.styleFrom(
    padding: EdgeInsets.only(
      left: 10,
      right: 10,
      top: 5,
      bottom: 5,
    ),
    elevation: 2,
    shadowColor: Colors.black,
    shape: RoundedRectangleBorder(
      side: BorderSide(
        color: Color.fromRGBO(85, 63, 48, 1.0), 
        width: 1, 
        strokeAlign: StrokeAlign.outside
      ), 
      borderRadius: BorderRadius.circular(2)
    ),
    backgroundColor: Color.fromRGBO(241, 206, 181, 1.0)
  ),
),
  • Related