简体   繁体   English

如何将 Flutter 中的“FlatButton”转换为“TextButton”?

[英]How to convert a "FlatButton" to "TextButton" in Flutter?

I got following code:我得到以下代码:

      FlatButton.icon(
        minWidth: double.infinity,
        padding: EdgeInsets.symmetric(
          vertical: kDefaultPadding,
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        color: kPrimaryColor,
        onPressed: () {},
        icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
        label: Text(
          "New message",
          style: TextStyle(color: Colors.white),
        ),
      ).addNeumorphism(
        topShadowColor: Colors.white,
        bottomShadowColor: Color(0xFF234395).withOpacity(0.2),
      ),

It seems the FlatButtun is now deprecated and I must convert it to TextButton but when I try that, it gives me errors for minWith parameter.似乎FlatButtun现在已被弃用,我必须将其转换为TextButton但是当我尝试这样做时,它会给我minWith参数错误。 When I try to wrap it with ConstrainedBox I get other errors for padding shape, etc.当我尝试用ConstrainedBox包装它时,我得到了padding形状等的其他错误。

I don't know how make this old code work as expected before?我不知道如何让这段旧代码按预期工作?

As I've commented, you can't just "convert" a FlatButton.icon to a TextButton.icon .正如我所评论的,您不能只是将TextButton.icon FlatButton.icon The changes in the buttons are breaking changes made to Flutter:按钮中的更改打破了对 Flutter 所做的更改:

A new set of basic material button widgets and themes have been added to Flutter. The original classes have been deprecated and will eventually be removed. Flutter 添加了一组新的基本材料按钮小部件和主题。原来的类已被弃用,最终将被删除。 The overall goal is to make buttons more flexible, and easier to configure via constructor parameters or themes.总体目标是使按钮更灵活,并且更容易通过构造函数参数或主题进行配置。

So, to solve your problem, you'll have to compose your own widgets to get close to FlatButton.icon .因此,要解决您的问题,您必须编写自己的小部件以接近FlatButton.icon

For your example, you can use the Padding widget for padding, and SizedBox for the width.对于您的示例,您可以使用Padding小部件进行填充,使用SizedBox来设置宽度。 for rounded corners, you can use the style property.对于圆角,您可以使用style属性。

Your button code can look something like this using TextButton.icon :使用TextButton.icon你的按钮代码看起来像这样:

SizedBox(
        width: double.infinity,
        child: Padding(
          padding: EdgeInsets.symmetric(
            vertical: kDefaultPadding,
            child: TextButton.icon(
              //rounded corners
              style: ButtonStyle(
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                  ),
                ),
              ),

              onPressed: () {},
              icon: WebsafeSvg.asset("assets/Icons/Edit.svg", width: 16),
              label: Text(
                "New message",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ))

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

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