【问题标题】:how to extract a widget that has an onPressed method after upgrading flutter to be Flutter 2.0.4将flutter升级为Flutter 2.0.4后如何提取具有onPressed方法的小部件
【发布时间】:2021-07-05 20:17:20
【问题描述】:

过去当我用来提取具有按钮和 onPressed 方法的小部件时

我曾经用函数数据类型

的变量替换了onPressed方法接受的回调函数

但是现在当我尝试提取一个我称为 RoundedButton 的小部件时 将 flutter 版本升级到 2.0.4 后,这是 Flutter 的最新稳定版本。

它给了我这个错误:

**The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.**

这是onPressed: onPressed,代码行下方的红线代码:

class RoundedButton extends StatelessWidget {
  final String title;
  final Color color;
  final Function onPressed;

  RoundedButton(
      {required this.onPressed, required this.color, required this.title});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        color: color,
        borderRadius: BorderRadius.all(Radius.circular(30.0)),
        elevation: 5.0,
        child: MaterialButton(
          onPressed: onPressed,
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            title,
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-upgrade


    【解决方案1】:

    使用 最终的 VoidCallback onPressed; //final void Function() onPressed;

    【讨论】:

      【解决方案2】:

      而不是它的旧类型:`

      功能

      我弄清楚了flutter 2.0.4中使用的onPressed方法的类型

      这是`

      VoidCallback?

      通过导入 **dart:io**

      【讨论】:

        猜你喜欢
        • 2021-11-11
        • 2018-08-10
        • 2021-12-12
        • 1970-01-01
        • 2020-11-25
        • 2019-11-21
        • 2023-02-18
        • 2023-03-18
        • 2021-08-13
        相关资源
        最近更新 更多