git-touch-android-ios-app/lib/widgets/avatar.dart

64 lines
1.8 KiB
Dart
Raw Normal View History

2021-06-13 20:17:48 +02:00
import 'package:fimber/fimber.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2019-11-02 14:44:26 +01:00
import 'package:git_touch/models/theme.dart';
2022-09-17 14:35:45 +02:00
import 'package:git_touch/utils/utils.dart';
2020-01-13 14:07:28 +01:00
import 'package:git_touch/widgets/link.dart';
2019-11-02 14:44:26 +01:00
import 'package:provider/provider.dart';
2019-12-22 04:11:13 +01:00
class AvatarSize {
static const double extraSmall = 20;
static const double small = 24;
static const double medium = 36;
static const double large = 48;
2020-01-12 07:49:46 +01:00
static const double extraLarge = 64;
2019-12-22 04:11:13 +01:00
}
class Avatar extends StatelessWidget {
2021-05-16 09:16:35 +02:00
final String? url;
2019-02-03 08:50:17 +01:00
final double size;
2021-05-16 09:16:35 +02:00
final String? linkUrl;
final BorderRadius? borderRadius;
2022-09-06 18:28:12 +02:00
const Avatar({
2021-05-16 09:16:35 +02:00
required this.url,
2019-12-27 08:06:45 +01:00
this.size = AvatarSize.medium,
this.linkUrl,
2019-12-30 14:25:02 +01:00
this.borderRadius,
2019-12-27 08:06:45 +01:00
});
2019-12-22 04:11:13 +01:00
@override
Widget build(BuildContext context) {
2021-01-30 08:55:08 +01:00
final theme = Provider.of<ThemeModel>(context);
final fallback = theme.brightness == Brightness.light
? 'images/avatar.png'
: 'images/avatar-dark.png';
2021-06-13 20:17:48 +02:00
final fallbackWidget = Image.asset(fallback, width: size, height: size);
2020-01-12 14:39:03 +01:00
final widget = ClipRRect(
borderRadius: borderRadius ?? BorderRadius.circular(size / 2),
2020-02-02 15:47:10 +01:00
child: url == null
2021-06-13 20:17:48 +02:00
? fallbackWidget
2020-02-02 15:47:10 +01:00
: FadeInImage.assetNetwork(
placeholder: fallback,
2021-05-16 09:16:35 +02:00
image: url!,
2020-02-02 15:47:10 +01:00
width: size,
height: size,
2022-09-06 18:28:12 +02:00
fadeInDuration: const Duration(milliseconds: 200),
fadeOutDuration: const Duration(milliseconds: 100),
2021-06-13 20:17:48 +02:00
imageErrorBuilder: (_, __, ___) {
2022-09-06 18:28:12 +02:00
Fimber.e('image error: ${url!}');
2021-06-13 20:17:48 +02:00
return fallbackWidget;
},
2020-02-02 15:47:10 +01:00
),
2020-01-12 14:39:03 +01:00
);
if (linkUrl == null) return widget;
2021-05-16 09:16:35 +02:00
return LinkWidget(
2020-01-12 14:39:03 +01:00
child: widget,
2019-11-02 14:44:26 +01:00
onTap: () {
2021-05-16 09:16:35 +02:00
context.read<ThemeModel>().push(context, linkUrl!);
2019-11-02 14:44:26 +01:00
},
);
}
}