Created
October 3, 2020 16:27
-
-
Save mulieriq/5675b7b083e2f74bf75dde0a9dfb261d to your computer and use it in GitHub Desktop.
fdfdfd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:country_code_picker/country_code_picker.dart'; | |
import 'package:device_info/device_info.dart'; | |
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'package:firebase_storage/firebase_storage.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_image_compress/flutter_image_compress.dart'; | |
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | |
import 'package:image_cropper/image_cropper.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
import 'package:scoped_model/scoped_model.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'package:toast/toast.dart'; | |
import '../../controllers/core/scoped.dart'; | |
import '../../routes/app.dart'; | |
import 'login.dart'; | |
class SetProfile extends StatefulWidget { | |
final String userEmail; | |
final String uid; | |
final String photoUrl; | |
final String userName; | |
SetProfile({this.uid, this.photoUrl, this.userName, this.userEmail}); | |
@override | |
_SetProfileState createState() => _SetProfileState(); | |
} | |
class _SetProfileState extends State<SetProfile> { | |
File img; | |
bool posting = false; | |
String imgUrlDownload; | |
bool upload = false; | |
String username; | |
String phoneNumber; | |
String collage; | |
String year; | |
String family; | |
String status; | |
String birthday; | |
bool compressing = false; | |
String tokens; | |
String countrycode = "+254"; | |
String countryname = "Kenya"; | |
String yearFellowship = "Select Family"; | |
final List<String> families = []; | |
final List<String> years = ["1", "2", "3", "4", "5", "6"]; | |
String currentyear = "1"; | |
//String error = " "; | |
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); | |
registerTokens() { | |
_firebaseMessaging.getToken().then((r) { | |
setState(() { | |
tokens = r; | |
}); | |
}); | |
} | |
Future<void> addData(data) async { | |
//print('function add data called'); | |
Firestore.instance | |
.collection('users') | |
.document(widget.uid) | |
.setData(data) | |
.then((result) { | |
print("data added ..................................... "); | |
// print(result); | |
}).catchError((e) { | |
print(e); | |
}); | |
} | |
Future uploadImage() async { | |
print("function upload image called"); | |
final StorageReference firebaseStorageRef = FirebaseStorage.instance | |
.ref() | |
.child( | |
"users/$yearFellowship/images/${widget.userName.split(' ')[0] + DateTime.now().toString()}.jpg"); | |
print("entering task mode........................"); | |
StorageUploadTask task = firebaseStorageRef.putFile(img); | |
print( | |
"leaving task moode ..................................................................."); | |
var imgdUrl = await (await task.onComplete).ref.getDownloadURL(); | |
String imgUrl = imgdUrl.toString(); //.replaceAll(".jpg", "_200x200.jpg"); | |
print("assing image url adress...................."); | |
setState(() { | |
print(imgUrlDownload); | |
imgUrlDownload = imgUrl; | |
}); | |
} | |
Future getImageGallery() async { | |
var image = await ImagePicker.pickImage(source: ImageSource.gallery); | |
File croppedFile = await ImageCropper.cropImage( | |
sourcePath: image.path, | |
ratioX: 1.0, | |
ratioY: 1.0, | |
maxWidth: 512, | |
maxHeight: 512, | |
); | |
var result = await FlutterImageCompress.compressAndGetFile( | |
croppedFile.path, | |
croppedFile.path, | |
quality: 25, | |
); | |
setState(() { | |
img = result; | |
print(img.lengthSync()); | |
}); | |
} | |
getFamilies() async { | |
return await Firestore.instance | |
.collection("families") | |
.getDocuments() | |
.then((results) { | |
results.documents.forEach((data) { | |
families.add(data.data["family_name"]); | |
print(families); | |
print(data.data); | |
}); | |
print(results.documents); | |
}); | |
} | |
phoneSanitizer<bool>({phoneNumber1}) { | |
String patttern = r'(^(?:[+0]9)?[0-9]{9}$)'; | |
RegExp regExp = new RegExp(patttern); | |
if (!regExp.hasMatch(phoneNumber1.trim())) { | |
return false; | |
} | |
return true; | |
} | |
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin(); | |
AndroidDeviceInfo androidInfo; | |
Future getdeviceInfo() async { | |
setState(() async { | |
androidInfo = await deviceInfoPlugin.androidInfo; | |
}); | |
} | |
var _formKey = GlobalKey<FormState>(); | |
@override | |
void initState() { | |
registerTokens(); | |
super.initState(); | |
getFamilies(); | |
getdeviceInfo(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ScopedModelDescendant( | |
builder: (BuildContext context, Widget child, Scoped model) { | |
return WillPopScope( | |
onWillPop: () { | |
if (Navigator.canPop(context)) { | |
//Navigator.pop(context); | |
model.signOut(context); | |
// Navigator.of(context).pop(); | |
// showToast("Can Poped", Colors.green); | |
} else { | |
// showToast( | |
// "Has fsjlj cjazfhcjc hjhcsajhcj ac jashf sajc jsafhcjzfhcjsafhj poped", | |
// Colors.red); | |
model.signOut(context); | |
// SystemNavigator.pop(); | |
} | |
}, | |
child: Scaffold( | |
appBar: AppBar( | |
elevation: 0.5, | |
title: Text("Register"), | |
centerTitle: true, | |
), | |
body: ListView( | |
children: <Widget>[ | |
// Container( | |
// child: error.isEmpty | |
// ? Text(error, style: TextStyle(color: Colors.red)) | |
// : Container()), | |
Stack( | |
children: <Widget>[ | |
Container( | |
padding: EdgeInsets.only(top: 10), | |
child: InkWell( | |
onTap: () { | |
return getImageGallery(); | |
}, | |
child: Center( | |
child: img == null | |
? new CircleAvatar( | |
backgroundColor: Colors.blue, | |
child: Icon( | |
Icons.person, | |
color: Colors.white, | |
size: | |
MediaQuery.of(context).size.height * 0.1, | |
), | |
radius: | |
MediaQuery.of(context).size.width * 0.25, | |
) | |
: new CircleAvatar( | |
backgroundImage: new FileImage(img), | |
radius: | |
MediaQuery.of(context).size.width * 0.25, | |
), | |
), | |
), | |
), | |
], | |
), | |
Form( | |
key: _formKey, | |
child: Container( | |
padding: EdgeInsets.all(15), | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
enabled: false, | |
decoration: InputDecoration( | |
icon: Icon( | |
Icons.person, | |
color: Colors.blue, | |
), | |
labelText: '${widget.userName}', | |
), | |
), | |
Container( | |
height: MediaQuery.of(context).size.height * .08, | |
margin: EdgeInsets.only(bottom: 10), | |
padding: EdgeInsets.only( | |
// left: 30, | |
// right: 30, | |
), | |
child: FormField<String>( | |
builder: (FormFieldState<String> state) { | |
return InputDecorator( | |
decoration: InputDecoration( | |
icon: Icon( | |
FontAwesomeIcons.user, | |
color: Colors.blue, | |
), | |
// labelStyle: , | |
errorStyle: TextStyle( | |
color: Colors.redAccent, fontSize: 16.0), | |
//hintText: 'Please select expense', | |
// border: OutlineInputBorder( | |
// borderRadius: BorderRadius.circular(5.0)) | |
), | |
isEmpty: true, //_currentSelectedValue == '', | |
child: DropdownButtonHideUnderline( | |
child: DropdownButton( | |
iconDisabledColor: Colors.red, | |
iconEnabledColor: Colors.green, | |
iconSize: 25, | |
items: families | |
.map((value) => DropdownMenuItem( | |
child: Text(value), | |
value: value, | |
)) | |
.toList(), | |
onChanged: (String value) { | |
setState(() { | |
yearFellowship = value; | |
}); | |
}, | |
isExpanded: false, | |
hint: Text(yearFellowship), | |
), | |
), | |
); | |
}, | |
), | |
), | |
Container( | |
//padding: EdgeInsets.only( | |
// right: MediaQuery.of(context).size.width * .04, | |
// left: MediaQuery.of(context).size.width * .03, | |
// top: 10), | |
child: TextFormField( | |
maxLength: 9, | |
keyboardType: TextInputType.phone, | |
onChanged: (value) { | |
phoneNumber = value; | |
}, | |
validator: (String input) { | |
if (input.isEmpty) { | |
return "Phone Number Is Empty"; | |
} | |
}, | |
decoration: InputDecoration( | |
icon: Icon( | |
Icons.phone, | |
color: Colors.blue, | |
), | |
prefixIcon: new CountryCodePicker( | |
padding: EdgeInsets.only( | |
left: MediaQuery.of(context).size.width * .04, | |
right: | |
MediaQuery.of(context).size.width * .005, | |
), | |
onChanged: (code) { | |
print(code.dialCode); | |
setState(() { | |
countryname = code.name; | |
countrycode = code.dialCode; | |
}); | |
}, | |
// Initial selection and favorite can be one of code ('IT') OR dial_code('+39') | |
initialSelection: 'KE', | |
favorite: [ | |
'+254', | |
'KE', | |
], | |
// optional. Shows only country name and flag | |
showCountryOnly: false, | |
showFlag: true, | |
showOnlyCountryWhenClosed: false, | |
// optional. Shows only country name and flag when popup is closed. | |
//showOnlyCountryCodeWhenClosed: false, | |
// optional. aligns the flag and the Text left | |
alignLeft: false, | |
), | |
// enabledBorder: OutlineInputBorder( | |
// // borderRadius: BorderRadius.all( | |
// // Radius.circular(40.0)), | |
// borderSide: BorderSide( | |
// color: Colors.blue, | |
// ), | |
// ), | |
// prefixIcon: Icon( | |
// Icons.person, | |
// color: Colors.blue, | |
// ), | |
// suffixIcon: Icon( | |
// Icons.phone, | |
// color: Colors.blue, | |
// ), | |
hintText: "Phone Number i.e 791 ******", | |
hintStyle: TextStyle( | |
color: Colors.black26, | |
), | |
filled: true, | |
fillColor: Colors.white, | |
// border: OutlineInputBorder( | |
// borderSide: BorderSide( | |
// color: Colors.blue, | |
// ), | |
// borderRadius: BorderRadius.all( | |
// Radius.circular(40.0)), | |
// ), | |
contentPadding: EdgeInsets.symmetric( | |
horizontal: 20.0, vertical: 16.0)), | |
), | |
), | |
// TextFormField( | |
// keyboardType: TextInputType.text, | |
// onChanged: (value) { | |
// phoneNumber = value; | |
// }, | |
// decoration: InputDecoration( | |
// prefixIcon: Icon(Icons.phone), | |
// labelText: 'PhoneNumber', | |
// hintText: '07*****95'), | |
// validator: (String input) { | |
// if (input.isEmpty) { | |
// return "Please Enter PhoneNumber"; | |
// } | |
// }), | |
yearFellowship == "Associates" | |
? Container() | |
: TextFormField( | |
keyboardType: TextInputType.text, | |
onChanged: (value) { | |
collage = value; | |
}, | |
decoration: InputDecoration( | |
icon: Icon( | |
Icons.school, | |
color: Colors.blue, | |
), | |
labelText: 'Collage', | |
hintText: 'Humanities'), | |
validator: (String input) { | |
if (input.isEmpty) { | |
return "Please Enter Collage"; | |
} | |
}), | |
yearFellowship == "Associates" | |
? Container() | |
: ListTile( | |
contentPadding: EdgeInsets.all(0), | |
leading: Icon( | |
Icons.calendar_today, | |
color: Colors.blue, | |
), | |
title: Text( | |
'Year', | |
style: TextStyle(color: Colors.grey), | |
), | |
trailing: DropdownButton( | |
// underline:, | |
underline: Divider( | |
height: 0, | |
color: Colors.white, | |
), | |
items: years | |
.map((value) => DropdownMenuItem( | |
child: Text( | |
value, | |
style: | |
TextStyle(color: Colors.blue), | |
), | |
value: value, | |
)) | |
.toList(), | |
onChanged: (String value) { | |
setState(() { | |
currentyear = value; | |
}); | |
}, | |
isExpanded: false, | |
hint: Text( | |
currentyear, | |
style: TextStyle(color: Colors.blue), | |
), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Container( | |
child: upload | |
? CupertinoActivityIndicator() | |
: RaisedButton( | |
onPressed: () { | |
var phoneSanitizer = this.phoneSanitizer( | |
phoneNumber1: "$phoneNumber"); | |
if (img == null) { | |
return showToast( | |
"Kindly Provide An Image", | |
Colors.red); | |
} else { | |
if (_formKey.currentState.validate()) { | |
if (yearFellowship == "Select Family") { | |
showToast( | |
"Please Provide Your Fellowship", | |
Colors.red); | |
} else { | |
if (phoneSanitizer) { | |
setState(() { | |
upload = true; | |
if (img != null) { | |
uploadImage() | |
.then((result) async { | |
print("uploded image"); | |
print(imgUrlDownload); | |
SharedPreferences | |
autoAuthToken = | |
await SharedPreferences | |
.getInstance(); | |
autoAuthToken.setString( | |
'emailToken', | |
widget.userEmail); | |
autoAuthToken.setString( | |
"status", | |
"Hey there,I am using MCCU Families app"); | |
autoAuthToken.setString( | |
'username', | |
widget.userName); | |
autoAuthToken.setString( | |
'role', 'Member'); | |
autoAuthToken.setString( | |
'uid', widget.uid); | |
autoAuthToken.setString( | |
'year', | |
yearFellowship == | |
"Associates" | |
? "Cleared School" | |
: currentyear, | |
); | |
autoAuthToken.setString( | |
'userImage', | |
imgUrlDownload); | |
autoAuthToken.setString( | |
'birthday', birthday); | |
autoAuthToken.setString( | |
'collage', | |
yearFellowship == | |
"Associates" | |
? "Cleared School" | |
: currentyear, | |
); | |
autoAuthToken.setString( | |
'phoneNumber', | |
"$countrycode$phoneNumber", | |
); | |
autoAuthToken.setString( | |
'family', | |
yearFellowship, | |
); | |
if (imgUrlDownload != null) { | |
addData({ | |
'status': | |
"Hey there,I am using MCCU Families app", | |
'userPhoneNumber': | |
"$countrycode$phoneNumber", | |
'userId': widget.uid, | |
'userEmail': | |
widget.userEmail, | |
'userName': | |
widget.userName, | |
'userImage': | |
imgUrlDownload, | |
'role': "member", | |
'family': yearFellowship | |
.toLowerCase(), | |
"isleader": false, | |
'userBio': { | |
'family': | |
yearFellowship, | |
'year': yearFellowship == | |
"Associates" | |
? "Cleared School" | |
: currentyear, | |
'birthday': birthday, | |
'collage': | |
yearFellowship == | |
"Associates" | |
? "Cleared School" | |
: collage, | |
}, | |
'verified': { | |
'verification': true, | |
'message': '', | |
'logout': false | |
}, | |
'deviceInfo': { | |
'userName': | |
widget.userName, | |
'userId': widget.uid, | |
'androidId': androidInfo | |
.androidId, | |
'board': | |
androidInfo.board, | |
'bootloader': | |
androidInfo | |
.bootloader, | |
'brand': | |
androidInfo.brand, | |
'device': | |
androidInfo.device, | |
'display': | |
androidInfo.display, | |
'fingerprint': | |
androidInfo | |
.fingerprint, | |
'hardware': androidInfo | |
.hardware, | |
'hashCode': androidInfo | |
.hashCode, | |
'host': | |
androidInfo.host, | |
'id': androidInfo.id, | |
'isPhysicalDevice': | |
androidInfo | |
.isPhysicalDevice, | |
'manufacturer': | |
androidInfo | |
.manufacturer, | |
'mode': | |
androidInfo.model, | |
'product': | |
androidInfo.product, | |
}, | |
'pushToken': tokens, | |
}).then((result) { | |
print( | |
"data was added success fully"); | |
model.setdeviceInfo( | |
userName: | |
widget.userName, | |
userid: widget.uid); | |
model.role = "Member"; | |
model.status = status; | |
Navigator.of(context) | |
.pushReplacement( | |
MaterialPageRoute( | |
builder: | |
(BuildContext | |
context) => | |
App( | |
uid: | |
widget.uid, | |
phonenumber: | |
"$countrycode$phoneNumber", | |
email: | |
widget.userEmail, | |
status: | |
"Hey there,I am using MCCU Families app", | |
username: | |
widget.userName, | |
userImage: | |
imgUrlDownload, | |
family: | |
yearFellowship, | |
role: | |
"Member", | |
year: yearFellowship == "Associates" | |
? "Cleared School" | |
: currentyear, | |
birthday: | |
birthday, | |
collage: | |
collage, | |
))); | |
}).catchError((e) { | |
print(e); | |
}); | |
} else { | |
setState(() { | |
upload = false; | |
}); | |
return Toast.show( | |
"Didnt get img Url", | |
context); | |
//print('didnt get img url'); | |
} | |
}); | |
} else { | |
return showDialog( | |
context: context, | |
builder: | |
(BuildContext context) { | |
return AlertDialog( | |
title: Text( | |
'No Image Added'), | |
content: Text( | |
"Please Select Your Profile Image"), | |
actions: <Widget>[ | |
Center( | |
child: RaisedButton( | |
child: Text( | |
"Got It"), | |
onPressed: () => | |
Navigator.pop( | |
context), | |
), | |
) | |
], | |
); | |
}); | |
} | |
}); | |
} else { | |
showToast("Invalid Phone Number", | |
Colors.red); | |
} | |
} | |
} | |
} | |
}, | |
child: new Text( | |
'Upload', | |
style: TextStyle(color: Colors.white), | |
), | |
shape: StadiumBorder(), | |
color: Colors.blue), | |
), | |
), | |
], | |
), | |
), | |
) | |
], | |
), | |
), | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment