在Flutter Container> FlatButton中删除填充

Don*_*ots 11 flutter

我想删除FlatButton的默认边距,但似乎无法设置/覆盖它.

带填充的按钮

Column(children: <Widget>[
      Container(
          children: [
            FractionallySizedBox(
              widthFactor: 0.6,
              child: FlatButton(
                  color: Color(0xFF00A0BE),
                  textColor: Color(0xFFFFFFFF),
                  child: Text('LOGIN', style: TextStyle(letterSpacing: 4.0)),
                  shape: RoundedRectangleBorder(side: BorderSide.none)))),
      Container(
          margin: const EdgeInsets.only(top: 0.0),
          child: FractionallySizedBox(
              widthFactor: 0.6,
              child: FlatButton(
                  color: Color(0xFF00A0BE),
                  textColor: Color(0xFF525252),
                  child: Text('SIGN UP',
                      style: TextStyle(
                          fontFamily: 'Lato',
                          fontSize: 12.0,
                          color: Color(0xFF525252),
                          letterSpacing: 2.0)))))
    ])
Run Code Online (Sandbox Code Playgroud)

我遇到过类似的事情ButtonTheme,debugDumpRenderTree()但是却无法正确实现它们.

Joe*_*röm 38

我发现将按钮包装在ButtonTheme 中更容易。

指定 maxWith 和高度(设置为零以包裹子项),然后将您的按钮作为子项传递。

您还可以将大部分按钮属性从按钮移动到主题,以在一个小部件中收集所有属性。

ButtonTheme(
  padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0), //adds padding inside the button
  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, //limits the touch area to the button area
  minWidth: 0, //wraps child's width
  height: 0, //wraps child's height
  child: RaisedButton(onPressed: (){}, child: Text('Button Text')), //your original button
);
Run Code Online (Sandbox Code Playgroud)


And*_*sky 33

FlatButton(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,)
Run Code Online (Sandbox Code Playgroud)

  • 你知道……这就是诀窍。不要以为我会遇到那个。愚蠢的我,正在寻找边距/填充语法。 (4认同)
  • 首先我也考虑了边距/填充))) (2认同)
  • 根据[文档](https://api.flutter.dev/flutter/material/FlatButton-class.html),FlatButton 的最小尺寸为 88.0 x 36.0,因此上述解决方案不适用于较小的按钮,需要包装在 ButtonTheme 内。检查@joel-broström 的答案。 (2认同)

Dur*_*rdu 31

FlatButton提供,在 flutter 的 git 上引入了幻影填充

如果有人需要一个带有 onPressed 事件而不用 Flutter 填充它的小部件。

你应该使用InkWell

InkWell(
    child: Center(child: Container(child: Text("SING UP"))),
    onTap: () => onPressed()
);
Run Code Online (Sandbox Code Playgroud)

响应触摸的材料的矩形区域。


Cop*_*oad 19

使用ElevatedButtonTextButton(推荐):

在此处输入图片说明


设置minimumSizepadding归零。例如:

  • ElevatedButton(以前RaisedButton

    ElevatedButton(
      style: ElevatedButton.styleFrom(
        minimumSize: Size.zero, // <-- Add this
        padding: EdgeInsets.zero, // <-- and this
      ),
      onPressed: () {},
      child: Text('Button'),
    )
    
    Run Code Online (Sandbox Code Playgroud)
  • TextButton(以前FlatButton

    TextButton(
      style: TextButton.styleFrom(
        minimumSize: Size.zero, // <-- Add this
        padding: EdgeInsets.zero, // <-- and this
      ),
      onPressed: () {},
      child: Text('Button'),
    )
    
    Run Code Online (Sandbox Code Playgroud)

您也可以使用原始 MaterialButton

MaterialButton(
  onPressed: () {},
  color: Colors.blue,
  minWidth: 0,
  height: 0,
  padding: EdgeInsets.zero,
  child: Text('Button'),
)
Run Code Online (Sandbox Code Playgroud)

  • 您应该添加 `tapTargetSize: MaterialTapTargetSize.shrinkWrap`。 (6认同)
  • 这应该是公认的答案,因为 Flutter 已经更新了按钮 (3认同)

Ari*_*ain 17

文本按钮以前是FlatButton

要删除2 个 TextButton 之间的间距,tapTargetSize请使用 tapTargetSize 设置为MaterialTapTargetSize.shrinkWrap

删除填充, 请设置paddingEdgeInsets.all(0)

TextButton(
    child: SizedBox(),
    style: TextButton.styleFrom(
        backgroundColor: Colors.red,
        padding: EdgeInsets.all(0),
        tapTargetSize: MaterialTapTargetSize.shrinkWrap
    ),
    onPressed: () {
    print('Button pressed')
    },
),
Run Code Online (Sandbox Code Playgroud)


Ars*_*hak 14

对于所有想知道如何操作的人remove the default padding around the text of a FlatButton,您可以改用RawMaterialButton并将约束设置为 BoxConstraints(),这会将按钮的默认最小宽度和高度重置为零

RawMaterialButton可用于配置不依赖于任何继承主题的按钮。因此,我们可以根据需要自定义所有默认值。

例子:

RawMaterialButton(
    constraints: BoxConstraints(),
    padding: EdgeInsets.all(5.0), // optional, in order to add additional space around text if needed
    child: Text('Button Text')
)
Run Code Online (Sandbox Code Playgroud)

请参阅文档以进一步定制。


ahm*_*afa 10

FlatButton(
  padding: EdgeInsets.all(0) 
)
Run Code Online (Sandbox Code Playgroud)

为我做了把戏

  • 根据[文档](https://api.flutter.dev/flutter/material/FlatButton-class.html),FlatButton 的最小尺寸为 88.0 x 36.0,因此上述解决方案不适用于较小的按钮,需要包装在 ButtonTheme 内。检查@joel-broström 的答案。 (7认同)
  • 或者更好的“EdgeInsets.zero” (4认同)

小智 5

您还可以通过用一个大小的框包围它来更改按钮宽度:

SizedBox(
  width: 40,
  height: 40,
  child: RaisedButton(
    elevation: 10,
    onPressed: () {},
    padding: EdgeInsets.all(0), // make the padding 0 so the child wont be dragged right by the default padding
    child: Container(
      child: Icon(Icons.menu),
    ),
  ),
),

Run Code Online (Sandbox Code Playgroud)