简体   繁体   English

无法更改 flutter 中 ElevatedButton 的宽度

[英]Unable to change the width of an ElevatedButton in flutter

I am unable to reduce the width of ElevatedButton eventhough I've put it inside a SizedBox.我无法减少 ElevatedButton 的宽度,尽管我已将它放在 SizedBox 中。 Given below is my code:下面给出的是我的代码:

SizedBox(
             width: 70.0,
             child: ElevatedButton(
               onPressed: () {},
               style: ElevatedButton.styleFrom(
                   padding:
                       EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),
                   shape: RoundedRectangleBorder(
                       borderRadius: BorderRadius.circular(20.0)),
                   primary: Color(0xff78048e)),
               child: Text(
                 "CLICK ME",
                 style: TextStyle(color: Colors.white, fontSize: 18),
               ),
             ),
           ),

you can set maximumSize property of the style which will change the size of the button您可以设置样式的 maximumSize 属性,这将改变按钮的大小

style: ElevatedButton.styleFrom(
             maximumSize: Size(maximumWidth,maximumHeight),
               padding:
                   EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),
               shape: RoundedRectangleBorder(
                   borderRadius: BorderRadius.circular(20.0)),
               primary: Color(0xff78048e)),

By default the minimum size of the elevated button is => Size(64, 36), use Size.zero to make button with no minimum size默认情况下,提升按钮的最小尺寸为 => Size(64, 36),使用 Size.zero 使按钮没有最小尺寸

Also check out the parent widget, it may forces child to take up a lot of width还要检查父部件,它可能会迫使子部件占用很多宽度

ElevatedButton(
               onPressed: () {},
               style: ElevatedButton.styleFrom(
                    minimumSize: Size.zero,
                   padding:
                       EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),
                   shape: RoundedRectangleBorder(
                       borderRadius: BorderRadius.circular(20.0)),
                   primary: Color(0xff78048e)),
               child: Text(
                 "CLICK ME",
                 style: TextStyle(color: Colors.white, fontSize: 18),
               ),
             ),

You are using padding EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0), which is forcing the button to have a large width.您正在使用填充EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),这会强制按钮具有较大的宽度。

Change改变

padding:  EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),

to

padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),

According to docs ElevatedButton has default minimum size of Size(64, 36) .根据文档, ElevatedButton的默认最小尺寸为Size(64, 36) You can change this by overriding the minimumSize property inside style .您可以通过覆盖style中的minimumSize属性来更改它。

Example:例子:

ElevatedButton(
  style: ElevatedButton.styleFrom(
    padding: const EdgeInsets.symmetric(
    horizontal: 10.0, vertical: 10.0),
    minimumSize: const Size(30, 20), // 👈 Change this according to your requirement
  ),
  onPressed: () {},
  child: const Text("Click Me"),
),

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM