Files

875 lines
45 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/providers/profile_image_provider.dart';
import 'package:initial_folder/providers/theme_provider.dart';
import 'package:initial_folder/providers/update_data_diri_provider.dart';
import 'package:initial_folder/providers/data_diri_provider.dart';
import 'package:initial_folder/services/user_info_service.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/helper/validator.dart';
import 'package:initial_folder/widgets/login_regist/custom_profile_text_field.dart';
import 'package:initial_folder/providers/user_info_provider.dart' as userInfo;
import 'package:provider/provider.dart';
class DataDiri extends StatefulWidget {
@override
State<DataDiri> createState() => _DataDiriState();
}
class _DataDiriState extends State<DataDiri> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final List<String> gender = ['pria', 'wanita'];
void _handleGenderChange(String? value, DataDiriProvider state) {
setState(() {
newGender = value ?? '';
state.newGender = true;
isGenderEmpty = false;
});
}
late String newGender = '';
late String newName;
late String newBirthDate = '';
late String newHeadline;
late String newBiograpy;
late String newEmail;
late String newPhone;
late String newInstagram = '';
late String newTwitter = '';
late String newFacebook = '';
late String newLinkedin = '';
bool? isNameEmpty;
bool? isGenderEmpty;
bool? isBirthDateEmpty;
bool? isEmailEmpty;
bool? isPhoneEmpty;
DateTime birthDate = DateTime.now();
bool isLoading = false;
String? email = "";
void getUserEmail() async {
email = await UsersInfo().getEmail();
}
@override
Widget build(BuildContext context) {
final themeProvider = Provider.of<ThemeProvider>(context);
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
UpdateDataDiriProvider updateDataDiriProvider =
Provider.of<UpdateDataDiriProvider>(context);
ProfileImageProvider profileImageProvider =
Provider.of<ProfileImageProvider>(context, listen: false);
final ImagePicker _picker = ImagePicker();
Future _showMessage(String text) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
backgroundColor: Colors.white,
surfaceTintColor: Colors.transparent,
contentPadding: EdgeInsets.fromLTRB(22, 30, 22, 30),
content: Text(
text,
textAlign: TextAlign.center,
style: thirdTextStyle.copyWith(
fontSize: getProportionateScreenWidth(12),
color: baruTexthitam,
),
),
),
);
}
void takePhoto(ImageSource source) async {
try {
final XFile? pickedFile = await _picker.pickImage(source: source);
var imageFile = (pickedFile != null) ? File(pickedFile.path) : File('');
var email = await UsersInfo().getEmail();
if (await profileImageProvider.addProfileImage(pckFile: imageFile)) {
setState(() {
isLoading = true;
});
await Future.delayed(Duration(seconds: 2));
profileImageProvider.setImageFile(imageFile);
setState(() {
imageFile;
isLoading = false;
});
// await Provider.of<userInfo.UserInfoProvider>(context, listen: false)
// .getUserInfo(email);
_showMessage('Berhasil Upload Image');
}
} on PlatformException catch (e) {
print('Failed to pick image : $e');
}
}
Widget bottomSheet() {
return Container(
height: 100.0,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(
horizontal: 20,
vertical: 20,
),
child: Column(
children: <Widget>[
Text(
"Choose Profile photo",
style: secondaryTextStyle.copyWith(fontSize: 20),
),
SizedBox(height: getProportionateScreenHeight(20)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton.icon(
icon: Icon(Icons.camera),
onPressed: () {
Navigator.pop(context);
takePhoto(ImageSource.camera);
},
label: Text(
"Camera",
style: primaryTextStyle,
),
),
TextButton.icon(
icon: Icon(Icons.image),
onPressed: () {
Navigator.pop(context);
takePhoto(ImageSource.gallery);
},
label: Text("Gallery", style: primaryTextStyle),
),
],
),
],
),
);
}
Widget imageProfile(String? urlImage) {
return Stack(
alignment: AlignmentDirectional.center,
children: [
CircleAvatar(
radius: getProportionateScreenWidth(40),
backgroundColor: Colors.amber,
backgroundImage: profileImageProvider.imageFile != null
? FileImage(profileImageProvider.imageFile!) as ImageProvider
: urlImage != null
? NetworkImage(urlImage) as ImageProvider
: AssetImage("assets/images/Profile Image.png")
as ImageProvider,
),
CircleAvatar(
radius: getProportionateScreenWidth(40),
backgroundColor: Color(0xff000000).withOpacity(0.5),
),
IconButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.pop(context);
},
child: bottomSheet(),
),
);
},
icon: Icon(
Icons.camera_alt_outlined,
color: Colors.white,
),
)
],
);
}
return SafeArea(
child: ChangeNotifierProvider(
create: (context) =>
DataDiriProvider(userInfoService: UserInfoService()),
child: Consumer<DataDiriProvider>(
builder: (context, state, _) {
if (state.state == ResultStateData.Loading) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(
color: secondaryColor,
strokeWidth: 2,
),
),
);
} else if (state.state == ResultStateData.HasData) {
var result = state.result!.data[0];
newGender = state.newGender ? newGender : result.gender ?? '';
return Scaffold(
appBar: AppBar(
elevation: 0.0,
backgroundColor: Theme.of(context).colorScheme.background,
centerTitle: true,
title: Text(
'Data Diri',
style: thirdTextStyle.copyWith(
letterSpacing: 0.23,
fontWeight: bold,
fontSize: getProportionateScreenWidth(14)),
),
actions: [
TextButton(
child: isLoading
? CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation(
Colors.white,
),
)
: Text('Simpan',
style: thirdTextStyle.copyWith(
color:themeProvider.themeData == ThemeClass.darkmode
?primaryColor : primaryColorligtmode,
fontWeight: reguler,
letterSpacing: 0.5,
fontSize: getProportionateScreenWidth(12))),
onPressed: () async {
setState(() {
isLoading = true;
});
if (this._formKey.currentState!.validate()) {
if (await updateDataDiriProvider.dataDiriUpdate(
fullname:
state.newName ? newName : result.fullname,
headline: state.newHeadline
? newHeadline
: result.headline,
biograph: state.newBiograpy
? newBiograpy
: result.biography,
twitter: state.newTwitter
? newTwitter
: result.socialLink?.twitter,
facebook: state.newFacebook
? newFacebook
: result.socialLink?.facebook,
linkedin: state.newLinkedin
? newLinkedin
: result.socialLink?.linkedin,
instagram: state.newInstagram
? newInstagram
: result.socialLink?.instagram,
phone: state.newPhone ? newPhone : result.phone,
// datebirth: state.isNewBirthDate
// ? newBirthDate
// : result.datebirth,
// gender:
// state.newGender ? newGender : result.gender,
email:
state.newEmail ? newEmail : result.email)) {
await Provider.of<DataDiriProvider>(context,
listen: false)
.getDataDiri();
_showMessage('Data diri berhasil diubah');
} else {
if (updateDataDiriProvider.updateDataDiriModel !=
null) {
dynamic updateDataDiriMessages =
updateDataDiriProvider
.updateDataDiriModel!.messages;
if (updateDataDiriMessages is! String) {
if (updateDataDiriMessages.fullname
is String) {
setState(() {
isNameEmpty = true;
});
}
if (updateDataDiriMessages.gender is String) {
setState(() {
isGenderEmpty = true;
});
}
if (updateDataDiriMessages.datebirth
is String) {
setState(() {
isBirthDateEmpty = true;
});
}
if (updateDataDiriMessages.phone is String) {
setState(() {
isPhoneEmpty = true;
});
}
if (updateDataDiriMessages.email is String) {
setState(() {
isEmailEmpty = true;
});
}
}
} else {
_showMessage('Silahkan Coba lagi');
}
if (state.newGender) {
if (newGender.isEmpty) {
setState(() {
isGenderEmpty = true;
});
}
} else {
if (result.gender == null) {
setState(() {
isGenderEmpty = true;
});
}
}
if (state.isNewBirthDate) {
if (newBirthDate.isEmpty) {
setState(() {
isBirthDateEmpty = true;
});
}
} else {
if (result.datebirth == null) {
setState(() {
isBirthDateEmpty = true;
});
}
}
}
}
setState(() {
isLoading = false;
});
})
],
),
body: ListView(
children: [
Container(
height: getProportionateScreenHeight(100),
// width: 360,
decoration: BoxDecoration(
color: Colors.grey[300],
image: DecorationImage(
image: AssetImage( isDarkMode
? 'assets/images/cover_dark.png'
: 'assets/images/cover_light.png'),
fit: BoxFit.cover,
),
),
// child: const Padding(
// padding: EdgeInsets.all(10),
// child: Align(
// alignment: Alignment.bottomRight,
// // child: Icon(
// // Icons.camera_alt,
// // color: Colors.white,
// // ),
// ),
// ),
),
Transform.translate(
offset: Offset(0, getProportionateScreenHeight(-45)),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Consumer<userInfo.UserInfoProvider>(
builder: (context, state, _) {
if (state.state ==
userInfo.ResultState.Loading) {
return Center(
child: CircularProgressIndicator(
color: secondaryColor,
strokeWidth: 2,
),
);
} else if (state.state ==
userInfo.ResultState.HasData) {
return Center(
child: imageProfile(
state.result?.data[0].fotoProfile),
);
} else if (state.state ==
userInfo.ResultState.NoData) {
return Center(child: Text(state.message));
} else {
return Center(child: Text(''));
}
}),
),
SizedBox(height: getProportionateScreenHeight(16)),
Form(
key: _formKey,
child: Column(
children: [
CustomProfileTextField(
pad: getProportionateScreenWidth(2),
text: state.newName
? newName
: result.fullname ?? '',
hinttext: 'Tuliskan nama lengkap Anda',
title: 'Nama Lengkap*',
validate: validateName,
onChanged: (value) {
state.newName = true;
newName = value;
},
isErrorManual: isNameEmpty ?? false,
textErrorManual:
"Nama lengkap tidak boleh kosong",
),
CustomProfileTextField(
pad: getProportionateScreenWidth(2),
text: state.newHeadline
? newHeadline
: result.headline ?? '',
hinttext: 'Maksimal 60 kalimat',
title: 'Headline',
onChanged: (value) {
state.newHeadline = true;
newHeadline = value;
},
),
CustomProfileTextField(
pad: getProportionateScreenWidth(2),
text: state.newBiograpy
? newBiograpy
: result.biography ?? '',
hinttext: 'Tulis biografi singkat kamu',
title: 'Biografi',
minLines: 1,
maxLines: 5,
onChanged: (value) {
state.newBiograpy = true;
newBiograpy = value;
},
),
// Column(
// crossAxisAlignment:
// CrossAxisAlignment.start,
// children: [
// Text(
// 'Jenis Kelamin',
// style: thirdTextStyle.copyWith(
// fontWeight: semiBold,
// fontSize:
// getProportionateScreenWidth(
// 12),
// color: isDarkMode
// ? baruTextutih
// : baruTexthitam,
// letterSpacing: 0.5),
// ),
// Row(
// mainAxisAlignment:
// MainAxisAlignment.start,
// children: [
// Radio<String>(
// value: 'pria',
// groupValue: newGender,
// onChanged: (value) {
// _handleGenderChange(
// value, state);
// },
// ),
// Text(
// 'Laki-Laki',
// style: thirdTextStyle.copyWith(
// fontSize:
// getProportionateScreenWidth(
// 12),
// color: isDarkMode
// ? baruTextutih
// : baruTexthitam,
// letterSpacing: 0.5),
// ),
// Radio<String>(
// value: 'wanita',
// groupValue: newGender,
// onChanged: (value) {
// _handleGenderChange(
// value, state);
// },
// ),
// Text(
// 'Perempuan',
// style: thirdTextStyle.copyWith(
// fontSize:
// getProportionateScreenWidth(
// 12),
// color: isDarkMode
// ? baruTextutih
// : baruTexthitam,
// letterSpacing: 0.5),
// ),
// ],
// ),
// customTextValidator(isGenderEmpty,
// 'Jenis Kelamin tidak boleh kosong')
// ],
// ),
// SizedBox(height: 15),
// GestureDetector(
// child: Container(
// margin: EdgeInsets.only(
// bottom:
// getProportionateScreenWidth(20),
// ),
// child: Column(
// crossAxisAlignment:
// CrossAxisAlignment.start,
// children: [
// Text('Tanggal Lahir',
// style: secondaryTextStyle
// .copyWith(
// fontWeight: semiBold,
// fontSize:
// getProportionateScreenWidth(
// 12),
// color: isDarkMode
// ? baruTextutih
// : baruTexthitam,
// letterSpacing: 0.5)),
// SizedBox(
// height:
// getProportionateScreenWidth(
// 5),
// ),
// Container(
// width: double.infinity,
// height:
// getProportionateScreenWidth(
// 50),
// margin:
// EdgeInsets.only(bottom: 7),
// child: Row(
// mainAxisAlignment:
// MainAxisAlignment
// .spaceBetween,
// children: [
// Container(
// margin: EdgeInsets.only(
// left: 13),
// child: Text(
// state.isNewBirthDate
// ? newBirthDate
// : result.datebirth ??
// 'YYYY-MM-DD',
// style: primaryTextStyle
// .copyWith(
// fontSize:
// getProportionateScreenWidth(
// 12),
// color: state
// .isNewBirthDate
// ? baruTexthitam
// : secondaryColor,
// letterSpacing: 0.5,
// ),
// ),
// ),
// Padding(
// padding: EdgeInsets.only(
// right:
// getProportionateScreenWidth(
// 5)),
// child: Icon(Icons
// .keyboard_arrow_down_outlined),
// )
// ],
// ),
// decoration: BoxDecoration(
// color: isDarkMode
// ? Color(0xff212643)
// : Color(0xffF2F2F2),
// borderRadius:
// BorderRadius.circular(10),
// border: Border.all(
// color: isBirthDateEmpty ==
// null
// ? isDarkMode
// ? Color(0xff212643)
// : Color(0xffF2F2F2)
// : isBirthDateEmpty!
// ? Colors.red[700] ??
// Color(
// 0xffd32f2f)
// : isDarkMode
// ? Color(
// 0xff212643)
// : Color(
// 0xffF2F2F2),
// ),
// ),
// ),
// customTextValidator(
// isBirthDateEmpty,
// 'Tanggal lahir tidak boleh kosong'),
// ],
// ),
// ),
// onTap: () async {
// DateTime initialDate = state
// .isNewBirthDate
// ? DateTime(
// int.parse(newBirthDate
// .split('-')[0]),
// int.parse(newBirthDate
// .split('-')[1]),
// int.parse(newBirthDate
// .split('-')[2]))
// : result.datebirth == null
// ? DateTime.now()
// : DateTime(
// int.parse(result
// .datebirth!
// .split('-')[0]),
// int.parse(result
// .datebirth!
// .split('-')[1]),
// int.parse(
// result.datebirth!.split('-')[2]));
// final datePick = await showDatePicker(
// context: context,
// initialDate: initialDate,
// firstDate: DateTime(1900),
// lastDate: DateTime(2100),
// builder: (BuildContext context,
// Widget? child) {
// return Theme(
// data:
// Theme.of(context).copyWith(
// datePickerTheme:
// DatePickerThemeData(
// backgroundColor:
// Colors.white,
// headerBackgroundColor:
// primaryColor,
// headerForegroundColor:
// Colors.white,
// surfaceTintColor:
// Colors
// .transparent),
// colorScheme:
// ColorScheme.light(
// primary:
// primaryColor, // header background color
// onPrimary: Colors
// .white, // header text color
// onSurface:
// primaryColor, // body text color
// ),
// textButtonTheme:
// TextButtonThemeData(
// style: TextButton.styleFrom(
// foregroundColor:
// primaryColor, // button text color
// ),
// ),
// ),
// child: child!,
// );
// },
// );
// if (datePick != null &&
// datePick != birthDate) {
// birthDate = datePick;
// state.isNewBirthDate = true;
// setState(() {
// isBirthDateEmpty = false;
// });
// newBirthDate =
// "${birthDate.year}-${birthDate.month}-${birthDate.day}";
// }
// }),
// CustomProfileTextField(
// keyboardType: TextInputType.number,
// pad: getProportionateScreenWidth(2),
// text: state.newEmail
// ? newEmail
// : result.email ?? '',
// hinttext: 'Tuliskan email Anda',
// title: 'Email',
// validate: validateEmail,
// onChanged: (value) {
// state.newEmail = true;
// newEmail = value;
// },
// isErrorManual: isNameEmpty ?? false,
// textErrorManual:
// "Mohon masukkan email yang valid",
// ),
CustomProfileTextField(
keyboardType: TextInputType.number,
pad: getProportionateScreenWidth(2),
text: state.newPhone
? newPhone
: result.phone ?? '',
hinttext:
'Tuliskan nomor telepon Anda yang terdaftar',
title: 'Nomor Telepon / WA',
validate: validatePhone,
onChanged: (value) {
state.newPhone = true;
newPhone = value;
},
isErrorManual: isNameEmpty ?? false,
textErrorManual:
"Mohon masukkan nomor telepon yang valid",
),
// SizedBox(
// height: getProportionateScreenHeight(20),
// ),
// CustomProfileTextField(
// prefix: Padding(
// padding: EdgeInsets.only(
// top: getProportionateScreenHeight(10),
// left: getProportionateScreenWidth(20),
// right:
// getProportionateScreenHeight(10),
// ),
// child: FaIcon(
// FontAwesomeIcons.instagram,
// color: tenthColor),
// ),
// pad: getProportionateScreenWidth(2),
// text: state.newInstagram
// ? newInstagram
// : result.socialLink?.instagram ?? '',
// hinttext: 'http://instagram.com/',
// noTitle: true,
// onChanged: (value) {
// state.newInstagram = true;
// newInstagram = value;
// },
// ),
// CustomProfileTextField(
// prefix: Padding(
// padding: EdgeInsets.only(
// top: getProportionateScreenHeight(10),
// left: getProportionateScreenWidth(20),
// right:
// getProportionateScreenHeight(10),
// ),
// child: FaIcon(FontAwesomeIcons.twitter,
// color: tenthColor),
// ),
// pad: getProportionateScreenWidth(2),
// text: state.newTwitter
// ? newTwitter
// : result.socialLink?.twitter ?? '',
// hinttext: 'http://twitter.com/',
// noTitle: true,
// onChanged: (value) {
// state.newTwitter = true;
// newTwitter = value;
// },
// ),
// CustomProfileTextField(
// prefix: Padding(
// padding: EdgeInsets.only(
// top: getProportionateScreenHeight(10),
// left: getProportionateScreenWidth(20),
// right:
// getProportionateScreenHeight(10),
// ),
// child: FaIcon(
// FontAwesomeIcons.facebookF,
// color: tenthColor),
// ),
// pad: getProportionateScreenWidth(2),
// text: state.newFacebook
// ? newFacebook
// : result.socialLink?.facebook ?? '',
// hinttext: 'http://facebook.com/',
// noTitle: true,
// onChanged: (value) {
// state.newFacebook = true;
// newFacebook = value;
// },
// ),
// CustomProfileTextField(
// prefix: Padding(
// padding: EdgeInsets.only(
// top: getProportionateScreenHeight(10),
// left: getProportionateScreenWidth(20),
// right:
// getProportionateScreenHeight(10),
// ),
// child: FaIcon(
// FontAwesomeIcons.linkedinIn,
// color: tenthColor),
// ),
// pad: getProportionateScreenWidth(2),
// text: state.newLinkedin
// ? newLinkedin
// : result.socialLink?.linkedin ?? '',
// hinttext: 'http://linkedin.com/',
// noTitle: true,
// onChanged: (value) {
// state.newLinkedin = true;
// newLinkedin = value;
// },
// ),
],
)),
],
),
),
)
],
),
);
} else if (state.state == ResultStateData.NoData) {
return Center(child: Text(state.message));
} else if (state.state == ResultStateData.Error) {
return Center(
child: TextButton(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('Server internal Error ${state.message}'),
Icon(Icons.refresh)
],
),
onPressed: () {}),
);
} else {
return Center(child: Text(''));
}
},
),
),
);
}
}