Created
November 18, 2020 07:39
-
-
Save mulieriq/ff20051e3aab6a61da7cec5021f11be8 to your computer and use it in GitHub Desktop.
Merchant user
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:firebase_storage/firebase_storage.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
//import 'package:flutter_image_compress/flutter_image_compress.dart'; | |
//import 'package:image_cropper/image_cropper.dart'; | |
import 'package:maberr/providers/auth/user.dart'; | |
import 'package:maberr/widgets/custom_text%20copy.dart'; | |
import 'package:provider/provider.dart'; | |
import 'package:toast/toast.dart'; | |
import '../../../constants.dart'; | |
import '../../nav.dart'; | |
class MerchantUser extends StatefulWidget { | |
static const String routeName = '/Merchantuser'; | |
final String userEmail; | |
final String uid; | |
final String profilePhoto; | |
final String userName; | |
MerchantUser({this.uid, this.profilePhoto, this.userName, this.userEmail}); | |
@override | |
_MerchantUserState createState() => _MerchantUserState(); | |
} | |
class _MerchantUserState extends State<MerchantUser> { | |
//String _name; | |
//String _email; | |
//String _password; | |
String _self; | |
String _sell; | |
String _postalCode; | |
String _address; | |
String _shop; | |
String _shopLocation; | |
String _shopUrl; | |
String _birthDay; | |
//String _url; | |
String _phoneNumber; | |
String countrycode = "+254"; | |
String countryname = "Kenya"; | |
File img; | |
//bool posting = false; | |
String imgUrlDownload; | |
DateTime selectedDate = DateTime.now(); | |
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); | |
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/images/profilepictures/${widget.userName}_${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 address...................."); | |
setState(() { | |
print(imgUrlDownload); | |
imgUrlDownload = imgUrl; | |
}); | |
} | |
Future getImageGallery() async { | |
var image = await ImagePicker.pickImage( | |
source: ImageSource.gallery, | |
maxWidth: 512, | |
maxHeight: 512, | |
); | |
setState(() { | |
img = image; | |
print(img.lengthSync()); | |
}); | |
} | |
Future getImageCamera() async { | |
var image = await ImagePicker.pickImage( | |
source: ImageSource.camera, | |
maxWidth: 512, | |
maxHeight: 512, | |
); | |
setState(() { | |
img = image; | |
print(img.lengthSync()); | |
}); | |
} | |
Widget _buildImage() { | |
return Container( | |
padding: EdgeInsets.only(top: 10), | |
child: InkWell( | |
onTap: () { | |
return _userEditBottomSheet(context); | |
}, | |
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, | |
), | |
), | |
), | |
); | |
} | |
Widget _buildName() { | |
final userProvider = Provider.of<UserProvider>(context); | |
return Row( | |
children: [ | |
Text( | |
"Name:", | |
style: TextStyle( | |
fontSize: 18, fontWeight: FontWeight.w500, color: Colors.black), | |
), | |
CustomText( | |
text: userProvider.userModel?.name ?? "username loading...", | |
color: kTextColor1, | |
weight: FontWeight.bold, | |
size: 18, | |
), | |
], | |
); | |
} | |
Widget _buildSelf() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'About Me'), | |
maxLength: 100, | |
onSaved: (String value) { | |
_self = value; | |
}, | |
); | |
} | |
Widget _buildSell() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'What do you intend to sell'), | |
//maxLength: 10, | |
validator: (String value) { | |
if (value.isEmpty) { | |
return 'What do you intend to sell'; | |
} | |
return null; | |
}, | |
onSaved: (String value) { | |
_sell = value; | |
}, | |
); | |
} | |
Widget _buildShop() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'Shop'), | |
maxLength: 10, | |
onSaved: (String value) { | |
_shop = value; | |
}, | |
); | |
} | |
Widget _buildEmail() { | |
final userProvider = Provider.of<UserProvider>(context); | |
return Row( | |
children: [ | |
Text( | |
"Email:", | |
style: TextStyle( | |
fontSize: 18, fontWeight: FontWeight.w500, color: Colors.black), | |
), | |
CustomText( | |
text: userProvider.userModel?.email ?? "email loading...", | |
color: kTextColor1, | |
weight: FontWeight.bold, | |
size: 18, | |
), | |
], | |
); | |
} | |
Widget _buildShopURL() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'Shop Url if website is present'), | |
keyboardType: TextInputType.url, | |
onSaved: (String value) { | |
_shopUrl = value; | |
}, | |
); | |
} | |
Widget _buildBirthday() { | |
return Container( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Text('Date of Birth'), | |
Text( | |
"${selectedDate.toLocal()}".split(' ')[0], | |
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold), | |
), | |
SizedBox( | |
height: 20.0, | |
), | |
RaisedButton( | |
onPressed: () => _selectDate(context), | |
// Refer step 3 | |
child: Text( | |
'Select date', | |
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold), | |
), | |
color: Colors.greenAccent, | |
), | |
], | |
)); | |
} | |
_selectDate(BuildContext context) async { | |
final DateTime picked = await showDatePicker( | |
context: context, | |
initialDate: selectedDate, // Refer step 1 | |
firstDate: DateTime(1920), | |
lastDate: DateTime(2025), | |
); | |
if (picked != null && picked != selectedDate) | |
setState(() { | |
selectedDate = picked; | |
onChanged: | |
(value) { | |
_birthDay = value; | |
}; | |
}); | |
} | |
Widget _buildPhoneNumber() { | |
return SingleChildScrollView( | |
child: 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)), | |
), | |
), | |
); | |
} | |
Widget _merchantContainer() { | |
return Container( | |
decoration: BoxDecoration( | |
color: kPrimaryColor, | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(8.0), | |
bottomLeft: Radius.circular(8.0), | |
bottomRight: Radius.circular(8.0), | |
topRight: Radius.circular(68.0)), | |
boxShadow: <BoxShadow>[ | |
BoxShadow( | |
color: Colors.grey.withOpacity(0.2), | |
offset: Offset(1.1, 1.1), | |
blurRadius: 10.0), | |
], | |
), | |
child: Column(children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(top: 16, left: 16, right: 16), | |
child: Row( | |
children: [ | |
Center( | |
child: Text( | |
'MERCHANT', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 40, | |
letterSpacing: 2, | |
color: kPrimaryLightColor, | |
), | |
), | |
), | |
IconButton( | |
icon: Icon( | |
Icons.monetization_on, | |
color: Colors.white, | |
), | |
onPressed: null) | |
], | |
)), | |
]), | |
/*child: Text( | |
'This is a Container', | |
textScaleFactor: 1, | |
style: TextStyle(color: Colors.black), | |
),*/ | |
); | |
} | |
Widget _contactContainer() { | |
return Container( | |
decoration: BoxDecoration( | |
color: kPrimaryLightColor, | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(8.0), | |
bottomLeft: Radius.circular(8.0), | |
bottomRight: Radius.circular(8.0), | |
topRight: Radius.circular(68.0)), | |
boxShadow: <BoxShadow>[ | |
BoxShadow( | |
color: Colors.grey.withOpacity(0.2), | |
offset: Offset(1.1, 1.1), | |
blurRadius: 10.0), | |
], | |
), | |
child: Column(children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(top: 16, left: 16, right: 16), | |
child: Row( | |
children: [ | |
Center( | |
child: Text( | |
'Contact', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 30, | |
letterSpacing: 2, | |
color: Colors.black, | |
), | |
), | |
), | |
SizedBox( | |
width: 30, | |
), | |
IconButton( | |
icon: Icon( | |
Icons.contact_mail, | |
color: Colors.black, | |
), | |
onPressed: null) | |
], | |
)), | |
_buildName(), | |
SizedBox(height: 10), | |
_buildEmail(), | |
SizedBox(height: 10), | |
_buildPhoneNumber(), | |
]), | |
/*child: Text( | |
'This is a Container', | |
textScaleFactor: 1, | |
style: TextStyle(color: Colors.black), | |
),*/ | |
); | |
} | |
Widget _addressContainer() { | |
return Container( | |
decoration: BoxDecoration( | |
color: kPrimaryLightColor, | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(8.0), | |
bottomLeft: Radius.circular(8.0), | |
bottomRight: Radius.circular(8.0), | |
topRight: Radius.circular(68.0)), | |
boxShadow: <BoxShadow>[ | |
BoxShadow( | |
color: Colors.grey.withOpacity(0.2), | |
offset: Offset(1.1, 1.1), | |
blurRadius: 10.0), | |
], | |
), | |
child: Column(children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(top: 16, left: 16, right: 16), | |
child: Row( | |
children: [ | |
Center( | |
child: Text( | |
'Address', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 30, | |
letterSpacing: 2, | |
color: Colors.black, | |
), | |
), | |
), | |
SizedBox( | |
width: 30, | |
), | |
IconButton( | |
icon: Icon( | |
Icons.local_convenience_store, | |
color: Colors.black, | |
), | |
onPressed: null) | |
], | |
)), | |
_buildPostalcode(), | |
_buildAddress(), | |
]), | |
/*child: Text( | |
'This is a Container', | |
textScaleFactor: 1, | |
style: TextStyle(color: Colors.black), | |
),*/ | |
); | |
} | |
Widget _buildPostalcode() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'Postal code'), | |
maxLength: 100, | |
onSaved: (String value) { | |
_postalCode = value; | |
}, | |
); | |
} | |
Widget _buildAddress() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'Address'), | |
maxLength: 100, | |
onSaved: (String value) { | |
_address = value; | |
}, | |
); | |
} | |
Widget _aboutContainer() { | |
return Container( | |
decoration: BoxDecoration( | |
color: kPrimaryLightColor, | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(8.0), | |
bottomLeft: Radius.circular(8.0), | |
bottomRight: Radius.circular(8.0), | |
topRight: Radius.circular(68.0)), | |
boxShadow: <BoxShadow>[ | |
BoxShadow( | |
color: Colors.grey.withOpacity(0.2), | |
offset: Offset(1.1, 1.1), | |
blurRadius: 10.0), | |
], | |
), | |
child: Column(children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(top: 16, left: 16, right: 16), | |
child: Row( | |
children: [ | |
Center( | |
child: Text( | |
'About me', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 30, | |
letterSpacing: 2, | |
color: Colors.black, | |
), | |
), | |
), | |
SizedBox( | |
width: 30, | |
), | |
IconButton( | |
icon: Icon( | |
FontAwesomeIcons.userCircle, | |
color: Colors.black, | |
), | |
onPressed: null) | |
], | |
)), | |
_buildSelf(), | |
_buildSell(), | |
SizedBox(height: 10), | |
_buildBirthday(), | |
]), | |
/*child: Text( | |
'This is a Container', | |
textScaleFactor: 1, | |
style: TextStyle(color: Colors.black), | |
),*/ | |
); | |
} | |
Widget _shopContainer() { | |
return Container( | |
decoration: BoxDecoration( | |
color: kPrimaryLightColor, | |
borderRadius: BorderRadius.only( | |
topLeft: Radius.circular(8.0), | |
bottomLeft: Radius.circular(8.0), | |
bottomRight: Radius.circular(8.0), | |
topRight: Radius.circular(68.0)), | |
boxShadow: <BoxShadow>[ | |
BoxShadow( | |
color: Colors.grey.withOpacity(0.2), | |
offset: Offset(1.1, 1.1), | |
blurRadius: 10.0), | |
], | |
), | |
child: Column(children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(top: 16, left: 16, right: 16), | |
child: Row( | |
children: [ | |
Center( | |
child: Text( | |
'Shop', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 30, | |
letterSpacing: 2, | |
color: Colors.black, | |
), | |
), | |
), | |
SizedBox( | |
width: 30, | |
), | |
IconButton( | |
icon: Icon( | |
FontAwesomeIcons.shoppingBag, | |
color: Colors.black, | |
), | |
onPressed: null), | |
], | |
)), | |
Text('Fill if physical shop is present'), | |
_buildShop(), | |
_buildShopLocation(), | |
_buildShopURL(), | |
]), | |
/*child: Text( | |
'This is a Container', | |
textScaleFactor: 1, | |
style: TextStyle(color: Colors.black), | |
),*/ | |
); | |
} | |
Widget _buildShopLocation() { | |
return TextFormField( | |
decoration: InputDecoration(labelText: 'Shop Location'), | |
//maxLength: 10, | |
onSaved: (String value) { | |
_shopLocation = value; | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text( | |
"Merchant account", | |
style: TextStyle(color: Colors.white), | |
), | |
backgroundColor: kPrimaryColor, | |
), | |
body: SingleChildScrollView( | |
child: Container( | |
margin: EdgeInsets.all(24), | |
child: Form( | |
key: _formKey, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
_merchantContainer(), | |
Column( | |
children: [ | |
_buildImage(), | |
SizedBox(height: 5), | |
Text('Please select photo') | |
], | |
), | |
SizedBox(height: 10), | |
_contactContainer(), | |
SizedBox(height: 10), | |
_aboutContainer(), | |
SizedBox(height: 10), | |
_addressContainer(), | |
SizedBox(height: 10), | |
_shopContainer(), | |
SizedBox(height: 10), | |
RaisedButton( | |
child: Text( | |
'Save', | |
style: TextStyle(color: Colors.blue, fontSize: 16), | |
), | |
onPressed: () { | |
if (img == null) { | |
return Toast.show("Kindly Provide An Image", (context)); | |
} | |
if (!_formKey.currentState.validate()) { | |
return; | |
} | |
if (img != null) { | |
uploadImage().then((result) async { | |
print("uploded image"); | |
print(imgUrlDownload); | |
if (imgUrlDownload != null) { | |
addData({ | |
'userPhoneNumber': "$countrycode$_phoneNumber", | |
'userId': widget.uid, | |
'userEmail': widget.userEmail, | |
'userName': widget.userName, | |
'userImage': imgUrlDownload, | |
'self': _self, | |
'sell': _sell, | |
'postalCode': _postalCode, | |
'address': _address, | |
'shop': _shop, | |
'shoplocation': _shopLocation, | |
'shopurl': _shopUrl, | |
'birthday': _birthDay, | |
"isMerchant": true, | |
/*'verified': { | |
'verification': true, | |
'message': '', | |
'logout': false | |
},*/ | |
}).then( | |
(result) { | |
print("data was added success fully"); | |
//Send to home | |
}, | |
); | |
} | |
}); | |
} | |
Navigator.push(context, | |
MaterialPageRoute(builder: (context) => Home())); | |
}) | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
void _userEditBottomSheet(BuildContext context) { | |
showModalBottomSheet( | |
context: context, | |
builder: (BuildContext bc) { | |
return Container( | |
height: MediaQuery.of(context).size.height * .30, | |
child: Padding( | |
padding: const EdgeInsets.only(left: 15.0, top: 15.0), | |
child: Column( | |
children: <Widget>[ | |
Row( | |
children: <Widget>[ | |
Container( | |
alignment: Alignment.center, | |
child: Padding( | |
padding: const EdgeInsets.only( | |
left: 35.0, | |
right: 15.0, | |
), | |
child: Column( | |
children: [ | |
IconButton( | |
icon: Icon(Icons.picture_in_picture_alt), | |
color: Colors.orange, | |
iconSize: 45, | |
onPressed: () { | |
getImageGallery(); | |
Navigator.pop(context); | |
}, | |
), | |
Text('Gallery') | |
], | |
), | |
), | |
), | |
SizedBox(width: 90), | |
//Spacer(), | |
Container( | |
child: Column( | |
children: [ | |
IconButton( | |
icon: Icon(Icons.camera), | |
color: Colors.orange, | |
iconSize: 45, | |
onPressed: () { | |
getImageCamera(); | |
Navigator.pop(context); | |
}, | |
), | |
Text('Camera') | |
], | |
), | |
), | |
], | |
), | |
], | |
), | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment