From 3a0c7397860030e9a8eb16ef592df68259cd234f Mon Sep 17 00:00:00 2001 From: Stein Milder Date: Fri, 25 Nov 2022 12:09:38 +0100 Subject: [PATCH] feat: onUploadStateChanged avatar --- .../lib/utils/example_profile_service.dart | 4 ++- lib/src/services/profile_service.dart | 6 +++-- lib/src/widgets/profile/profile_wrapper.dart | 25 +++++++++++++++---- test/test_classes/test_profile_service.dart | 7 +++++- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/example/lib/utils/example_profile_service.dart b/example/lib/utils/example_profile_service.dart index 4a74b7d..9e505ce 100644 --- a/example/lib/utils/example_profile_service.dart +++ b/example/lib/utils/example_profile_service.dart @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: BSD-3-Clause +import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_profile/flutter_profile.dart'; @@ -23,7 +24,8 @@ class ExampleProfileService extends ProfileService { } @override - Future uploadImage(BuildContext context) async { + FutureOr uploadImage(BuildContext context, + {required Function(bool isUploading) onUploadStateChanged}) { debugPrint('Updating avatar'); } } diff --git a/lib/src/services/profile_service.dart b/lib/src/services/profile_service.dart index 54ed581..ed95f4e 100644 --- a/lib/src/services/profile_service.dart +++ b/lib/src/services/profile_service.dart @@ -3,7 +3,6 @@ // SPDX-License-Identifier: BSD-3-Clause import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter_profile/src/models/user.dart'; @@ -21,5 +20,8 @@ abstract class ProfileService { FutureOr editProfile(User user, String key, String? value); - FutureOr uploadImage(BuildContext context); + FutureOr uploadImage( + BuildContext context, { + required Function(bool isUploading) onUploadStateChanged, + }); } diff --git a/lib/src/widgets/profile/profile_wrapper.dart b/lib/src/widgets/profile/profile_wrapper.dart index 98e7b20..391eda2 100644 --- a/lib/src/widgets/profile/profile_wrapper.dart +++ b/lib/src/widgets/profile/profile_wrapper.dart @@ -49,9 +49,9 @@ class ProfileWrapper extends StatefulWidget { class _ProfileWrapperState extends State { List defaultItems = []; - GlobalKey formKey = GlobalKey(); Map formValues = {}; + bool _isUploadingImage = false; late final Widget child; @override @@ -215,13 +215,28 @@ class _ProfileWrapperState extends State { children: [ if (widget.showAvatar) ...[ InkWell( - onTap: () async { - await widget.service.uploadImage(context); - }, + onTap: () => widget.service.uploadImage( + context, + onUploadStateChanged: (isUploading) => setState( + () { + _isUploadingImage = isUploading; + }, + ), + ), child: AvatarWrapper( user: widget.user, textStyle: widget.style.avatarTextStyle, - customAvatar: widget.customAvatar, + customAvatar: _isUploadingImage + ? Container( + width: 100, + height: 100, + decoration: const BoxDecoration( + color: Colors.black, + shape: BoxShape.circle, + ), + child: const CircularProgressIndicator(), + ) + : widget.customAvatar, ), ), SizedBox( diff --git a/test/test_classes/test_profile_service.dart b/test/test_classes/test_profile_service.dart index 41ee19a..32cc46f 100644 --- a/test/test_classes/test_profile_service.dart +++ b/test/test_classes/test_profile_service.dart @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: BSD-3-Clause +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_profile/flutter_profile.dart'; @@ -19,5 +21,8 @@ class TestProfileService extends ProfileService { ) {} @override - Future uploadImage(BuildContext context) async {} + FutureOr uploadImage( + BuildContext context, { + required Function(bool isUploading) onUploadStateChanged, + }) {} }