Skip to content

Commit cf83070

Browse files
authored
Merge pull request #255 from xkaper001/xkaper001/issue239
Implemented image uploading on web
2 parents 3820bb3 + 393c24c commit cf83070

File tree

13 files changed

+67
-103
lines changed

13 files changed

+67
-103
lines changed

lib/application/authentication/#_register/#_register_bloc.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'dart:developer';
2-
import 'dart:io';
2+
import 'dart:typed_data';
33

44
import 'package:firebase_auth/firebase_auth.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -40,7 +40,7 @@ class LoginRegisterBloc extends Bloc<LoginRegisterEvent, LoginRegisterState> {
4040

4141
if (event.profilePictureFile != null) {
4242
url = await _socialRepository.uploadProfilePicForUrl(
43-
file: event.profilePictureFile!);
43+
fileBytes: event.profilePictureFile!);
4444
} else {
4545
url = defaultProfilePicture;
4646
}
@@ -128,7 +128,7 @@ class LoginRegisterBloc extends Bloc<LoginRegisterEvent, LoginRegisterState> {
128128

129129
if (event.profilePictureFile != null) {
130130
url = await _socialRepository.uploadProfilePicForUrl(
131-
file: event.profilePictureFile!);
131+
fileBytes: event.profilePictureFile!);
132132
} else {
133133
url = defaultProfilePicture;
134134
}

lib/application/authentication/#_register/#_register_event.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class #WithEmailPressed extends LoginRegisterEvent {
4444
final String name;
4545
final String status;
4646
final String username;
47-
final File? profilePictureFile;
47+
final Uint8List? profilePictureFile;
4848

