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

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),
              )),

效果如下:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/MrLizuo/article/details/127639743