简体   繁体   English

在 Flutter 中制作自定义小部件时,onPress 不起作用

[英]onPress not working when make Custom Widget in Flutter

i have some issue don't know what happanning when make custom widget class and pass onpress function its not working.我有一些问题不知道在制作自定义小部件 class 并通过 onpress function 时会发生什么,它不起作用。

RoundButton dart file RoundButton dart文件

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

Where I call that widget.我称之为小部件的地方。

RoundedButton(
          text: "LOGIN",
          press: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return LoginScreen();
                },
              ),
            );
          },
        )

I tried like but not working我试过但没有工作

onPressed: press()

onPressed: (){press;}

onPressed: () =>press

But when I add the Navigation function directly at the RoundButton widget.但是当我直接在 RoundButton 小部件上添加导航 function 时。

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

It worked.有效。

But don't know why custom function not working.但不知道为什么自定义 function 不起作用。

the only we need to do is add.call() to the end of the function to be void.我们唯一需要做的是 add.call() 到 function 的末尾是无效的。 onPressed: ()=>onPressed to onPressed: ()=>onPressed.call() onPressed: ()=>onPressed 到 onPressed: ()=>onPressed.call()

@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.
 child: Text(text),
          style: TextButton.styleFrom(
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.symmetric(vertical: 16, horizontal: 40)),
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当Flutter Drawer提取到自定义小部件时会延迟加载 - Flutter Drawer lazy load when extracted to custom widget 为什么在 flutter 中使用堆栈小部件时显示/隐藏密码不起作用 - why show/hide password not working when use stack widget in flutter Flutter 小部件包裹在自定义小部件中 - Flutter widget wrapped inside custom widget 如果我们在 flutter 应用程序中进行发布构建,自定义声音将不起作用 - Custom sound not working if we make release build in flutter app Flutter:Imageblur 过滤器正在填充整个父窗口小部件。 我尝试使用具有自定义高度的定位小部件和容器。但不工作 - Flutter : Imageblur filter is filling the whole parent widget . I tried using positioned widget and container with custom heights.But not working TouchableOpacity onPress 不适用于 Android - TouchableOpacity onPress not working on Android Flutter:从自定义小部件打开抽屉 - Flutter: Open Drawer from a custom widget 自定义小部件ListView无法正常工作 - Custom widget ListView not working properly 如何在 flutter 中禁用 RadioListTile 小部件 - How to make RadioListTile widget disabled in flutter 是否可以在 Flutter 的 Widget 中创建 If else 语句 - Is it possible to make a If else statment in an Widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM