Sen Alexandru
Sen Alexandru

Reputation: 2263

Flutter TextButton padding

With the Flutter 2.0 release, the FlatButton has been replaced with TextButton.

Hence, the padding property is not longer available directly, but as a ButtonStyle property.

My problem is, how can I set it since it's no longer available as EdgeInsets?

TextButton(
   style: new ButtonStyle(
     padding: ???,
   ),
   //padding: const EdgeInsets.all(0),   //NOT AVAILABLE
   child: Text("Support", style: Theme.of(context).textTheme.headline2),
      onPressed: () => {Navigator.pushNamed(context, SupportScreen().routeName)},
   ),

Upvotes: 27

Views: 31466

Answers (4)

Mariam Ali
Mariam Ali

Reputation: 1

If you are facing this error or warning

MaterialStatePropertyAll' is deprecated and shouldn't be used.

Try this

padding: WidgetStatePropertyAll(EdgeInsets.zero)

Upvotes: 0

bishoyN
bishoyN

Reputation: 599

Here is my code for TextButton

Container(
    child: TextButton(
    style: ButtonStyle(
        backgroundColor:
            MaterialStateProperty.all<Color>(Colors.green),
        padding: MaterialStateProperty.all<EdgeInsets>(
            EdgeInsets.all(10)),
    ),
    child: Text(
        "Login",
        style: TextStyle(
        height: 1.0,
        fontSize: 30,
        color: Colors.white,
        ),
    ),
    onPressed: () => {print("login")},
    ),  
),

see I used the style property for the TextButton Widget to insert the padding and the background and for both I used MaterialStateProperty.all

Upvotes: 46

Alexander Kremenchuk
Alexander Kremenchuk

Reputation: 396

You may set different values for MaterialStates:

class TextButtonPadding extends StatelessWidget {
  EdgeInsets _getPadding(Set<MaterialState> states) {
    const interactiveStates = <MaterialState>{
      MaterialState.pressed,
      MaterialState.hovered,
    };
    if (states.any(interactiveStates.contains)) {
      return EdgeInsets.all(30.0);
    }
    return EdgeInsets.zero;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: TextButton(
          style: ButtonStyle(
            padding: MaterialStateProperty.resolveWith(_getPadding),
          ),
          onPressed: () {},
          child: Text(
            'Text Button',
          ),
        ),
      ),
    );
  }
}

Or set one value for all MaterialStates:

class TextButtonPadding extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: TextButton(
          style: ButtonStyle(
            padding: MaterialStateProperty.all(
              EdgeInsets.all(30.0),
            ),
          ),
          onPressed: () {},
          child: Text(
            'Text Button',
          ),
        ),
      ),
    );
  }
}

Please see https://api.flutter.dev/flutter/material/MaterialStateProperty-class.html

Upvotes: 3

Marcel Dz
Marcel Dz

Reputation: 2714

You can do it like this for example:

TextButton(
              onPressed: () {},
              child: Text('Hello World'),
              style: TextButton.styleFrom(
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                primary: Colors.teal,
              ),
            ),

TextButton.styleFrom() ref doc: https://api.flutter.dev/flutter/material/TextButton-class.html

example and playarounds with new button: https://www.woolha.com/tutorials/flutter-using-textbutton-widget-examples

Upvotes: 11

Related Questions