4949
const #WithEmailPressed({
5050
required this.email,
@@ -80,7 +80,7 @@ class SaveOnboardingDetails extends LoginRegisterEvent {
8080
final String name;
8181
final String username;
8282
final String status;
83-
final File? profilePictureFile;
83+
final Uint8List? profilePictureFile;
8484

8585
const SaveOnboardingDetails({
8686
required this.name,

lib/application/profile/update_profile/update_profile_bloc.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {
2727

2828
if (event.userInfo.values.first != null) {
2929
url = await _socialRepository.uploadProfilePicForUrl(
30-
file: event.userInfo.values.first);
30+
fileBytes: event.userInfo.values.first);
3131
} else {
3232
url = defaultProfilePicture;
3333
}
@@ -66,7 +66,7 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {
6666
if (event.userInfo.keys.contains('image') &&
6767
event.userInfo['image'] != null) {
6868
String url = await _socialRepository.uploadProfilePicForUrl(
69-
file: event.userInfo['image']);
69+
fileBytes: event.userInfo['image']);
7070
event.userInfo['profilePictureUrl'] = url;
7171
event.userInfo.remove('image');
7272
}

lib/data/repositories/firebase_social_repository.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:developer';
22
import 'dart:io';
3+
import 'dart:typed_data';
34

45
import 'package:cloud_firestore/cloud_firestore.dart';
56
import 'package:firebase_storage/firebase_storage.dart';
@@ -38,14 +39,14 @@ class FirebaseSocialRepository implements SocialRepository {
3839
}
3940

4041
@override
41-
Future<String> uploadProfilePicForUrl({required File file}) async {
42+
Future<String> uploadProfilePicForUrl({required Uint8List fileBytes}) async {
4243
String fileName = const Uuid().v4();
4344

4445
UploadTask task = _storage
4546
.ref()
4647
.child("profilePictures")
4748
.child("$fileName.jpg")
48-
.putFile(file);
49+
.putData(fileBytes);
4950

5051
TaskSnapshot snapshot = await task.whenComplete(() => null);
5152

lib/domain/repositories/social_repository.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:io';
2+
import 'dart:typed_data';
23

34
import 'package:monumento/data/models/comment_model.dart';
45
import 'package:monumento/data/models/notification_model.dart';
@@ -10,7 +11,7 @@ abstract interface class SocialRepository {
1011
Future<String> uploadImageForUrl(
1112
{required File file, required String address});
1213

13-
Future<String> uploadProfilePicForUrl({required File file});
14+
Future<String> uploadProfilePicForUrl({required Uint8List fileBytes});
1415

1516
Future<UserModel> getUserByUid({required String uid});
1617

lib/presentation/authentication/desktop/sign_up_view_desktop.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:io';
1+
import 'dart:typed_data';
22

33
import 'package:expandable_page_view/expandable_page_view.dart';
44
import 'package:flutter/material.dart';
@@ -31,7 +31,7 @@ class _#ViewDesktopState extends State<#ViewDesktop>
3131
late TextEditingController statusController;
3232
late PageController controller;
3333
GlobalKey<FormState> formKey = GlobalKey<FormState>();
34-
XFile? image;
34+
Uint8List? imageBytes;
3535
bool isSeen = false;
3636

3737
@override
@@ -140,16 +140,16 @@ class _#ViewDesktopState extends State<#ViewDesktop>
140140
final ImagePicker picker = ImagePicker();
141141
final img = await picker.pickImage(
142142
source: ImageSource.gallery,
143-
);
143+
).then((value) => value!.readAsBytes());
144144
setState(() {
145-
image = img;
145+
imageBytes = img;
146146
});
147147
},
148-
child: image != null
148+
child: imageBytes != null
149149
? CircleAvatar(
150150
radius: 40,
151151
backgroundImage:
152-
FileImage(File(image!.path)))
152+
MemoryImage(imageBytes!),)
153153
: CircleAvatar(
154154
radius: 40,
155155
backgroundColor:
@@ -261,8 +261,8 @@ class _#ViewDesktopState extends State<#ViewDesktop>
261261
name: nameController.text,
262262
username: usernameController.text,
263263
status: statusController.text,
264-
profilePictureFile: image != null
265-
? File(image!.path)
264+
profilePictureFile: imageBytes != null
265+
? imageBytes!
266266
: null,
267267
),
268268
);

lib/presentation/authentication/mobile/sign_up_view_mobile.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:io';
1+
import 'dart:typed_data';
22

33
import 'package:expandable_page_view/expandable_page_view.dart';
44
import 'package:flutter/material.dart';
@@ -28,7 +28,7 @@ class _#ViewMobileState extends State<#ViewMobile> {
2828
late TextEditingController statusController;
2929
late PageController controller;
3030
GlobalKey<FormState> formKey = GlobalKey<FormState>();
31-
XFile? image;
31+
Uint8List? imageBytes;
3232
bool render = false;
3333
bool isSeen = false;
3434

@@ -139,16 +139,16 @@ class _#ViewMobileState extends State<#ViewMobile> {
139139
final ImagePicker picker = ImagePicker();
140140
final img = await picker.pickImage(
141141
source: ImageSource.gallery,
142-
);
142+
).then((value) => value!.readAsBytes(),);
143143
setState(() {
144-
image = img;
144+
imageBytes = img;
145145
});
146146
},
147-
child: image != null
147+
child: imageBytes != null
148148
? CircleAvatar(
149149
radius: 40,
150150
backgroundImage:
151-
FileImage(File(image!.path)))
151+
MemoryImage(imageBytes!))
152152
: CircleAvatar(
153153
radius: 40,
154154
backgroundColor:
@@ -254,8 +254,8 @@ class _#ViewMobileState extends State<#ViewMobile> {
254254
name: nameController.text,
255255
username: usernameController.text,
256256
status: statusController.text,
257-
profilePictureFile: image != null
258-
? File(image!.path)
257+
profilePictureFile: imageBytes != null
258+
? imageBytes
259259
: null,
260260
),
261261
);

lib/presentation/discover/desktop/widgets/post_details_popup_widget.dart

