20.4 名称、邮件、密码 保存

20.4 名称、邮件、密码 保存

img

实现步骤:


第 1 步:api 接口

put /users/me

img

lib/common/api/user.dart

1
2
3
4
5
6
7
8
9
10
11
12
/// 保存用户 first name 、 last name 、 email
static Future<UserProfileModel> saveBaseInfo(UserProfileModel req) async {
var res = await WPHttpService.to.put(
'/users/me',
data: {
"first_name": req.firstName,
"last_name": req.lastName,
"email": req.email,
},
);
return UserProfileModel.fromJson(res.data);
}

第 2 步:控制器

lib/pages/my/profile_edit/controller.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 保存
Future<void> onSave() async {
if ((formKey.currentState as FormState).validate()) {
// 密码 email 不修改 影响登录

// 提交
UserProfileModel profile = await UserApi.saveBaseInfo(UserProfileModel(
firstName: firstNameController.text,
lastName: lastNameController.text,
// email: emailController.text,
));

// 更新本地
UserService.to.setProfile(profile);
Loading.success();
update(["profile_edit"]);
}
}

第 3 步:视图

lib/pages/my/profile_edit/view.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//  profile 表单
Widget _buildProfileForm() {
return <Widget>[
// first name
TextFormWidget(
controller: controller.firstNameController,
labelText: LocaleKeys.profileEditFirstName.tr,
validator: Validatorless.multiple([
Validatorless.required("The field is obligatory"),
Validatorless.min(
3, "Length cannot be less than @size".trParams({"size": "3"})),
Validatorless.max(18,
"Length cannot be greater than @size".trParams({"size": "18"})),
]),
),

// last name
TextFormWidget(
controller: controller.lastNameController,
labelText: LocaleKeys.profileEditLastName.tr,
validator: Validatorless.multiple([
Validatorless.required("The field is obligatory"),
Validatorless.min(
3, "Length cannot be less than @size".trParams({"size": "3"})),
Validatorless.max(18,
"Length cannot be greater than @size".trParams({"size": "18"})),
]),
),

// Email
TextFormWidget(
keyboardType: TextInputType.emailAddress,
controller: controller.emailController,
labelText: LocaleKeys.profileEditEmail.tr,
validator: Validatorless.multiple([
Validatorless.required("The field is obligatory"),
Validatorless.email(LocaleKeys.validatorEmail.tr),
]),
),
// end
].toColumn().paddingAll(AppSpace.card).card().paddingBottom(AppSpace.card);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//  password 表单
Widget _buildPasswordForm() {
return <Widget>[
// old password
TextFormWidget(
isObscure: true,
keyboardType: TextInputType.visiblePassword,
controller: controller.oldPasswordController,
labelText: LocaleKeys.profileEditOldPassword.tr,
hintText: LocaleKeys.profileEditPasswordTip.tr,
validator: Validatorless.multiple([
Validatorless.min(
3, "Length cannot be less than @size".trParams({"size": "3"})),
Validatorless.max(18,
"Length cannot be greater than @size".trParams({"size": "18"})),
]),
),

// new password
TextFormWidget(
isObscure: true,
keyboardType: TextInputType.visiblePassword,
controller: controller.newPasswordController,
labelText: LocaleKeys.profileEditNewPassword.tr,
hintText: LocaleKeys.profileEditPasswordTip.tr,
validator: Validatorless.multiple([
Validatorless.min(
3, "Length cannot be less than @size".trParams({"size": "3"})),
Validatorless.max(18,
"Length cannot be greater than @size".trParams({"size": "18"})),
]),
),

// confirm password
TextFormWidget(
isObscure: true,
keyboardType: TextInputType.visiblePassword,
controller: controller.confirmNewPasswordController,
labelText: LocaleKeys.profileEditConfirmPassword.tr,
hintText: LocaleKeys.profileEditPasswordTip.tr,
validator: Validatorless.multiple([
Validatorless.min(
3, "Length cannot be less than @size".trParams({"size": "3"})),
Validatorless.max(18,
"Length cannot be greater than @size".trParams({"size": "18"})),
]),
),

// end
].toColumn().paddingAll(AppSpace.card).card();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 主视图
Widget _buildView() {
return SingleChildScrollView(
child: <Widget>[
...

// 保存按钮
ButtonWidget.primary(
LocaleKeys.commonBottomSave.tr,
onTap: controller.onSave, // 保存 tap 事件
height: AppSpace.buttonHeight,
).paddingHorizontal(AppSpace.page),
].toColumn().padding(
top: 45.h,
),
);
}

提交代码到 git