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

56 lines
1.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2019-11-02 14:44:26 +01:00
import 'package:git_touch/models/theme.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 {
final String url;
2019-02-03 08:50:17 +01:00
final double size;
2019-12-27 08:06:45 +01:00
final String linkUrl;
2019-12-30 14:25:02 +01:00
final BorderRadius borderRadius;
2019-12-27 08:06:45 +01:00
Avatar({
2019-10-02 09:23:33 +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';
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
2020-02-03 07:41:24 +01:00
? Image.asset(fallback, width: size, height: size)
2020-02-02 15:47:10 +01:00
: FadeInImage.assetNetwork(
placeholder: fallback,
image: url,
width: size,
height: size,
fadeInDuration: Duration(milliseconds: 200),
fadeOutDuration: Duration(milliseconds: 100),
),
2020-01-12 14:39:03 +01:00
);
if (linkUrl == null) return widget;
2020-01-13 14:07:28 +01:00
return Link(
2020-01-12 14:39:03 +01:00
child: widget,
2019-11-02 14:44:26 +01:00
onTap: () {
2020-10-04 14:37:23 +02:00
context.read<ThemeModel>().push(context, linkUrl);
2019-11-02 14:44:26 +01:00
},
);
}
}