颤振 onPressed 函数

Yas*_*lte 3 function button flutter

onPressed 中出错尝试传递 onPressed 函数但出现错误。

错误 - 参数类型“Function”无法分配给参数类型“void Function()?”。在此输入图像描述

class CircleButton extends StatelessWidget {
  CircleButton({required this.icon, required this.onPressed});
  final IconData icon;
  final Function onPressed;                 [![Error][1]][1]//Here
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Icon(icon),
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
        elevation: 6.0,
        primary: Color(0xFF4C4F5E),
        shape: CircleBorder(),
        padding: EdgeInsets.all(15),
      ),);
  }}
Run Code Online (Sandbox Code Playgroud)

Mig*_*ivo 7

类型Function实际上就是一种dynamic Function类型。

需要ElevatedButtonavoid Function到它的onPressed方法,实际上有一个类型是typedef它的 a :VoidCallback

因此,将您的替换Function onPressedVoidCallback onPressed.