Skip to content

Commit b9694e3

Browse files
committed
content [nfc]: Decompose Avatar into AvatarImage and AvatarShape
As Greg suggests: zulip#249 (comment)
1 parent e73868a commit b9694e3

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

lib/widgets/content.dart

+39-2
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,26 @@ class Avatar extends StatelessWidget {
825825
final double size;
826826
final double borderRadius;
827827

828+
@override
829+
Widget build(BuildContext context) {
830+
return AvatarShape(
831+
size: size,
832+
borderRadius: borderRadius,
833+
child: AvatarImage(userId: userId));
834+
}
835+
}
836+
837+
/// The appropriate avatar image for a user ID.
838+
///
839+
/// Wrap this with [AvatarShape].
840+
class AvatarImage extends StatelessWidget {
841+
const AvatarImage({
842+
super.key,
843+
required this.userId,
844+
});
845+
846+
final int userId;
847+
828848
@override
829849
Widget build(BuildContext context) {
830850
final store = PerAccountStoreWidget.of(context);
@@ -834,16 +854,33 @@ class Avatar extends StatelessWidget {
834854
null => null, // TODO(#255): handle computing gravatars
835855
var avatarUrl => resolveUrl(avatarUrl, store.account),
836856
};
837-
final avatar = (resolvedUrl == null)
857+
return (resolvedUrl == null)
838858
? const SizedBox.shrink()
839859
: RealmContentNetworkImage(resolvedUrl, filterQuality: FilterQuality.medium);
860+
}
861+
}
862+
863+
/// A rounded square shape, to wrap an [AvatarImage] or similar.
864+
class AvatarShape extends StatelessWidget {
865+
const AvatarShape({
866+
super.key,
867+
required this.size,
868+
required this.borderRadius,
869+
required this.child,
870+
});
840871

872+
final double size;
873+
final double borderRadius;
874+
final Widget child;
875+
876+
@override
877+
Widget build(BuildContext context) {
841878
return SizedBox.square(
842879
dimension: size,
843880
child: ClipRRect(
844881
borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
845882
clipBehavior: Clip.antiAlias,
846-
child: avatar));
883+
child: child));
847884
}
848885
}
849886

0 commit comments

Comments
 (0)