Skip to content

Commit

Permalink
Merge pull request #12 from acterglobal/ben-fix-parent-avatar
Browse files Browse the repository at this point in the history
Fix parent avatar
  • Loading branch information
gtalha07 authored Mar 8, 2024
2 parents 84f22dd + 98a2552 commit 29ecf75
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 177 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [unreleased]

- Fix: Show Parent even if we fallback for the main avatar

### [1.0.0+4]

- bump package dependencies.
Expand Down
16 changes: 13 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class _MyHomePageState extends State<MyHomePage> {
height: 20,
),
Text(
'Rectangular Acter Avatars ',
'Space Acter Avatars ',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
),
Padding(
Expand Down Expand Up @@ -308,7 +308,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
const SizedBox(height: 20),
Text(
'Rectangular Acter Avatars With Parent Badge',
'Space Acter Avatars With Parent Badge',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
),
Padding(
Expand Down Expand Up @@ -356,7 +356,10 @@ class _MyHomePageState extends State<MyHomePage> {
),
avatarsInfo: [
AvatarInfo(
uniqueId: uuid.v4(), displayName: 'Lorem Ipsum')
displayName: "C-Space",
uniqueId: uuid.v4(),
avatar: AssetImage('assets/images/space-3.jpg'),
),
],
),
SizedBox(
Expand Down Expand Up @@ -409,6 +412,13 @@ class _MyHomePageState extends State<MyHomePage> {
uniqueId: uuid.v4(),
avatar: AssetImage('assets/images/space-3.jpg'),
),
avatarsInfo: [
AvatarInfo(
displayName: "C-Space",
uniqueId: uuid.v4(),
avatar: AssetImage('assets/images/space-3.jpg'),
),
],
),
],
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0+3"
version: "1.0.0+4"
args:
dependency: transitive
description:
Expand Down
207 changes: 63 additions & 144 deletions lib/src/acter_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,53 +42,56 @@ class ActerAvatar extends StatefulWidget {
/// Avatar gesture tap for parent badge of `DisplayMode.Space`.
final void Function()? onParentBadgeTap;

ActerAvatar(
{Key? key,
required this.avatarInfo,
required this.mode,
this.onAvatarTap,
this.onParentBadgeTap,
this.avatarsInfo,
this.tooltip = TooltipStyle.Combined,
this.secondaryToolTip = TooltipStyle.Combined,
this.size,
this.badgeSize})
: super(key: key ?? Key('avatar-${avatarInfo.uniqueId}-$size'));
ActerAvatar({
Key? key,
required this.avatarInfo,
required this.mode,
this.onAvatarTap,
this.onParentBadgeTap,
this.avatarsInfo,
this.tooltip = TooltipStyle.Combined,
this.secondaryToolTip = TooltipStyle.Combined,
this.size,
this.badgeSize = 20,
}) : super(key: key ?? Key('avatar-${avatarInfo.uniqueId}-$size'));

@override
_ActerAvatar createState() => _ActerAvatar();
}

class _ActerAvatar extends State<ActerAvatar> {
bool imgSuccess = false;
bool secondaryImgSuccess = false;
ImageProvider<Object>? avatar;
ImageProvider<Object>? secondaryAvatar;

@override
void initState() {
super.initState();
_refreshAvatar();
}

@override
void didUpdateWidget(ActerAvatar oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.avatarInfo.avatar != oldWidget.avatarInfo.avatar ||
widget.avatarInfo.avatarFuture != oldWidget.avatarInfo.avatarFuture) {
_refreshAvatar();
}
}

void _refreshAvatar() {
ImageStreamListener listener =
ImageStreamListener(setImage, onError: setImageError);
ImageStreamListener secondaryListener =
ImageStreamListener(setSecondaryImage, onError: setSecondaryImageError);

// reset
avatar = null;
imgSuccess = false;

if (widget.avatarInfo.avatar != null) {
widget.avatarInfo.avatar!
.resolve(ImageConfiguration())
.addListener(listener);
if (widget.avatarsInfo != null && widget.avatarsInfo!.isNotEmpty) {
if (widget.avatarsInfo![0].avatar != null) {
widget.avatarsInfo![0].avatar!
.resolve(ImageConfiguration())
.addListener(secondaryListener);
}
}
} else if (widget.avatarInfo.avatarFuture != null) {
fetchImageProvider(listener);
if (widget.avatarsInfo != null && widget.avatarsInfo!.isNotEmpty) {
fetchSecondaryImageProvider(secondaryListener);
}
}
}

Expand All @@ -98,36 +101,18 @@ class _ActerAvatar extends State<ActerAvatar> {
avatar = res;
}

void fetchSecondaryImageProvider(ImageStreamListener listener) async {
var res = await widget.avatarsInfo![0].avatarFuture!;
res!.resolve(ImageConfiguration()).addListener(listener);
secondaryAvatar = res;
}

void setImage(ImageInfo image, bool sync) {
if (mounted) {
setState(() => imgSuccess = true);
}
}

void setSecondaryImage(ImageInfo image, bool sync) {
if (mounted) {
setState(() => secondaryImgSuccess = true);
}
}

void setImageError(Object obj, StackTrace? st) {
if (mounted) {
setState(() => imgSuccess = false);
}
}

void setSecondaryImageError(Object obj, StackTrace? st) {
if (mounted) {
setState(() => secondaryImgSuccess = false);
}
}

@override
Widget build(BuildContext context) {
final child =
Expand Down Expand Up @@ -177,20 +162,6 @@ class _ActerAvatar extends State<ActerAvatar> {
}
}

