Android 在颤振中创建自定义小部件时onPress不工作

Android 在颤振中创建自定义小部件时onPress不工作,android,flutter,click,Android,Flutter,Click,我有一些问题,不知道当自定义小部件类和传递onpress函数不工作时会发生什么 @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; return Container( margin: EdgeInsets.symmetric(vertical: 8), width: size.width * 0.8, child: C

我有一些问题,不知道当自定义小部件类和传递onpress函数不工作时会发生什么

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin: EdgeInsets.symmetric(vertical: 8),
      width: size.width * 0.8,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(30),
        child: TextButton(
          onPressed: press(),   // call just press ,here u r calling the func // right after the widget is rendered. Its worked fine for me in versions b4 //null safety.
圆钮省道锉

class RoundedButton extends StatelessWidget {
  final String text;
  final Function press;
  final Color color, textColor;

  const RoundedButton({
    Key? key,
    required this.text,
    required this.press,
    this.color = primaryColor,
    this.textColor = Colors.white,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin: EdgeInsets.symmetric(vertical: 8),
      width: size.width * 0.8,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(30),
        child: TextButton(
          onPressed: press(),
          child: Text(text),
          style: TextButton.styleFrom(
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.symmetric(vertical: 16, horizontal: 40)),
        ),
      ),
    );
  }
}
我称之为widget的地方

RoundedButton(
          text: "LOGIN",
          press: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return LoginScreen();
                },
              ),
            );
          },
        )
我试过了,但没有成功

onPressed: press()

onPressed: (){press;}

onPressed: () =>press
但是当我直接在RoundButton小部件上添加导航功能时

press: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) {
              return LoginScreen();
            },
          ),
        )
成功了

但不知道为什么自定义函数不起作用

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin: EdgeInsets.symmetric(vertical: 8),
      width: size.width * 0.8,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(30),
        child: TextButton(
          onPressed: press(),   // call just press ,here u r calling the func // right after the widget is rendered. Its worked fine for me in versions b4 //null safety.

onPressed:press,
flatter 2.2 onPressed:press创建错误。参数类型“Function”无法分配给参数类型“void Function()?”。已重试;(
final void callback press;
将起作用,然后
on按下:按,