Files
sfm_app_final/lib/feature/settings/profile/profile_screen.dart
2025-03-31 10:42:42 +07:00

441 lines
20 KiB
Dart

// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import '../../../product/shared/shared_snack_bar.dart';
import '../../../product/constant/icon/icon_constants.dart';
import '../../../product/services/api_services.dart';
import '../../../bloc/settings_bloc.dart';
import '../../../product/shared/shared_input_decoration.dart';
import '../../../product/extension/context_extension.dart';
import '../../../product/services/language_services.dart';
import 'profile_model.dart';
changeUserInfomation(
BuildContext context, User user, SettingsBloc settingsBloc) {
final formKey = GlobalKey<FormState>();
String username = user.name ?? "";
String email = user.email ?? "";
String tel = user.phone ?? "";
String address = user.address ?? "";
bool isChange = false;
APIServices apiServices = APIServices();
showModalBottomSheet(
isScrollControlled: true,
useSafeArea: true,
context: context,
builder: (modalBottomSheetContext) {
return StreamBuilder<bool>(
stream: settingsBloc.streamIsChangeProfileInfomation,
initialData: isChange,
builder: (context, isChangeSnapshot) {
return Scaffold(
appBar: AppBar(
title: Text(appLocalization(context).change_profile_title),
centerTitle: true,
actions: [
isChangeSnapshot.data ?? isChange
? IconButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
String latitude = user.latitude ?? "";
String longitude = user.longitude ?? "";
Map<String, dynamic> body = {
"name": username,
"email": email,
"phone": tel,
"address": address,
"latitude": latitude,
"longitude": longitude
};
int statusCode =
await apiServices.updateUserProfile(body);
if (statusCode == 200) {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_profile_success,
Colors.green,
Colors.white);
} else {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_profile_failed,
Colors.redAccent,
Colors.white);
}
Navigator.pop(modalBottomSheetContext);
}
},
icon:
IconConstants.instance.getMaterialIcon(Icons.check),
)
: const SizedBox.shrink()
],
),
body: SingleChildScrollView(
child: Form(
key: formKey,
child: Padding(
padding: context.paddingLow,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
appLocalization(context).change_profile_username,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: username,
textInputAction: TextInputAction.next,
validator: (usernameValue) {
if (usernameValue == "null" ||
usernameValue!.isEmpty) {
return appLocalization(modalBottomSheetContext)
.login_account_not_empty;
}
return null;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
onSaved: (newUsername) {
username = newUsername!;
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_username_hint),
),
),
Text(
appLocalization(context).change_profile_email,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: email,
textInputAction: TextInputAction.next,
validator: (emailValue) {
if (emailValue == "null" || emailValue!.isEmpty) {
return appLocalization(modalBottomSheetContext)
.change_profile_email_not_empty;
}
return null;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
onSaved: (newEmail) {
email = newEmail!;
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_email_hint),
),
),
Text(
appLocalization(context).change_profile_tel,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: tel,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.phone,
validator: (telValue) {
if (telValue == "null" || telValue!.isEmpty) {
return appLocalization(modalBottomSheetContext)
.change_profile_tel_not_empty;
}
return null;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
onSaved: (newTel) {
tel = newTel!;
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_tel_hint),
),
),
Text(
appLocalization(context).change_profile_address,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: address,
textInputAction: TextInputAction.done,
onSaved: (newAddress) {
address = newAddress!;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_address_hint),
),
),
SizedBox(height: context.mediumValue),
isChangeSnapshot.data ?? isChange
? Center(
child: TextButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
String latitude = user.latitude ?? "";
String longitude = user.longitude ?? "";
Map<String, dynamic> body = {
"name": username,
"email": email,
"phone": tel,
"address": address,
"latitude": latitude,
"longitude": longitude
};
int statusCode = await apiServices
.updateUserProfile(body);
if (statusCode == 200) {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_profile_success,
Colors.green,
Colors.white);
} else {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_profile_failed,
Colors.redAccent,
Colors.white);
}
Navigator.pop(modalBottomSheetContext);
}
},
style: const ButtonStyle(
backgroundColor:
WidgetStatePropertyAll(Colors.blue),
foregroundColor:
WidgetStatePropertyAll(Colors.white),
),
child: Text(appLocalization(context)
.update_button_content),
),
)
: const SizedBox.shrink(),
],
),
),
),
),
);
},
);
},
);
}
changeUserPassword(BuildContext context, SettingsBloc settingsBloc) {
final formKey = GlobalKey<FormState>();
String oldPass = "";
String newPass = "";
APIServices apiServices = APIServices();
bool isChange = false;
showModalBottomSheet(
isScrollControlled: true,
useSafeArea: true,
context: context,
builder: (modalBottomSheetContext) {
return StreamBuilder<bool>(
stream: settingsBloc.streamIsChangeProfileInfomation,
initialData: isChange,
builder: (context, isChangeSnapshot) {
return Scaffold(
appBar: AppBar(
title: Text(appLocalization(context).change_profile_title),
centerTitle: true,
actions: [
isChangeSnapshot.data ?? isChange
? IconButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
Map<String, dynamic> body = {
"password_old": oldPass,
"password_new": newPass,
};
int statusCode =
await apiServices.updateUserPassword(body);
if (statusCode == 200) {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_password_success,
Colors.green,
Colors.white);
} else {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_password_failed,
Colors.redAccent,
Colors.white);
}
Navigator.pop(modalBottomSheetContext);
}
},
icon:
IconConstants.instance.getMaterialIcon(Icons.check),
)
: const SizedBox.shrink()
],
),
body: SingleChildScrollView(
child: Form(
key: formKey,
child: Padding(
padding: context.paddingLow,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
appLocalization(context).change_profile_old_pass,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: oldPass,
textInputAction: TextInputAction.next,
validator: (oldPassValue) {
if (oldPassValue == "null" ||
oldPassValue!.isEmpty) {
return appLocalization(modalBottomSheetContext)
.change_profile_old_pass_not_empty;
}
return null;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
onSaved: (newOldPass) {
oldPass = newOldPass!;
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_old_pass_hint),
),
),
Text(
appLocalization(context).change_profile_new_pass,
style: context.titleMediumTextStyle,
),
Padding(
padding: context.paddingLowVertical,
child: TextFormField(
initialValue: newPass,
textInputAction: TextInputAction.done,
validator: (newPassValue) {
if (newPassValue == "null" ||
newPassValue!.isEmpty) {
return appLocalization(modalBottomSheetContext)
.change_profile_new_pass_not_empty;
}
return null;
},
onChanged: (value) {
isChange = true;
settingsBloc.sinkIsChangeProfileInfomation
.add(isChange);
},
onSaved: (newnewPass) {
newPass = newnewPass!;
},
decoration: borderRadiusTopLeftAndBottomRight(
modalBottomSheetContext,
appLocalization(modalBottomSheetContext)
.change_profile_new_pass_hint),
),
),
SizedBox(height: context.mediumValue),
isChangeSnapshot.data ?? isChange
? Center(
child: TextButton(
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
Map<String, dynamic> body = {
"password_old": oldPass,
"password_new": newPass,
};
int statusCode = await apiServices
.updateUserPassword(body);
if (statusCode == 200) {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_password_success,
Colors.green,
Colors.white);
} else {
showNoIconTopSnackBar(
modalBottomSheetContext,
appLocalization(context)
.notification_update_password_failed,
Colors.redAccent,
Colors.white);
}
Navigator.pop(modalBottomSheetContext);
}
},
style: const ButtonStyle(
backgroundColor:
WidgetStatePropertyAll(Colors.blue),
foregroundColor:
WidgetStatePropertyAll(Colors.white),
),
child: Text(appLocalization(context)
.update_button_content),
),
)
: const SizedBox.shrink()
],
),
),
),
),
);
},
);
},
);
}