Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the buttons in Flutter Web have a margin?

I have the following code:

Scaffold(
      appBar: AppBar(
        title: Text('Test'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {},
              child: Text('Boton 1'),
            ),
            ElevatedButton(
              onPressed: () {},
              child: Text('Boton 2'),
            ),
            ElevatedButton(
              onPressed: () {},
              child: Text('Boton 3'),
            ),
          ],
        ),
      ),
    )

Why on mobile do the buttons have a margin but on the web they do not?

Note: I know I can add padding/margin manually, set default button style, etc. I want to know why the default behaviour is different.

enter image description here

like image 423
Yayo Arellano Avatar asked May 18 '21 05:05

Yayo Arellano


People also ask

What is the difference between padding and margin in Flutter?

Margin means the spacing outside of the border, while padding is the spacing inside of the border.

Do you use padding or margin in your flutter widgets?

This also seems to be what some flutter widgets do (button bar is wrapped in padding that is more of a margin in my opinion) Pretty much the only time I actually use margin is if I want a container with both margin and padding.

What are the new buttons in flutter?

The names of the new Flutter buttons are meant to sync Flutter with the Material Design spec. This means the buttons are up to date with new guidelines and also have new styles. Put simply, the new buttons available in Flutter are are easier to understand and use. They make it simple to define common themes at the app and widget levels.

How to add margins to a widget using container?

The Container has a property named margin. Hence, you can add margins to a widget as given below: Container ( margin: EdgeInsets.only (left:30.0, top:20.0,right:30.0,bottom:20.0), child: Text ("Check out my margins!"), )

Should I use a container with margin or padding?

Using a container with margin you have to look closer to see if anything else is being done with the container. This also seems to be what some flutter widgets do (button bar is wrapped in padding that is more of a margin in my opinion) Pretty much the only time I actually use margin is if I want a container with both margin and padding.


1 Answers

Quite an interesting question. Thanks for asking.

This is basically due to the ThemeData.materialTapTargetSize parameter for the MaterialApp.

This feature decides what should be touchable dimensions of Material Button, in your case ElevatedButton.

The difference in behaviour is due to this piece of code in flutter/lib/src/material/theme_data.dart at line no 377. flutter sdk: ">=2.12.0 <3.0.0"

switch (platform) {
  case TargetPlatform.android:
  case TargetPlatform.fuchsia:
  case TargetPlatform.iOS:
    materialTapTargetSize ??= MaterialTapTargetSize.padded;
    break;
  case TargetPlatform.linux:
  case TargetPlatform.macOS:
  case TargetPlatform.windows:
     materialTapTargetSize ??= MaterialTapTargetSize.shrinkWrap;
    break;
}

The MaterialTapTargetSize.padded, make buttons take a height of 48. Whereas, the MaterialTapTargetSize.shrinkWrap does what it says and removes that constraint.

If you have your MaterialApp like this,

MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
  home: CupertinoPickerExample(),
)

Then the out put is,

enter image description here

like image 63
Nisanth Reddy Avatar answered Oct 19 '22 10:10

Nisanth Reddy