Flutter FlatButton 已弃用 - 具有宽度和高度的替代解决方案

Flutter FlatButton is deprecated - alternative solution with width and height

Flutter 升级后“FlatButton”已弃用,我必须改用 TextButton。我没有找到具有宽度和高度的新按钮类型的解决方案。

这是我的工作 FlatButton。我如何使用 textButton 或 elevatedButton 解决它?

_buttonPreview(double _height, double _width) {
    return FlatButton(
      onPressed: () {  },
      height: _height,
      minWidth: _width,
      color: Colors.grey,
      padding: EdgeInsets.all(0),
      child: Text(
        "some text",
        style: TextStyle(color: Colors.white),
      ),
    );
  }

你可以这样做。

FlatButton 到 TextButton 迁移

    final ButtonStyle flatButtonStyle = TextButton.styleFrom(
      primary: Colors.white,
      minimumSize: Size(88, 44),
      padding: EdgeInsets.symmetric(horizontal: 16.0),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(2.0)),
      ),
      backgroundColor: Colors.blue,
    );

    return TextButton(
      style: flatButtonStyle,
      onPressed: () {
        print('Button pressed');
      },
      child: Text('FlatButton To TextButton Migration'),
    );
  }

示例按钮

参考

Migrating to the New Material Buttons and their Themes

New Buttons and Button Themes

我按照这里的指南操作:https://flutter.dev/docs/release/breaking-changes/buttons

_buttonPreview(double _height, double _width) {
  final ButtonStyle flatButtonStyle = TextButton.styleFrom(
    minimumSize: Size(_width, _height),
    backgroundColor: Colors.grey,
    padding: EdgeInsets.all(0),
  );
  return TextButton(
    style: flatButtonStyle,
    onPressed: () {},
    child: Text(
      "some text",
      style: TextStyle(color: Colors.white),
    ),
  );
}

这对我有用,使用:

ElevatedButton(
  onPressed: () {},
  child: Text('Simple FlatButton'),
)

而不是:

FlatButton(
  onPressed: () {},
  child: Text('Simple FlatButton'),
)

创建一个您可能会像这样用于按钮的样式:

final ButtonStyle flatButtonStyle = TextButton.styleFrom(
backgroundColor: Colors.blue,
padding: EdgeInsets.all(8),
//few more styles 

);

然后在替换 flatButton 时使用上面的样式

return TextButton(
style: flatButtonStyle,
onPressed: () {},
child: Text(
  "some text",
  style: TextStyle(color: Colors.white),
),);

FlatButton 已弃用,因此它是 ElevatedButton() 的最佳选择。 这是代码:

ElevatedButton(
        style: ElevatedButton.styleFrom(
          primary: Colors.teal,
          fixedSize: Size.fromWidth(100),
          padding: EdgeInsets.all(10)
        ),
        child: Text("Press Here"),
        onPressed: (){
          //Code Here
        },
      )