【问题标题】:Flutter RaisedButton not getting correctly roundedFlutter RaisedButton 未正确舍入
【发布时间】:2020-07-02 12:42:04
【问题描述】:

我正在学习使用 Flutter,现在我在设置按钮样式时偶然发现了一个问题。即使遵循此处提出的指南和问题,我的样式按钮也会出现“错误”,可能是因为我的代码中存在问题。检查按钮现在的外观:

这是我的按钮代码(问题就在那里,因为它甚至只显示按钮),我认为问题出在我里面的容器中,这可能是错误的,但我无法做到发现问题:

 RaisedButton(
            onPressed: () {},
            textColor: Colors.white,
            shape: new RoundedRectangleBorder(
              borderRadius: new BorderRadius.circular(30.0),
            ),
            child: Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: <Color>[
                    Colors.purple,
                    Color.fromARGB(1, 247, 120, 150),
                  ],
                ),
              ),
              padding: const EdgeInsets.all(20.0),
              child: const Text('Empezar partida',
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.white,
                      fontWeight: FontWeight.bold)),
            ),
          )

【问题讨论】:

  • 你想让那个按钮完全填满吗?
  • @LonelyWolf 我的想法是内部按钮的渐变占据了整个按钮形状,这意味着稍微圆形,具有边框半径。

标签: flutter dart flutter-layout


【解决方案1】:

试试这个代码。

Container(
             height: 50.0,
             margin: EdgeInsets.all(10),
             child: RaisedButton(
               onPressed: () {},
               shape: RoundedRectangleBorder(
                   borderRadius: BorderRadius.circular(80.0)),
               padding: EdgeInsets.all(0.0),
               child: Ink(
                 decoration: BoxDecoration(
                     gradient: LinearGradient(
                       colors: [Color(0xff374ABE), Color(0xff64B6FF)],
                       begin: Alignment.centerLeft,
                       end: Alignment.centerRight,
                     ),
                     borderRadius: BorderRadius.circular(30.0)),
                 child: Container(
                   constraints:
                       BoxConstraints(maxWidth: 250.0, minHeight: 50.0),
                   alignment: Alignment.center,
                   child: Text(
                     "Gradient Button",
                     textAlign: TextAlign.center,
                     style: TextStyle(color: Colors.white, fontSize: 15),
                   ),
                 ),
               ),
             ),
           ),

使用 Ink 小部件在您的按钮中绘制渐变。 https://api.flutter.dev/flutter/material/Ink-class.html

【讨论】:

  • 这个似乎有效!这不完全是我想要的显示方式,但它是某种东西。无论如何,我会提出我的答案,因为我的问题的具体问题可以通过不同的方式解决。无论如何我都赞成你的解决方案!
  • 如果它解决了您的问题,请接受答案。谢谢。
【解决方案2】:

检查此代码

RaisedButton(
 padding: const EdgeInsets.all(0.0),
   shape: RoundedRectangleBorder(
   borderRadius: BorderRadius.circular(30.0),),
  onPressed: () {},
  textColor: Colors.white,
  child: Container(
    decoration: const BoxDecoration(
      shape: BoxShape.rectangle,
      borderRadius: BorderRadius.all(Radius.circular(30)),
      gradient: LinearGradient(
        colors: <Color>[
          Colors.purple,
          Color.fromARGB(1, 247, 120, 150),
        ],
      ),
    ),
    padding: const EdgeInsets.all(20.0),
    child: const Text('Empezar partida',
        style: TextStyle(
            fontSize: 20,
            color: Colors.white,
            fontWeight: FontWeight.bold)),
  ),
),

【讨论】:

  • 也不行;我仍然看到内部容器(及其渐变)和实际按钮之间的填充:i.imgur.com/JY1Vp2j.png
  • 添加填充:EdgeInsets.all(0.0) 它将删除填充并为凸起按钮添加形状更新我的代码检查
  • 我刚刚修复了它,为每个方向添加了特定的填充!我使用了您的代码并将以下行添加到 Container() 中: EdgeInsets.only(left: 25.0, top: 18, bottom: 18, right: 25) 现在它工作正常:)
【解决方案3】:

在@AshwithSaldanha 和@VarunKamani 的解决方案的帮助下(你有我的赞成票!),我设法找到了一个可以很好地解决我的问题的不同解决方案:我刚刚在我的 Container() 中使用了 EdgeInsets.only () 并提供以下参数来修复它:

padding: EdgeInsets.only(left: 27.0, top: 19, bottom: 19, right: 27)

这只有在 RaisedButton() 的填充为 0 时才有效:

padding: const EdgeInsets.all(0.0)

最终代码如下所示:

      RaisedButton(
        padding: const EdgeInsets.all(0.0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(30.0),
        ),
        onPressed: () {},
        textColor: Colors.white,
        child: Container(
          decoration: const BoxDecoration(
            shape: BoxShape.rectangle,
            borderRadius: BorderRadius.all(Radius.circular(30)),
            gradient: LinearGradient(
              colors: <Color>[
                Colors.purple,
                Color.fromARGB(1, 247, 120, 150),
              ],
            ),
          ),
          padding:
              EdgeInsets.only(left: 27.0, top: 19, bottom: 19, right: 27),
          child: const Text('Empezar partida',
              style: TextStyle(
                  fontSize: 21,
                  color: Colors.white,
                  fontWeight: FontWeight.bold)),
        ),
      ),

这就是它的外观:

【讨论】:

  • 你还是有同样的问题。如果您的文字太短,此解决方案将不起作用。codepen.io/varunkamani/pen/pogpbRp
  • 确实如此,但仅适用于 1 个字母的文本。 2 或 3 的渲染效果也很好,只需更改 EdgeInsets.only() 参数即可修复。您会提出哪种更通用的解决方案?如果它的作用相同并且适用于所有边缘情况,我会选择它作为正确答案。以前的答案没有提供预期的结果。
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 2019-04-30
  • 2014-03-08
  • 2012-03-07
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 2021-10-20
相关资源
最近更新 更多