【发布时间】:2023-01-07 18:59:47
【问题描述】:
在“AlertDialog-Form”中,我想对双变量使用 ValueChanged 回调。在 TextFormField 中使用它会出错
不能将参数类型“void Function(double)”分配给参数类型“void Function(String)?”。
如何将其返回为 double?
class GewichtFormWidget extends StatelessWidget { final double gewicht; final DateTime messzeitpunkt; final ValueChanged<double> onChangedGewicht; final ValueChanged<DateTime> onChangedDate; final VoidCallback onSavedGewicht; GewichtFormWidget({ this.gewicht = 0, DateTime? messzeitpunkt, required this.onChangedGewicht, required this.onChangedDate, required this.onSavedGewicht, }) : this.messzeitpunkt = messzeitpunkt ?? DateTime.now(); @override Widget build(BuildContext context) { return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, children: [ buildGewicht(), ], ), ); } Widget buildGewicht() => TextFormField( style: TextStyle(color: Colors.grey[800]), initialValue: gewicht.toString(), keyboardType: TextInputType.number, decoration: ThemeHelper() .textInputDecorationGreen('Gewicht', 'Gib das Gewicht ein.'), validator: (val) { if (val!.isEmpty) { return "Bitte gib ein Gewicht ein."; } return null; }, onChanged: onChangedGewicht , ); }这是警报对话框:
return AlertDialog( content: //some other content GewichtFormWidget( onChangedGewicht: (gewicht) => setState((() => this.gewicht = gewicht)), onChangedDate: (messzeitpunkt) => setState((() => this.messzeitpunkt = messzeitpunkt)), onSavedGewicht: () {}, ) ]), );
【问题讨论】:
-
将你的
double传递参数转换为string因为你的函数的parameter数据类型必须是string类型
标签: flutter dart flutter-alertdialog