From fad22bf9df23297a95268464a3c77e995351529d Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 7 Jun 2023 08:58:56 +0200 Subject: [PATCH] neon: Display user avatars according to size constraints --- packages/neon/neon/lib/src/pages/home.dart | 6 +- .../neon/lib/src/widgets/account_tile.dart | 6 +- .../neon/lib/src/widgets/user_avatar.dart | 82 ++++++++++--------- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 76bd3566..c9d23b18 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -495,10 +495,8 @@ class _HomePageState extends State { AccountSettingsRoute(accountid: account.id).go(context); }, tooltip: AppLocalizations.of(context).settingsAccount, - icon: IntrinsicWidth( - child: NeonUserAvatar( - account: account, - ), + icon: NeonUserAvatar( + account: account, ), ), ], diff --git a/packages/neon/neon/lib/src/widgets/account_tile.dart b/packages/neon/neon/lib/src/widgets/account_tile.dart index 4da61494..54eb5b15 100644 --- a/packages/neon/neon/lib/src/widgets/account_tile.dart +++ b/packages/neon/neon/lib/src/widgets/account_tile.dart @@ -33,10 +33,8 @@ class NeonAccountTile extends StatelessWidget { vertical: -4, ) : null, - leading: IntrinsicWidth( - child: NeonUserAvatar( - account: account, - ), + leading: NeonUserAvatar( + account: account, ), title: ResultBuilder( stream: userDetailsBloc.userDetails, diff --git a/packages/neon/neon/lib/src/widgets/user_avatar.dart b/packages/neon/neon/lib/src/widgets/user_avatar.dart index c19285bc..ce23ce3b 100644 --- a/packages/neon/neon/lib/src/widgets/user_avatar.dart +++ b/packages/neon/neon/lib/src/widgets/user_avatar.dart @@ -25,6 +25,7 @@ class NeonUserAvatar extends StatefulWidget { class _UserAvatarState extends State { late final _userStatusBloc = Provider.of(context, listen: false).getUserStatusesBloc(widget.account); + late double size; @override void initState() { @@ -34,47 +35,50 @@ class _UserAvatarState extends State { } @override - Widget build(final BuildContext context) { - final isDark = Theme.of(context).brightness == Brightness.dark; - final size = (widget.size * MediaQuery.of(context).devicePixelRatio).toInt(); - return Stack( - alignment: Alignment.center, - children: [ - CircleAvatar( - radius: widget.size / 2, - child: ClipOval( - child: NeonCachedApiImage( - account: widget.account, - cacheKey: 'avatar-${widget.username}-${isDark ? 'dark' : 'light'}$size', - download: () async { - if (isDark) { - return widget.account.client.core.getDarkAvatar( - userId: widget.username, - size: size, - ); - } else { - return widget.account.client.core.getAvatar( - userId: widget.username, - size: size, - ); - } - }, - ), - ), - ), - if (widget.showStatus) ...[ - ResultBuilder( - stream: _userStatusBloc.statuses.map((final statuses) => statuses[widget.username]), - builder: _userStatusIconBuilder, - ), - ], - ], - ); - } + Widget build(final BuildContext context) => LayoutBuilder( + builder: (final context, final constraints) { + final isDark = Theme.of(context).brightness == Brightness.dark; + size = constraints.constrain(Size.square(widget.size)).shortestSide; + final pixelSize = (size * MediaQuery.of(context).devicePixelRatio).toInt(); + return Stack( + alignment: Alignment.center, + children: [ + CircleAvatar( + radius: size / 2, + child: ClipOval( + child: NeonCachedApiImage( + account: widget.account, + cacheKey: 'avatar-${widget.username}-${isDark ? 'dark' : 'light'}$pixelSize', + download: () async { + if (isDark) { + return widget.account.client.core.getDarkAvatar( + userId: widget.username, + size: pixelSize, + ); + } else { + return widget.account.client.core.getAvatar( + userId: widget.username, + size: pixelSize, + ); + } + }, + ), + ), + ), + if (widget.showStatus) ...[ + ResultBuilder( + stream: _userStatusBloc.statuses.map((final statuses) => statuses[widget.username]), + builder: _userStatusIconBuilder, + ), + ], + ], + ); + }, + ); Widget _userStatusIconBuilder(final BuildContext context, final Result result) { final hasEmoji = result.data?.icon != null; - final scaledSize = widget.size / (hasEmoji ? 2 : 3); + final scaledSize = size / (hasEmoji ? 2 : 3); Widget? child; Decoration? decoration; @@ -104,7 +108,7 @@ class _UserAvatarState extends State { } return SizedBox.square( - dimension: widget.size, + dimension: size, child: Align( alignment: Alignment.bottomRight, child: Container(