Skip to content

Commit 61b2788

Browse files
add loader on profile picture upload (#49)
1 parent 65aa0d1 commit 61b2788

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

lib/presentation/common/core/widgets/upload_file.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
55
import 'package:hooks_riverpod/hooks_riverpod.dart';
66
import 'package:image_picker/image_picker.dart';
7+
import '../utils/ui_utils.dart';
78

89
import '../utils/color_utils.dart';
910

@@ -15,13 +16,18 @@ class UploadFileWidget extends HookConsumerWidget {
1516
/// The function to call after we chose the picture
1617
final Function callbackFunc;
1718

19+
final bool isUploading;
20+
1821
final _picker = ImagePicker();
1922

2023
/// Creates a [UploadFileWidget] widget.
2124
///
2225
/// The [image] is the image to display.
2326
UploadFileWidget(
24-
{super.key, required this.image, required this.callbackFunc});
27+
{super.key,
28+
required this.image,
29+
required this.callbackFunc,
30+
required this.isUploading});
2531

2632
@override
2733
Widget build(BuildContext context, WidgetRef ref) {
@@ -33,10 +39,14 @@ class UploadFileWidget extends HookConsumerWidget {
3339
width: 200,
3440
height: 200,
3541
color: ColorUtils.greyLight,
36-
child: image != null
37-
? Image.memory(image!, fit: BoxFit.cover)
38-
: Text(
39-
AppLocalizations.of(context)!.profile_picture_select_please),
42+
child: isUploading
43+
? Center(
44+
child: UIUtils.loader,
45+
)
46+
: image != null
47+
? Image.memory(image!, fit: BoxFit.cover)
48+
: Text(AppLocalizations.of(context)!
49+
.profile_picture_select_please),
4050
),
4151
),
4252
ElevatedButton(

lib/presentation/settings/screens/edit_profile_screen.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class EditProfileScreen extends HookConsumerWidget {
8686
const SizedBox(height: 10),
8787
UploadFileWidget(
8888
image: profilePicture,
89+
isUploading:
90+
state.isUploading,
8991
callbackFunc: provider
9092
.chooseNewProfilePicture),
9193
// Firstname TextFormField

lib/presentation/settings/view_model/edit_profile_view_model.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class EditProfileViewModel extends StateNotifier<EditProfileState> {
4242
}
4343

4444
Future<void> chooseNewProfilePicture(Uint8List image) async {
45+
state = state.copyWith(isUploading: true);
4546
ref
4647
.read(userRepositoryProvider)
4748
.uploadProfilePicture(image)
@@ -52,6 +53,7 @@ class EditProfileViewModel extends StateNotifier<EditProfileState> {
5253
.watch(profilePictureViewModelProvider(currentUser.id).notifier)
5354
.editProfilePicture(image);
5455
}
56+
state = state.copyWith(isUploading: false);
5557
});
5658
}
5759

lib/presentation/settings/view_model/state/edit_profile_state.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ class EditProfileState {
77
final Uint8List? profilePicture;
88
final bool isEditing;
99
final bool errorOnRequest;
10+
final bool isUploading;
1011

1112
/// Creates a new instance of [EditProfileState].
1213
const EditProfileState(
1314
{required this.firstname,
1415
required this.lastname,
1516
required this.profilePicture,
1617
required this.isEditing,
17-
required this.errorOnRequest});
18+
required this.errorOnRequest,
19+
required this.isUploading});
1820

1921
/// Creates the initial state for the edit profile screen.
2022
factory EditProfileState.initial() {
@@ -23,7 +25,8 @@ class EditProfileState {
2325
lastname: '',
2426
profilePicture: null,
2527
isEditing: false,
26-
errorOnRequest: false);
28+
errorOnRequest: false,
29+
isUploading: false);
2730
}
2831

2932
/// Creates a copy of this state object with the specified changes.
@@ -32,12 +35,14 @@ class EditProfileState {
3235
String? lastname,
3336
Uint8List? profilePicture,
3437
bool? isEditing,
35-
bool? errorOnRequest}) {
38+
bool? errorOnRequest,
39+
bool? isUploading}) {
3640
return EditProfileState(
3741
firstname: firstname ?? this.firstname,
3842
lastname: lastname ?? this.lastname,
3943
profilePicture: profilePicture ?? this.profilePicture,
4044
isEditing: isEditing ?? this.isEditing,
41-
errorOnRequest: errorOnRequest ?? this.errorOnRequest);
45+
errorOnRequest: errorOnRequest ?? this.errorOnRequest,
46+
isUploading: isUploading ?? this.isUploading);
4247
}
4348
}

0 commit comments

Comments
 (0)