Flutter中Button如何去掉默认的外边距 和 内边距

本文介绍了如何在Flutter中删除Button的默认内外边距。通过调整样式,可以实现TextButton的外边距和内边距消除,从而达到自定义布局的效果。
Flutter中Button如何去掉默认的外边距 和 内边距

默认情况下,Button是带有内边距的和外边距的。

TextButton(
     style: TextButton.styleFrom(
           backgroundColor: Colors.red,
           tapTargetSize: MaterialTapTargetSize.shrinkWrap,
       ),
      child: Text("TextButton 1",
              style: TextStyle(
                    color: Colors.white,
               )),
              onPressed: () {},
      ),

TextButton(
              onPressed: () {},
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.resolveWith((states) {
                  return Colors.yellow;
                }),
              ),
              child: Text(
                "TextButton2",
                style: TextStyle(color: Colors.white),
              )),

如下图:
在这里插入图片描述

  • 去掉外边距

    //TextButton 中的 style  的 tapTargetSize 属性
     tapTargetSize: MaterialTapTargetSize.shrinkWrap
    
  • 去掉内边距

    //TextButton 中的 style  的 padding 属性 
    padding: EdgeInsets.zero,
    
更改之后

TextButton 1 去掉了内边距和外边距

TextButton 2 去掉了外边距

TextButton(
     style: TextButton.styleFrom(
           padding: EdgeInsets.all(0),//内边距
           backgroundColor: Colors.red,
           tapTargetSize: MaterialTapTargetSize.shrinkWrap,//外边距
       ),
      child: Text("TextButton 1",
              style: TextStyle(
                    color: Colors.white,
               )),
              onPressed: () {},
      ),

TextButton(
              onPressed: () {},
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.resolveWith((states) {
                  return Colors.yellow;
                }),
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,//外边距
              ),
              child: Text(
                "TextButton2",
                style: TextStyle(color: Colors.white),
              )),

效果如下:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值