我有一个customButton,我想通过表单验证将VoidCallback函数传递给它

 CustomButtonWidget(
                      text: 'Cadastrar',
                       callback: () async {
                       final FormState form = formKey.currentState;
                        if (form.validate()) {
                        form.save();
                        controller.cadastrar();
                        } else {
                        //snackbar
                        print('erro ao entrar');
                        }
                        }),
我的自定义ButtonClass
    class CustomButtonWidget extends Container {
  final String text;
  final VoidCallback callback;
  CustomButtonWidget({@required this.text, this.callback,});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 0.5,
      height: 50,
      child: RaisedButton(
        autofocus: false,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10)
        ),
          onPressed: this.callback,
          child: Text(
            this.text,
            style: textButton,
          ),
          color: Colors.green),
    );
  }
}
我收到这个错误
Handler: "onTap"
I/flutter (29423): Recognizer:
I/flutter (29423):   TapGestureRecognizer#9b3b5
I/flutter (29423): ═════════════════════════════════════════
或这个
I/flutter (29423): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (29423): The method 'call' was called on null.
I/flutter (29423): Receiver: null
I/flutter (29423): Tried calling: call()
I/flutter (29423):
我尝试了所有方法()=>回调或异步操作,我不知道该怎么办

最佳答案

我已经尝试过您的存储库,但发现问题不在CustomButtonWidget内。
实际上是从lib / app / modules / cadastro / cadastro_page.dart中的这一行开始的:

CustomTextFormField(
  onSaved: (value) => controller.onSavedName(value),
  // <<< ------ this line
  action: TextInputAction.next,
  text: 'Nome',
),
根据CustomTextFormField的定义,在lib / app / widgets / custom_textff_widget.dart中,强制添加
onChanged : (value) => {}
validator : (value) => {}
您一改这行
CustomTextFormField(
  onSaved: (value) => controller.onSavedName(value),
    onChanged: (value) =>
        controller.onChangeName(value),
    validator: (value) =>
        controller.nameValidate(value),
  action: TextInputAction.next,
  text: 'Nome',
),

该问题将得到解决。不要忘记在 Controller 上添加onChangeName和nameValidate
这是最终结果的屏幕截图
android - Handler onTap自定义按钮和按钮-LMLPHP

07-28 04:04
查看更多