fluffychat/lib/components/avatar.dart

82 lines
2.3 KiB
Dart
Raw Normal View History

2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-18 13:22:22 +01:00
import 'package:fluffychat/utils/string_color.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2020-09-07 17:08:01 +02:00
import 'package:cached_network_image/cached_network_image.dart';
2020-01-01 19:10:13 +01:00
import 'matrix.dart';
class Avatar extends StatelessWidget {
2020-04-28 14:11:56 +02:00
final Uri mxContent;
2020-01-18 13:22:22 +01:00
final String name;
2020-01-01 19:10:13 +01:00
final double size;
final Function onTap;
2020-03-13 21:42:05 +01:00
static const double defaultSize = 44;
2020-01-01 19:10:13 +01:00
2020-03-13 21:42:05 +01:00
const Avatar(
this.mxContent,
this.name, {
this.size = defaultSize,
this.onTap,
Key key,
}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2020-05-13 15:58:59 +02:00
var thumbnail = mxContent?.getThumbnail(
2020-01-01 19:10:13 +01:00
Matrix.of(context).client,
width: size * MediaQuery.of(context).devicePixelRatio,
height: size * MediaQuery.of(context).devicePixelRatio,
);
2020-05-13 15:58:59 +02:00
final src = thumbnail;
var fallbackLetters = '@';
2020-10-19 16:56:40 +02:00
if ((name?.runes?.length ?? 0) >= 2) {
fallbackLetters = String.fromCharCodes(name.runes, 0, 2);
2020-10-19 16:56:40 +02:00
} else if ((name?.runes?.length ?? 0) == 1) {
2020-01-18 13:22:22 +01:00
fallbackLetters = name;
}
final textWidget = Center(
child: Text(
fallbackLetters,
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
);
2020-05-01 12:14:49 +02:00
final noPic = mxContent == null || mxContent.toString().isEmpty;
return InkWell(
onTap: onTap,
child: ClipRRect(
borderRadius: BorderRadius.circular(size / 2),
child: Container(
width: size,
height: size,
color: noPic
? name?.lightColor ?? Theme.of(context).secondaryHeaderColor
: Theme.of(context).secondaryHeaderColor,
child: noPic
? textWidget
: CachedNetworkImage(
imageUrl: src,
fit: BoxFit.cover,
width: size,
height: size,
placeholder: (c, s) => Stack(
children: [
Center(child: CircularProgressIndicator(strokeWidth: 2)),
textWidget,
],
),
errorWidget: (c, s, d) => Stack(
children: [
textWidget,
],
),
),
),
),
2020-01-01 19:10:13 +01:00
);
}
}