【问题标题】:How to use Flatbutton also after upgrading flutter升级 flutter 后如何使用 Flatbutton
【发布时间】:2022-12-16 02:58:25
【问题描述】:

升级 flutter 后,FlatButtons 不再起作用,因为它们在较新版本的 flutter 中被删除并替换为 TextButtons。您可以使 TextButtons 看起来像 Flatbuttons。但是我真的必须为我使用的每个FlatButton 都这样做吗?没有更快的解决方案或解决方法吗?

【问题讨论】:

    标签: flutter dart version flatbutton


    【解决方案1】:

    注意:FlatButton 没有包。


    解决方案

    我创建了一个模仿 FlatButton 的小部件。它具有 FlatButton 的基本属性。如果您需要更多属性,您只需添加它们即可。

    这是小部件代码:

    import 'package:flutter/material.dart';
    
    class FlatButton2 extends StatefulWidget {
      FlatButton2({
        this.onPressed,
        this.child,
        this.padding,
        this.height,
        this.width,
        this.minWidth,
        this.minHeight,
        this.color,
      });
    
      var padding;
      var width;
      var height;
      var onPressed;
      var child;
      var minWidth;
      var minHeight;
      var color;
    
      @override
      State<FlatButton2> createState() => _FlatButton2State();
    }
    
    class _FlatButton2State extends State<FlatButton2> {
    
      @override
      Widget build(BuildContext context) {
        return Container(
                width: widget.width,
                height: widget.height,
                child: TextButton(
                  onPressed: widget.onPressed,
                  child: widget.child,
                  style: TextButton.styleFrom(
                    padding: widget.padding,
                    backgroundColor: widget.color,
                    minimumSize: Size(
                      widget.minWidth ?? 0.0,
                      widget.minHeight ?? 0.0,
                    ),
                  ),
                ),
              );
      }
    }
    

    因此,在将 Widget 添加到项目后,您要做的就是:

    • 在每个文件中,你需要FlatButton2,导入里面有Widget的文件。
    • FlatButton转为FlatButton2

    希望能帮助到你!

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2021-08-13
      • 2019-12-24
      • 1970-01-01
      • 2021-06-11
      • 2020-06-29
      • 2021-01-21
      • 2020-01-14
      • 2022-07-16
      相关资源
      最近更新 更多