void secondaryAvatarError(Object error, StackTrace? stackTrace) {
log.warning(
'Error loading avatar for ${widget.avatarsInfo![0].uniqueId}. Returning to fallback.',
error,
stackTrace,
);
if (mounted) {
setState(() {
secondaryAvatar = null;
secondaryImgSuccess = false;
});
}
}

String? secTooltipMsg() {
if (widget.avatarsInfo != null || widget.avatarsInfo!.isNotEmpty) {
switch (widget.secondaryToolTip) {
Expand Down Expand Up @@ -224,7 +195,6 @@ class _ActerAvatar extends State<ActerAvatar> {
radius: widget.size ?? 24,
);
case DisplayMode.Space:
double badgeOverflow = badgeSize / 5;
return Stack(
clipBehavior: Clip.none,
children: <Widget>[
Expand All @@ -240,50 +210,7 @@ class _ActerAvatar extends State<ActerAvatar> {
),
),
),
Positioned(
bottom: -badgeOverflow,
right: -badgeOverflow,
child: (widget.avatarsInfo == null ||
widget.avatarsInfo!.isEmpty ||
widget.avatarsInfo![0].avatar == null)
? SizedBox(height: badgeSize + badgeOverflow)
: GestureDetector(
onTap: widget.onParentBadgeTap,
child: Column(
children: <Widget>[
SizedBox(
height: widget.badgeSize ?? badgeSize,
width: widget.badgeSize ?? badgeSize,
child: widget.secondaryToolTip != TooltipStyle.None
? Tooltip(
message: secTooltipMsg(),
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(6.0),
image: DecorationImage(
fit: BoxFit.cover,
image: widget.avatarsInfo![0].avatar!,
onError: secondaryAvatarError,
),
),
),
)
: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
image: DecorationImage(
fit: BoxFit.cover,
image: widget.avatarsInfo![0].avatar!,
onError: secondaryAvatarError,
),
),
),
),
],
),
),
),
renderSpaceParent(context),
],
);
case DisplayMode.GroupChat:
Expand Down Expand Up @@ -318,13 +245,11 @@ class _ActerAvatar extends State<ActerAvatar> {
message: secTooltipMsg(),
child: CircleAvatar(
foregroundImage: widget.avatarsInfo![0].avatar,
onForegroundImageError: secondaryAvatarError,
radius: widget.size ?? 24,
),
)
: CircleAvatar(
foregroundImage: widget.avatarsInfo![0].avatar,
onForegroundImageError: secondaryAvatarError,
radius: widget.size ?? 24,
),
),
Expand Down Expand Up @@ -359,9 +284,39 @@ class _ActerAvatar extends State<ActerAvatar> {
}
}

Widget renderFallback(BuildContext context) {
Widget renderSpaceParent(BuildContext context) {
if (widget.badgeSize == null) {
// nothing. ignore
return SizedBox.shrink();
}
final badgeSize = widget.badgeSize ?? 20;
if (widget.avatarsInfo == null || widget.avatarsInfo!.isEmpty) {
return SizedBox(
height: badgeSize,
width: badgeSize,
);
}

final parentInfo = widget.avatarsInfo![0];
double badgeOverflow = badgeSize / 5;
double textFallbackSize = widget.size == null ? 48 : widget.size!;

return Positioned(
bottom: -badgeOverflow,
right: -badgeOverflow,
child: GestureDetector(
onTap: widget.onParentBadgeTap,
child: ActerAvatar(
avatarInfo: parentInfo,
mode: DisplayMode.Space,
size: badgeSize,
badgeSize: null,
),
),
);
}

Widget renderFallback(BuildContext context) {
double textFallbackSize = widget.size ?? 48;
double multiFallbackSize = widget.size == null ? 48 : widget.size! * 2.0;

/// Fallback
Expand Down Expand Up @@ -435,43 +390,7 @@ class _ActerAvatar extends State<ActerAvatar> {
size: textFallbackSize,
shape: Shape.Rectangle,
),
Positioned(
bottom: -badgeOverflow,
right: -badgeOverflow,
child: widget.avatarsInfo == null || widget.avatarsInfo!.isEmpty
? SizedBox(height: badgeSize + badgeOverflow)
: GestureDetector(
onTap: widget.onParentBadgeTap,
child: Column(
children: <Widget>[
SizedBox(
height: widget.badgeSize ?? badgeSize,
width: widget.badgeSize ?? badgeSize,
child: widget.secondaryToolTip != TooltipStyle.None
? Tooltip(
message: secTooltipMsg(),
child: TextAvatar(
text:
widget.avatarsInfo![0].displayName ??
widget.avatarsInfo![0].uniqueId,
sourceText:
widget.avatarsInfo![0].uniqueId,
fontSize: 6,
shape: Shape.Rectangle,
),
)
: TextAvatar(
text: widget.avatarsInfo![0].displayName ??
widget.avatarsInfo![0].uniqueId,
sourceText: widget.avatarsInfo![0].uniqueId,
fontSize: 6,
shape: Shape.Rectangle,
),
),
],
),
),
),
renderSpaceParent(context),
],
);
case DisplayMode.GroupChat:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/constants/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class TestKeys {
static const multiAvatarKey = Key('Multi-avatar');
static const textAvatarKey = Key('Text-Avatar');
static const circleAvatarKey = Key('Circle-Avatar');
static const rectangleAvatarKey = Key('Rectangle-Avatar');
static const spaceAvatarKey = Key('Space-Avatar');
static const stackedAvatarKey = Key('Stacked-Avatar');
}
Loading

0 comments on commit 29ecf75

Please sign in to comment.