lemmur-app-android/lib/widgets/avatar.dart

71 lines
1.7 KiB
Dart
Raw Normal View History

2021-02-18 09:19:00 +01:00
import 'package:flutter/material.dart';
2021-04-16 20:41:33 +02:00
import 'package:flutter_hooks/flutter_hooks.dart';
2021-02-18 09:19:00 +01:00
2021-04-16 20:41:33 +02:00
import '../hooks/stores.dart';
import '../stores/config_store.dart';
2021-10-21 14:40:28 +02:00
import 'cached_network_image.dart';
2021-04-16 20:41:33 +02:00
/// User's avatar. Respects the `showAvatars` setting from configStore
2021-02-18 09:19:00 +01:00
/// If passed url is null, a blank box is displayed to prevent weird indents
/// Can be disabled with `noBlank`
2021-04-16 20:41:33 +02:00
class Avatar extends HookWidget {
2021-02-18 09:19:00 +01:00
const Avatar({
2022-05-11 22:23:18 +02:00
super.key,
required this.url,
2021-02-18 09:19:00 +01:00
this.radius = 25,
this.noBlank = false,
2021-04-16 20:41:33 +02:00
this.alwaysShow = false,
this.padding = EdgeInsets.zero,
this.onTap,
2022-05-11 22:23:18 +02:00
});
2021-02-18 09:19:00 +01:00
final String? url;
2021-02-18 09:19:00 +01:00
final double radius;
final bool noBlank;
final VoidCallback? onTap;
/// padding is applied unless blank widget is returned
final EdgeInsetsGeometry padding;
2021-02-18 09:19:00 +01:00
2021-04-16 20:41:33 +02:00
/// Overrides the `showAvatars` setting
final bool alwaysShow;
2021-02-18 09:19:00 +01:00
@override
Widget build(BuildContext context) {
2021-04-16 20:41:33 +02:00
final showAvatars =
useStore((ConfigStore store) => store.showAvatars) || alwaysShow;
2021-04-16 20:41:33 +02:00
2021-02-18 09:19:00 +01:00
final blankWidget = () {
if (noBlank) return const SizedBox.shrink();
return SizedBox(
width: radius * 2,
height: radius * 2,
);
}();
final imageUrl = url;
2021-04-16 20:41:33 +02:00
if (imageUrl == null || !showAvatars) {
2021-02-18 09:19:00 +01:00
return blankWidget;
}
return Padding(
padding: padding,
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(radius),
child: ClipOval(
child: CachedNetworkImage(
height: radius * 2,
width: radius * 2,
imageUrl: imageUrl,
fit: BoxFit.cover,
errorBuilder: (_, __) => blankWidget,
),
),
2021-02-18 09:19:00 +01:00
),
);
}
}