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';
|
2021-10-31 12:52:58 +01:00
|
|
|
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({
|
2021-04-09 00:11:44 +02:00
|
|
|
Key? 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,
|
2021-11-03 22:50:03 +01:00
|
|
|
this.padding = EdgeInsets.zero,
|
|
|
|
this.onTap,
|
2021-02-18 09:19:00 +01:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
final String? url;
|
2021-02-18 09:19:00 +01:00
|
|
|
final double radius;
|
|
|
|
final bool noBlank;
|
2021-11-03 22:50:03 +01:00
|
|
|
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 =
|
2021-10-31 12:52:58 +01:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
}();
|
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:50:03 +01:00
|
|
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|