Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented image uploading on web #255

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';

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

if (event.profilePictureFile != null) {
url = await _socialRepository.uploadProfilePicForUrl(
file: event.profilePictureFile!);
fileBytes: event.profilePictureFile!);
} else {
url = defaultProfilePicture;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ class LoginRegisterBloc extends Bloc<LoginRegisterEvent, LoginRegisterState> {

if (event.profilePictureFile != null) {
url = await _socialRepository.uploadProfilePicForUrl(
file: event.profilePictureFile!);
fileBytes: event.profilePictureFile!);
} else {
url = defaultProfilePicture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SignUpWithEmailPressed extends LoginRegisterEvent {
final String name;
final String status;
final String username;
final File? profilePictureFile;
final Uint8List? profilePictureFile;

const SignUpWithEmailPressed({
required this.email,
Expand Down Expand Up @@ -80,7 +80,7 @@ class SaveOnboardingDetails extends LoginRegisterEvent {
final String name;
final String username;
final String status;
final File? profilePictureFile;
final Uint8List? profilePictureFile;

const SaveOnboardingDetails({
required this.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {

if (event.userInfo.values.first != null) {
url = await _socialRepository.uploadProfilePicForUrl(
file: event.userInfo.values.first);
fileBytes: event.userInfo.values.first);
} else {
url = defaultProfilePicture;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {
if (event.userInfo.keys.contains('image') &&
event.userInfo['image'] != null) {
String url = await _socialRepository.uploadProfilePicForUrl(
file: event.userInfo['image']);
fileBytes: event.userInfo['image']);
event.userInfo['profilePictureUrl'] = url;
event.userInfo.remove('image');
}
Expand Down
5 changes: 3 additions & 2 deletions lib/data/repositories/firebase_social_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
Expand Down Expand Up @@ -38,14 +39,14 @@ class FirebaseSocialRepository implements SocialRepository {
}

@override
Future<String> uploadProfilePicForUrl({required File file}) async {
Future<String> uploadProfilePicForUrl({required Uint8List fileBytes}) async {
String fileName = const Uuid().v4();

UploadTask task = _storage
.ref()
.child("profilePictures")
.child("$fileName.jpg")
.putFile(file);
.putData(fileBytes);

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

Expand Down
3 changes: 2 additions & 1 deletion lib/domain/repositories/social_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

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

Future<String> uploadProfilePicForUrl({required File file});
Future<String> uploadProfilePicForUrl({required Uint8List fileBytes});

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:expandable_page_view/expandable_page_view.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -31,7 +31,7 @@ class _SignUpViewDesktopState extends State<SignUpViewDesktop>
late TextEditingController statusController;
late PageController controller;
GlobalKey<FormState> formKey = GlobalKey<FormState>();
XFile? image;
Uint8List? imageBytes;
bool isSeen = false;

@override
Expand Down Expand Up @@ -140,16 +140,16 @@ class _SignUpViewDesktopState extends State<SignUpViewDesktop>
final ImagePicker picker = ImagePicker();
final img = await picker.pickImage(
source: ImageSource.gallery,
);
).then((value) => value!.readAsBytes());
setState(() {
image = img;
imageBytes = img;
});
},
child: image != null
child: imageBytes != null
? CircleAvatar(
radius: 40,
backgroundImage:
FileImage(File(image!.path)))
MemoryImage(imageBytes!),)
: CircleAvatar(
radius: 40,
backgroundColor:
Expand Down Expand Up @@ -261,8 +261,8 @@ class _SignUpViewDesktopState extends State<SignUpViewDesktop>
name: nameController.text,
username: usernameController.text,
status: statusController.text,
profilePictureFile: image != null
? File(image!.path)
profilePictureFile: imageBytes != null
? imageBytes!
: null,
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:expandable_page_view/expandable_page_view.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -28,7 +28,7 @@ class _SignUpViewMobileState extends State<SignUpViewMobile> {
late TextEditingController statusController;
late PageController controller;
GlobalKey<FormState> formKey = GlobalKey<FormState>();
XFile? image;
Uint8List? imageBytes;
bool render = false;
bool isSeen = false;

Expand Down Expand Up @@ -138,16 +138,16 @@ class _SignUpViewMobileState extends State<SignUpViewMobile> {
final ImagePicker picker = ImagePicker();
final img = await picker.pickImage(
source: ImageSource.gallery,
);
).then((value) => value!.readAsBytes(),);
setState(() {
image = img;
imageBytes = img;
});
},
child: image != null
child: imageBytes != null
? CircleAvatar(
radius: 40,
backgroundImage:
FileImage(File(image!.path)))
MemoryImage(imageBytes!))
: CircleAvatar(
radius: 40,
backgroundColor:
Expand Down Expand Up @@ -253,8 +253,8 @@ class _SignUpViewMobileState extends State<SignUpViewMobile> {
name: nameController.text,
username: usernameController.text,
status: statusController.text,
profilePictureFile: image != null
? File(image!.path)
profilePictureFile: imageBytes != null
? imageBytes
: null,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,9 @@ class _PostDetailsPopupWidgetState extends State<PostDetailsPopupWidget> {
),
CircleAvatar(
radius: 20,
child: CachedNetworkImage(
imageUrl: widget.post.author.profilePictureUrl ??
backgroundImage: CachedNetworkImageProvider(
widget.post.author.profilePictureUrl ??
defaultProfilePicture,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(Icons.error),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
),
),
const SizedBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ class _CreatePostCardState extends State<CreatePostCard>
children: [
CircleAvatar(
radius: 24,
child: CachedNetworkImage(
imageUrl:
backgroundImage: CachedNetworkImageProvider(
state.user.profilePictureUrl ?? defaultProfilePicture,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(Icons.error),
),
),
const SizedBox(
Expand Down
30 changes: 5 additions & 25 deletions lib/presentation/feed/desktop/widgets/feed_post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,8 @@ class _FeedPostCardState extends State<FeedPostCard>
children: [
CircleAvatar(
radius: 20,
child: CachedNetworkImage(
imageUrl: widget.post.author.profilePictureUrl ??
defaultProfilePicture,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(Icons.error),
imageBuilder: (context, imageProvider) {
return Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
);
},
foregroundImage: CachedNetworkImageProvider(
widget.post.author.profilePictureUrl ?? defaultProfilePicture,
),
),
const SizedBox(
Expand Down Expand Up @@ -298,13 +282,9 @@ class _FeedPostCardState extends State<FeedPostCard>
state = state as Authenticated;
return CircleAvatar(
radius: 20,
child: CachedNetworkImage(
imageUrl: state.user.profilePictureUrl ??
defaultProfilePicture,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(Icons.error),
foregroundImage: CachedNetworkImageProvider(
state.user.profilePictureUrl ??
defaultProfilePicture
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,14 @@ class _NavigationRailState extends State<_NavigationRail> {
CircleAvatar(
radius: 32,
backgroundColor: AppColor.appGreyAccent,
child: state.user.profilePictureUrl == null
foregroundImage: state.user.profilePictureUrl == null
? SvgPicture.asset(
Assets.icons.icUser.path,
)
: CachedNetworkImage(
imageUrl: state.user.profilePictureUrl!,
width: 64,
height: 64,
) as ImageProvider
: CachedNetworkImageProvider(
state.user.profilePictureUrl!,
maxWidth: 64,
maxHeight: 64,
)),
const SizedBox(
height: 10,
Expand Down
28 changes: 12 additions & 16 deletions lib/presentation/settings/desktop/edit_profile_widget.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
Expand All @@ -22,7 +22,7 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
late TextEditingController nameController;
late TextEditingController usernameController;
late TextEditingController statusController;
XFile? image;
Uint8List? image;

@override
void initState() {
Expand Down Expand Up @@ -82,24 +82,20 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
children: [
CircleAvatar(
radius: 40,
child: image != null
? Image.file(File(image!.path))
: CachedNetworkImage(
imageUrl: widget.user.profilePictureUrl ??
defaultProfilePicture,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(Icons.error),
),
foregroundImage: image != null
? MemoryImage(image!) as ImageProvider
: CachedNetworkImageProvider(widget.user.profilePictureUrl ??
defaultProfilePicture),
),
const SizedBox(width: 50),
OutlinedButton(
onPressed: () async {
final ImagePicker picker = ImagePicker();
final img = await picker.pickImage(
source: ImageSource.gallery,
);
final img = await picker
.pickImage(
source: ImageSource.gallery,
)
.then((value) => value!.readAsBytes());
setState(() {
image = img;
});
Expand Down Expand Up @@ -152,7 +148,7 @@ class _EditProfileWidgetState extends State<EditProfileWidget> {
'name': nameController.text,
'username': usernameController.text,
'status': statusController.text,
'image': image != null ? File(image!.path) : null,
'image': image != null ? image : null,
'shouldUpdateUsername':
usernameController.text != widget.user.username,
},
Expand Down
Loading