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.
Margin means the spacing outside of the border, while padding is the spacing inside of the border.
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.
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.
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!"), )
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.
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With