我有一个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这是最终结果的屏幕截图