我已经将TextFormField
Widget从编辑页面中分离出来,以清除混乱。
这在我的CustomFormField
:中
class CustomFormField extends StatefulWidget {
String? val;
bool? isNumberPadRequired;
CustomFormField({
Key? key,
this.val = '',
this.isNumberPadRequired = false,
}) : super(key: key);
@override
_CustomFormFieldState createState() => _CustomFormFieldState();
}
class _CustomFormFieldState extends State<CustomFormField> {
@override
Widget build(BuildContext context) {
return Container(
height: 40,
child: Center(
child: TextFormField(
initialValue: widget.val!,
onSaved: (val) {
print(val);
if (val!.isNotEmpty) {
setState(() {
widget.val = val;
});
}
},
keyboardType: widget.isNumberPadRequired!
? TextInputType.number
: TextInputType.text,
textAlign: TextAlign.left,
decoration: InputDecoration(
isDense: true,
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(
vertical: 25.0,
horizontal: 10.0,
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: CustomColors.secondary,
width: 2,
),
),
),
),
),
);
}
}
在我的EditProfilePage
中,我有一些字符串值,如name
、email
和number
,它们的当前值为:
String email = user.email; // john@john.com
String name = user.name; // John Doe
在Form
:内部
return Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildName(),
....
],
),
);
Widget _buildName() {
return FormFieldWrapper(
label: "Name",
child: CustomFormField(
val: firstName, // newly typed name: Jane Monroe
),
);
}
但当我尝试调用_handleUpdate()
:时
_updateProfile() async {
(_formKey.currentState as FormState).save();
print(name);
}
我得到了旧的价值观,即无名氏。
您应该将String Function参数添加到自定义文本字段中。当调用saves方法时,应该为更新名称或任何变量调用此参数。
示例;
class CustomFormField extends StatefulWidget {
String? val;
bool? isNumberPadRequired;
final Fuction(String savedValue) onSaved;
CustomFormField({
Key? key,
this.val = '',
required this.onSaved,
this.isNumberPadRequired = false,
}) : super(key: key);
@override
_CustomFormFieldState createState() => _CustomFormFieldState();
}
class _CustomFormFieldState extends State<CustomFormField> {
@override
Widget build(BuildContext context) {
return Container(
height: 40,
child: Center(
child: TextFormField(
initialValue: widget.val!,
onSaved: (val) {
print(val);
if (val!.isNotEmpty) {
widget.onSaved(val);
}
},
keyboardType: widget.isNumberPadRequired!
? TextInputType.number
: TextInputType.text,
textAlign: TextAlign.left,
decoration: InputDecoration(
isDense: true,
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(
vertical: 25.0,
horizontal: 10.0,
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: CustomColors.secondary,
width: 2,
),
),
),
),
),
);
}
}
并应在Form窗口小部件内部实现;
return Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildName(),
....
],
),
);
Widget _buildName() {
return FormFieldWrapper(
label: "Name",
child: CustomFormField(
val: firstName, // newly typed name: Jane Monroe
onSaved: (String savedValue){
name = savedValue;
},
),
);
}