首页 > 解决方案 > 在颤动中的按钮内使用函数的行为不符合预期

问题描述

我为按钮创建了一个类来重新设计它并在我的所有应用程序中使用它

  const PrimaryButton({
    Key key,
    @required this.text,
    @required this.paddingH,
    this.press,
  }) : super(key: key);

  final String text;
  final VoidCallback press;
  final double paddingH;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: press,
      style: ElevatedButton.styleFrom(
        primary: AppTheme.hexColor,
        padding: EdgeInsets.symmetric(horizontal: paddingH),
        elevation: 2,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      ),
      child: Text(
        text,
        style: const TextStyle(
            fontSize: 15, letterSpacing: 2.2, color: Colors.white),
      ),
    );
  }
}

我试图将类型从 voidCallback 更改为 Function 或 void 但它不起作用。

问题出在我的代码中的示例:

                  PrimaryButton(
                    text: "SAVE",
                    press:  submitPassword,
                    paddingH: 30,
                  ),

我想在 PrimaryButton 中传递这个函数

    submitPassword() {
    if (newPassword.text == confirmPassword.text) {
      debugPrint("if");
    } else {
      debugPrint("else");
      BotController()
          .botText("passwords do not mach\n write them again please");
    }
  }

但它没有用,它总是给我 debugPrint("if");

我认为这是调用该函数的问题,但我无法弄清楚。

我看到了这个答案,但对我没有帮助 https://stackoverflow.com/a/50361572/13470875

解决方案是什么?为什么会这样?谢谢

标签: flutterdart

解决方案


推荐阅读