Files
elcaribe/source_code/lib/screens/profile.dart
2023-08-07 15:52:04 -04:00

436 lines
15 KiB
Dart

import 'package:active_ecommerce_seller_app/custom/buttons.dart';
import 'package:active_ecommerce_seller_app/custom/input_decorations.dart';
import 'package:active_ecommerce_seller_app/custom/localization.dart';
import 'package:active_ecommerce_seller_app/custom/my_app_bar.dart';
import 'package:active_ecommerce_seller_app/custom/my_widget.dart';
import 'package:active_ecommerce_seller_app/custom/toast_component.dart';
import 'package:active_ecommerce_seller_app/helpers/file_helper.dart';
import 'package:active_ecommerce_seller_app/helpers/shared_value_helper.dart';
import 'package:active_ecommerce_seller_app/my_theme.dart';
import 'package:active_ecommerce_seller_app/repositories/profile_repository.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class ProfileEdit extends StatefulWidget {
const ProfileEdit({Key? key}) : super(key: key);
@override
_ProfileEditState createState() => _ProfileEditState();
}
class _ProfileEditState extends State<ProfileEdit> {
ScrollController _mainScrollController = ScrollController();
String? avatar_original="";
TextEditingController _nameController =
TextEditingController(text: "");
TextEditingController _passwordController = TextEditingController();
TextEditingController _passwordConfirmController = TextEditingController();
//for image uploading
final ImagePicker _picker = ImagePicker();
XFile? _file;
sellerInfo()async{
var response = await ProfileRepository().getSellerInfo();
_nameController.text = response.name.toString();
avatar_original = response.avatarOriginal;
setState(() {
});
}
chooseAndUploadImage(context) async {
var status = await Permission.photos.request();
if (status.isDenied) {
// We didn't ask for permission yet.
showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: Text(LangText(context: context).getLocal()!.photo_permission_ucf),
content: Text(
AppLocalizations.of(context)!.this_app_needs_permission),
actions: <Widget>[
CupertinoDialogAction(
child: Text(LangText(context: context).getLocal()!.deny_ucf),
onPressed: () => Navigator.of(context).pop(),
),
CupertinoDialogAction(
child: Text(LangText(context: context).getLocal()!.settings_ucf),
onPressed: () => openAppSettings(),
),
],
));
} else if (status.isRestricted) {
ToastComponent.showDialog(
LangText(context: context).getLocal()!.go_to_your_application_settings_and_give_photo_permission,
gravity: Toast.center,
duration: Toast.lengthLong);
} else if (status.isGranted) {
//file = await ImagePicker.pickImage(source: ImageSource.camera);
_file = await _picker.pickImage(source: ImageSource.gallery);
if (_file == null) {
ToastComponent.showDialog(AppLocalizations.of(context)!.no_file_is_chosen,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
//return;
String base64Image = FileHelper.getBase64FormateFile(_file!.path);
String fileName = _file!.path.split("/").last;
var profileImageUpdateResponse =
await ProfileRepository().getProfileImageUpdateResponse(
base64Image,
fileName,
);
if (profileImageUpdateResponse.result == false) {
ToastComponent.showDialog(profileImageUpdateResponse.message!,
gravity: Toast.center, duration: Toast.lengthLong);
return;
} else {
ToastComponent.showDialog(profileImageUpdateResponse.message!,
gravity: Toast.center, duration: Toast.lengthLong);
avatar_original = profileImageUpdateResponse.path;
setState(() {});
}
}
}
Future<void> _onPageRefresh() async {}
onPressUpdate() async {
var name = _nameController.text.toString();
var password = _passwordController.text.toString();
var password_confirm = _passwordConfirmController.text.toString();
var change_password = password != "" ||
password_confirm !=
""; // if both fields are empty we will not change user's password
if (name == "") {
ToastComponent.showDialog(AppLocalizations.of(context)!.enter_your_name,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
if (change_password && password == "") {
ToastComponent.showDialog(AppLocalizations.of(context)!.enter_password,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
if (change_password && password_confirm == "") {
ToastComponent.showDialog(AppLocalizations.of(context)!.confirm_your_password,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
if (change_password && password.length < 6) {
ToastComponent.showDialog(
AppLocalizations.of(context)!.password_must_contain_at_least_6_characters,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
if (change_password && password != password_confirm) {
ToastComponent.showDialog(AppLocalizations.of(context)!.passwords_do_not_match,
gravity: Toast.center, duration: Toast.lengthLong);
return;
}
var profileUpdateResponse =
await ProfileRepository().getProfileUpdateResponse(
name,
change_password ? password : "",
);
if (profileUpdateResponse.result == false) {
ToastComponent.showDialog(profileUpdateResponse.message!,
gravity: Toast.center, duration: Toast.lengthLong);
} else {
ToastComponent.showDialog(profileUpdateResponse.message!,
gravity: Toast.center, duration: Toast.lengthLong);
setState(() {});
}
}
@override
void initState() {
sellerInfo();
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: app_language_rtl.$! ? TextDirection.rtl : TextDirection.ltr,
child: Scaffold(
backgroundColor: Colors.white,
appBar:MyAppBar(context: context,title:AppLocalizations.of(context)!.edit_profile_ucf, ).show(),
body: buildBody(context),
),
);
}
AppBar buildAppBar(BuildContext context) {
return AppBar(
backgroundColor: Colors.white,
centerTitle: true,
leading: Builder(
builder: (context) => IconButton(
icon: Icon(Icons.arrow_back, color: MyTheme.dark_grey),
onPressed: () => Navigator.of(context).pop(),
),
),
title: Text(
AppLocalizations.of(context)!.edit_profile_ucf,
style: TextStyle(fontSize: 16, color: MyTheme.app_accent_color),
),
elevation: 0.0,
titleSpacing: 0,
);
}
buildBody(context) {
if (!is_logged_in.$) {
return Container(
height: 100,
child: Center(
child: Text(
AppLocalizations.of(context)!.please_log_in_to_see_the_profile,
style: TextStyle(color: MyTheme.font_grey),
)));
} else {
return RefreshIndicator(
color: MyTheme.app_accent_color,
backgroundColor: Colors.white,
onRefresh: _onPageRefresh,
displacement: 10,
child: CustomScrollView(
controller: _mainScrollController,
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
slivers: [
SliverList(
delegate: SliverChildListDelegate([
buildTopSection(),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Divider(
height: 24,
),
),
buildProfileForm(context)
]),
)
],
),
);
}
}
buildTopSection() {
return Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 8.0),
child: Stack(
children: [
MyWidget.roundImageWithPlaceholder(height: 120.0,width: 120.0,borderRadius: 60,url: avatar_original),
/* Container(
width: 120,
height: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
border: Border.all(
color: Color.fromRGBO(112, 112, 112, .3), width: 2),
//shape: BoxShape.rectangle,
),
child: ClipRRect(
clipBehavior: Clip.hardEdge,
borderRadius: BorderRadius.all(Radius.circular(100.0)),
child: FadeInImage.assetNetwork(
placeholder: 'assets/placeholder.png',
image: "${avatar_original.$}",
fit: BoxFit.fill,
)),
),*/
Positioned(
right: 8,
bottom: 8,
child: SizedBox(
width: 24,
height: 24,
child: Buttons(
padding: EdgeInsets.all(0),
child: Icon(
Icons.edit,
color: MyTheme.font_grey,
size: 14,
),
shape: CircleBorder(
side:
new BorderSide(color: MyTheme.light_grey, width: 1.0),
),
color: MyTheme.light_grey,
onPressed: () {
chooseAndUploadImage(context);
},
),
),
)
],
),
),
],
);
}
buildProfileForm(context) {
return Padding(
padding:
const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 16.0, right: 16.0),
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
AppLocalizations.of(context)!.basic_information_ucf,
style: TextStyle(
color: MyTheme.grey_153,
fontWeight: FontWeight.w600,
fontSize: 14.0),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: Text(
AppLocalizations.of(context)!.name_ucf,
style: TextStyle(
color: MyTheme.app_accent_color, fontWeight: FontWeight.w600),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
height: 36,
child: TextField(
controller: _nameController,
autofocus: false,
decoration: InputDecorations.buildInputDecoration_1(
hint_text: "John Doe"),
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: Text(
AppLocalizations.of(context)!.password_ucf,
style: TextStyle(
color: MyTheme.app_accent_color, fontWeight: FontWeight.w600),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
height: 36,
child: TextField(
controller: _passwordController,
autofocus: false,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
decoration: InputDecorations.buildInputDecoration_1(
hint_text: "• • • • • • • •"),
),
),
Text(
AppLocalizations.of(context)!.password_must_contain_at_least_6_characters,
style: TextStyle(
color: MyTheme.textfield_grey,
fontStyle: FontStyle.italic),
)
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: Text(
AppLocalizations.of(context)!.retype_password_ucf,
style: TextStyle(
color: MyTheme.app_accent_color, fontWeight: FontWeight.w600),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
height: 36,
child: TextField(
controller: _passwordConfirmController,
autofocus: false,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
decoration: InputDecorations.buildInputDecoration_1(
hint_text: "• • • • • • • •"),
),
),
),
Row(
children: [
Spacer(),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Container(
width: 120,
height: 36,
decoration: BoxDecoration(
border:
Border.all(color: MyTheme.textfield_grey, width: 1),
borderRadius:
const BorderRadius.all(Radius.circular(8.0))),
child: Buttons(
width: MediaQuery.of(context).size.width,
//height: 50,
color: MyTheme.app_accent_color,
shape: RoundedRectangleBorder(
borderRadius:
const BorderRadius.all(Radius.circular(8.0))),
child: Text(
AppLocalizations.of(context)!.update_profile_ucf,
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w600),
),
onPressed: () {
onPressUpdate();
},
),
),
),
],
),
],
),
),
);
}
}