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

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),
      ),
    );
  }

回答

我遵循了这里的指南: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),
    ),
  );
}


回答

你可以做这样的事情。

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'),
    );
  }

示例按钮

参考

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

新按钮和按钮主题


以上是FlutterFlatButton已弃用-具有宽度和高度的替代解决方案的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>