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),
),);
}}
最佳答案
Function
类型实际上是一个dynamic Function
类型。
ElevatedButton
需要一个 void Function
到它的 onPressed
方法,实际上有一个类型是 typedef
为:VoidCallback
因此,将您的 Function onPressed
替换为 VoidCallback onPressed
。
关于Flutter onPressed 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71208496/