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

Moo*_*Moo 6 deprecated flutter flatbutton

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

这是我的工作 FlatButton。如何使用 textButton 或提升按钮解决它?

_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),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

小智 17

FlatButton已弃用,因此最好的选择是ElevatedButton.

这是代码:

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


Pat*_*ara 9

我遵循了这里的指南:https : //flutter.dev/docs/release/break-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),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)


hba*_*ara 8

你可以做这样的事情。

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'),
    );
  }
Run Code Online (Sandbox Code Playgroud)

示例按钮

在此处输入图片说明

参考

迁移到新材质按钮及其主题

新按钮和按钮主题