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

58 lines
1.4 KiB
Dart
Raw Normal View History

2021-02-18 09:19:00 +01:00
import 'package:cached_network_image/cached_network_image.dart';
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';
/// 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({
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-02-18 09:19:00 +01:00
}) : super(key: key);
final String? url;
2021-02-18 09:19:00 +01:00
final double radius;
final bool noBlank;
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 =
useConfigStoreSelect((configStore) => configStore.showAvatars) ||
alwaysShow;
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 ClipOval(
child: CachedNetworkImage(
height: radius * 2,
width: radius * 2,
imageUrl: imageUrl,
2021-02-18 09:19:00 +01:00
fit: BoxFit.cover,
errorWidget: (_, __, ___) => blankWidget,
),
);
}
}