+2-15
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,9 @@ class _PostDetailsPopupWidgetState extends State<PostDetailsPopupWidget> {
8282
),
8383
CircleAvatar(
8484
radius: 20,
85-
child: CachedNetworkImage(
86-
imageUrl: widget.post.author.profilePictureUrl ??
85+
backgroundImage: CachedNetworkImageProvider(
86+
widget.post.author.profilePictureUrl ??
8787
defaultProfilePicture,
88-
placeholder: (context, url) =>
89-
const CircularProgressIndicator(),
90-
errorWidget: (context, url, error) =>
91-
const Icon(Icons.error),
92-
imageBuilder: (context, imageProvider) => Container(
93-
decoration: BoxDecoration(
94-
shape: BoxShape.circle,
95-
image: DecorationImage(
96-
image: imageProvider,
97-
fit: BoxFit.cover,
98-
),
99-
),
100-
),
10188
),
10289
),
10390
const SizedBox(

lib/presentation/feed/desktop/widgets/create_post_card.dart

+1-6
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,8 @@ class _CreatePostCardState extends State<CreatePostCard>
6666
children: [
6767
CircleAvatar(
6868
radius: 24,
69-
child: CachedNetworkImage(
70-
imageUrl:
69+
backgroundImage: CachedNetworkImageProvider(
7170
state.user.profilePictureUrl ?? defaultProfilePicture,
72-
placeholder: (context, url) =>
73-
const CircularProgressIndicator(),
74-
errorWidget: (context, url, error) =>
75-
const Icon(Icons.error),
7671
),
7772
),
7873
const SizedBox(

lib/presentation/feed/desktop/widgets/feed_post_card.dart

+5-25
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,8 @@ class _FeedPostCardState extends State<FeedPostCard>
6767
children: [
6868
CircleAvatar(
6969
radius: 20,
70-
child: CachedNetworkImage(
71-
imageUrl: widget.post.author.profilePictureUrl ??
72-
defaultProfilePicture,
73-
placeholder: (context, url) =>
74-
const CircularProgressIndicator(),
75-
errorWidget: (context, url, error) =>
76-
const Icon(Icons.error),
77-
imageBuilder: (context, imageProvider) {
78-
return Container(
79-
decoration: BoxDecoration(
80-
shape: BoxShape.circle,
81-
image: DecorationImage(
82-
image: imageProvider,
83-
fit: BoxFit.cover,
84-
),
85-
),
86-
);
87-
},
70+
foregroundImage: CachedNetworkImageProvider(
71+
widget.post.author.profilePictureUrl ?? defaultProfilePicture,
8872
),
8973
),
9074
const SizedBox(
@@ -298,13 +282,9 @@ class _FeedPostCardState extends State<FeedPostCard>
298282
state = state as Authenticated;
299283
return CircleAvatar(
300284
radius: 20,
301-
child: CachedNetworkImage(
302-
imageUrl: state.user.profilePictureUrl ??
303-
defaultProfilePicture,
304-
placeholder: (context, url) =>
305-
const CircularProgressIndicator(),
306-
errorWidget: (context, url, error) =>
307-
const Icon(Icons.error),
285+
foregroundImage: CachedNetworkImageProvider(
286+
state.user.profilePictureUrl ??
287+
defaultProfilePicture
308288
),
309289
);
310290
},

lib/presentation/home/desktop/widgets/scaffold_with_navigation.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,14 @@ class _NavigationRailState extends State<_NavigationRail> {
401401
CircleAvatar(
402402
radius: 32,
403403
backgroundColor: AppColor.appGreyAccent,
404-
child: state.user.profilePictureUrl == null
404+
foregroundImage: state.user.profilePictureUrl == null
405405
? SvgPicture.asset(
406406
Assets.icons.icUser.path,
407-
)
408-
: CachedNetworkImage(
409-
imageUrl: state.user.profilePictureUrl!,
410-
width: 64,
411-
height: 64,
407+
) as ImageProvider
408+
: CachedNetworkImageProvider(
409+
state.user.profilePictureUrl!,
410+
maxWidth: 64,
411+
maxHeight: 64,
412412
)),
413413
const SizedBox(
414414
height: 10,

lib/presentation/settings/desktop/edit_profile_widget.dart

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:io';
1+
import 'dart:typed_data';
22

33
import 'package:cached_network_image/cached_network_image.dart';
44
import 'package:flutter/material.dart';
@@ -22,7 +22,7 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
2222
late TextEditingController nameController;
2323
late TextEditingController usernameController;
2424
late TextEditingController statusController;
25-
XFile? image;
25+
Uint8List? image;
2626

2727
@override
2828
void initState() {
@@ -82,24 +82,20 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
8282
children: [
8383
CircleAvatar(
8484
radius: 40,
85-
child: image != null
86-
? Image.file(File(image!.path))
87-
: CachedNetworkImage(
88-
imageUrl: widget.user.profilePictureUrl ??
89-
defaultProfilePicture,
90-
placeholder: (context, url) =>
91-
const CircularProgressIndicator(),
92-
errorWidget: (context, url, error) =>
93-
const Icon(Icons.error),
94-
),
85+
foregroundImage: image != null
86+
? MemoryImage(image!) as ImageProvider
87+
: CachedNetworkImageProvider(widget.user.profilePictureUrl ??
88+
defaultProfilePicture),
9589
),
9690
const SizedBox(width: 50),
9791
OutlinedButton(
9892
onPressed: () async {
9993
final ImagePicker picker = ImagePicker();
100-
final img = await picker.pickImage(
101-
source: ImageSource.gallery,
102-
);
94+
final img = await picker
95+
.pickImage(
96+
source: ImageSource.gallery,
97+
)
98+
.then((value) => value!.readAsBytes());
10399
setState(() {
104100
image = img;
105101
});
@@ -152,7 +148,7 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
152148
'name': nameController.text,
153149
'username': usernameController.text,
154150
'status': statusController.text,
155-
'image': image != null ? File(image!.path) : null,
151+
'image': image != null ? image : null,
156152
'shouldUpdateUsername':
157153
usernameController.text != widget.user.username,
158154
},

0 commit comments

Comments
 (0)