我想对“注册”按钮设置一些条件。当我不更改任何信息(姓名、地址和电子邮件)时,我想禁用按钮。该按钮仅在我更改主题之一时有效
我的代码:
Row(
mainAxisAlignment:
MainAxisAlignment.end,
children: [
Container(
margin: EdgeInsets.symmetric(
vertical: 10),
width: size.width * 0.4,
child: ElevatedButton(
onPressed: () {
editUserProfile();
},
child: Text('Enregistrer'),
style:
ElevatedButton.styleFrom(
primary: Colors.transparent,
shape:
RoundedRectangleBorder(
borderRadius:
BorderRadius
.circular(
20),
side: BorderSide(
color: Colors
.white)),
),
)),
void editUserProfile() async {
setState(() {});
String name = _nameController.text;
String email = _emailController.text;
String adress = _adressController.text;
userApi.editUserProfile(name, email, adress).then((data) {
print(data);
if (data != null) {
}
setState(() {
enableup = false;
enableadress = false;
enableemail = false;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data)));
}).catchError((error) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(error.toString())));
});
setState(() {});
}
当我不修改任何文本输入时,我如何在 OnPressed 上设置条件以禁用按钮?
答案 0 :(得分:0)
当您编辑某个值时,按下“铅笔”图标,将值从 false 更改为 true。
bool isModified = false;
...
// If Name, Email, Address is changed, change 'isModified' value and call setState to update ui.
setState(() {
isModified = true;
}
之后,更改“onPressed”位置,如下所示。
Row(
mainAxisAlignment:
MainAxisAlignment.end,
children: [
Container(
margin: EdgeInsets.symmetric(
vertical: 10),
width: size.width * 0.4,
child: ElevatedButton(
onPressed: isModified ? () {
editUserProfile();
} : null,
child: Text('Enregistrer'),
style:
ElevatedButton.styleFrom(
primary: Colors.transparent,
shape:
RoundedRectangleBorder(
borderRadius:
BorderRadius
.circular(
20),
side: BorderSide(
color: Colors
.white)),
),
)),
答案 1 :(得分:0)
也许有帮助
bool isProfileChanged = false;
@override
void initState() {
super.initState();
_emailController.addListener(() {
if (!isProfileChanged) {
setState(() {});
}
isProfileChanged = true;
});
}
child: ElevatedButton(
onPressed: isProfileChanged ? () {
editUserProfile();
} : null,
child: Text('Enregistrer'),
);