Alex.F
Alex.F

Reputation: 6181

How to set minimum width on a TextButton (without changing minimum height)

Lets say I have this TextButton:

TextButton(
   style: TextButton.styleFrom(
     minimumSize: const Size(newWidth, newHeight),
   ),
  onPressed: _onTapped,
  child: const Text('Button'),
),

Is there a way to set the minimum width while retaining whatever was the minimum height?

The motivation here is to keep the theme's values and overriding only what I need.
I was hoping that TextButton.defaultStyleOf(context) would be helpful alas it is not a static method.

Some notes:
I am not looking to use the old material buttons like FlatButton as they are being replaced with the new buttons (TextButton in this case), which is why I have this question in the first place.
But if there is a way to achieve that with MaterialButton, that would be good enough.

Upvotes: 7

Views: 5925

Answers (1)

RahulZx
RahulZx

Reputation: 127

Use MediaQuery for size in ratio.

TextButton(onPressed:() {

            },
            style: TextButton.styleFrom(
              primary: Colors.white,
              backgroundColor: Colors.teal,
              onSurface: Colors.grey,
              minimumSize: Size(MediaQuery.of(context).size.width-20,40)
            ),
            child: Text("Button")),

by default it take width according to its text width Or you can try with MediaQuery

Upvotes: 6

Related Questions