Skip to content

Commit 3c55c69

Browse files
committed
✨ (feat) Add module to upload profile banner image
1 parent 682bbda commit 3c55c69

File tree

4 files changed

+90
-28
lines changed

4 files changed

+90
-28
lines changed

lib/model/user.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class UserModel {
66
String userName;
77
String webSite;
88
String profilePic;
9+
String bannerImage;
910
String contact;
1011
String bio;
1112
String location;
@@ -23,6 +24,7 @@ class UserModel {
2324
this.userId,
2425
this.displayName,
2526
this.profilePic,
27+
this.bannerImage,
2628
this.key,
2729
this.contact,
2830
this.bio,
@@ -49,6 +51,7 @@ class UserModel {
4951
userId = map['userId'];
5052
displayName = map['displayName'];
5153
profilePic = map['profilePic'];
54+
bannerImage = map['bannerImage'];
5255
key = map['key'];
5356
dob = map['dob'];
5457
bio = map['bio'];
@@ -82,8 +85,8 @@ class UserModel {
8285
"userId": userId,
8386
"email": email,
8487
'displayName': displayName,
85-
'userId': userId,
8688
'profilePic': profilePic,
89+
'bannerImage': bannerImage,
8790
'contact': contact,
8891
'dob': dob,
8992
'bio': bio,
@@ -109,6 +112,7 @@ class UserModel {
109112
String contact,
110113
bio,
111114
String dob,
115+
String bannerImage,
112116
String location,
113117
String createdAt,
114118
String userName,
@@ -132,6 +136,7 @@ class UserModel {
132136
key: key ?? this.key,
133137
location: location ?? this.location,
134138
profilePic: profilePic ?? this.profilePic,
139+
bannerImage: bannerImage ?? this.bannerImage,
135140
userId: userId ?? this.userId,
136141
userName: userName ?? this.userName,
137142
webSite: webSite ?? this.webSite,

lib/page/profile/EditProfilePage.dart

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class EditProfilePage extends StatefulWidget {
1212

1313
class _EditProfilePageState extends State<EditProfilePage> {
1414
File _image;
15+
File _banner;
1516
TextEditingController _name;
1617
TextEditingController _bio;
1718
TextEditingController _location;
@@ -49,13 +50,7 @@ class _EditProfilePageState extends State<EditProfilePage> {
4950
height: 180,
5051
child: Stack(
5152
children: <Widget>[
52-
Container(
53-
height: 180,
54-
padding: EdgeInsets.only(bottom: 50),
55-
child: customNetworkImage(
56-
'https://pbs.twimg.com/profile_banners/457684585/1510495215/1500x500',
57-
fit: BoxFit.fill),
58-
),
53+
_bannerImage(authstate),
5954
Align(
6055
alignment: Alignment.bottomLeft,
6156
child: _userImage(authstate),
@@ -107,6 +102,42 @@ class _EditProfilePageState extends State<EditProfilePage> {
107102
);
108103
}
109104

105+
Widget _bannerImage(AuthState authstate) {
106+
return Container(
107+
height: 180,
108+
decoration: BoxDecoration(
109+
image: authstate.userModel.bannerImage == null
110+
? null
111+
: DecorationImage(
112+
image:
113+
customAdvanceNetworkImage(authstate.userModel.bannerImage),
114+
fit: BoxFit.cover),
115+
),
116+
child: Container(
117+
decoration: BoxDecoration(
118+
color: Colors.black45,
119+
),
120+
child: Stack(
121+
children: [
122+
_banner != null
123+
? Image.file(_banner,
124+
fit: BoxFit.fill, width: MediaQuery.of(context).size.width)
125+
: customNetworkImage(
126+
authstate.userModel.bannerImage ??
127+
'https://pbs.twimg.com/profile_banners/457684585/1510495215/1500x500',
128+
fit: BoxFit.fill),
129+
Center(
130+
child: IconButton(
131+
onPressed: uploadBanner,
132+
icon: Icon(Icons.camera_alt, color: Colors.white),
133+
),
134+
),
135+
],
136+
),
137+
),
138+
);
139+
}
140+
110141
Widget _entry(String title,
111142
{TextEditingController controller,
112143
int maxLine = 1,
@@ -161,6 +192,7 @@ class _EditProfilePageState extends State<EditProfilePage> {
161192
location: state.userModel.location,
162193
profilePic: state.userModel.profilePic,
163194
userId: state.userModel.userId,
195+
bannerImage: state.userModel.bannerImage,
164196
);
165197
if (_name.text != null && _name.text.isNotEmpty) {
166198
model.displayName = _name.text;
@@ -174,7 +206,7 @@ class _EditProfilePageState extends State<EditProfilePage> {
174206
if (dob != null) {
175207
model.dob = dob;
176208
}
177-
state.updateUserProfile(model, image: _image);
209+
state.updateUserProfile(model, image: _image, bannerImage: _banner);
178210
Navigator.of(context).pop();
179211
}
180212

@@ -186,6 +218,14 @@ class _EditProfilePageState extends State<EditProfilePage> {
186218
});
187219
}
188220

221+
void uploadBanner() {
222+
openImagePicker(context, (file) {
223+
setState(() {
224+
_banner = file;
225+
});
226+
});
227+
}
228+
189229
@override
190230
Widget build(BuildContext context) {
191231
return Scaffold(

lib/page/profile/profilePage.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class _ProfilePageState extends State<ProfilePage>
9797
height: 180,
9898
padding: EdgeInsets.only(top: 28),
9999
child: customNetworkImage(
100-
'https://pbs.twimg.com/profile_banners/457684585/1510495215/1500x500',
100+
authstate.userModel.bannerImage ??
101+
'https://pbs.twimg.com/profile_banners/457684585/1510495215/1500x500',
101102
fit: BoxFit.fill,
102103
),
103104
),

lib/state/authState.dart

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AuthState extends AppState {
2323
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
2424
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
2525
final GoogleSignIn _googleSignIn = GoogleSignIn();
26+
final FirebaseStorage _firebaseStorage = FirebaseStorage.instance;
2627
dabase.Query _profileQuery;
2728
List<UserModel> _profileUserModelList;
2829
UserModel _userModel;
@@ -296,29 +297,35 @@ class AuthState extends AppState {
296297
}
297298

298299
/// `Update user` profile
299-
Future<void> updateUserProfile(UserModel userModel, {File image}) async {
300+
Future<void> updateUserProfile(UserModel userModel,
301+
{File image, File bannerImage}) async {
300302
try {
301-
if (image == null) {
303+
if (image == null && bannerImage == null) {
302304
createUser(userModel);
303305
} else {
304-
var storageReference = FirebaseStorage.instance
305-
.ref()
306-
.child('user/profile/${Path.basename(image.path)}');
307-
await storageReference.putFile(image);
308-
309-
storageReference.getDownloadURL().then((fileURL) async {
310-
print(fileURL);
306+
/// upload profile image if not null
307+
if (image != null) {
308+
/// get image storage path from server
309+
userModel.profilePic = await _uploadFileToStorage(image,
310+
'user/profile/${userModel.userName}/${Path.basename(image.path)}');
311+
// print(fileURL);
311312
var name = userModel?.displayName ?? user.displayName;
312313
_firebaseAuth.currentUser
313-
.updateProfile(displayName: name, photoURL: fileURL);
314-
if (userModel != null) {
315-
userModel.profilePic = fileURL;
316-
createUser(userModel);
317-
} else {
318-
_userModel.profilePic = fileURL;
319-
createUser(_userModel);
320-
}
321-
});
314+
.updateProfile(displayName: name, photoURL: userModel.profilePic);
315+
}
316+
317+
/// upload banner image if not null
318+
if (bannerImage != null) {
319+
/// get banner storage path from server
320+
userModel.bannerImage = await _uploadFileToStorage(bannerImage,
321+
'user/profile/${userModel.userName}/${Path.basename(bannerImage.path)}');
322+
}
323+
324+
if (userModel != null) {
325+
createUser(userModel);
326+
} else {
327+
createUser(_userModel);
328+
}
322329
}
323330

324331
logEvent('update_user');
@@ -327,6 +334,15 @@ class AuthState extends AppState {
327334
}
328335
}
329336

337+
Future<String> _uploadFileToStorage(File file, path) async {
338+
var task = _firebaseStorage.ref().child(path);
339+
var status = await task.putFile(file);
340+
print(status.state);
341+
342+
/// get file storage path from server
343+
return await task.getDownloadURL();
344+
}
345+
330346
/// `Fetch` user `detail` whoose userId is passed
331347
Future<UserModel> getuserDetail(String userId) async {
332348
UserModel user;

0 commit comments

Comments
 (0)