2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.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;
|
2020-01-17 11:08:12 +01:00
|
|
|
final Function onTap;
|
2020-03-13 21:42:05 +01:00
|
|
|
static const double defaultSize = 44;
|
2021-01-20 20:27:09 +01:00
|
|
|
final Client client;
|
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,
|
2021-01-20 20:27:09 +01:00
|
|
|
this.client,
|
2020-03-13 21:42:05 +01:00
|
|
|
Key key,
|
|
|
|
}) : super(key: key);
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-05-01 08:14:58 +02:00
|
|
|
final src = mxContent?.getThumbnail(
|
2021-01-20 20:27:09 +01:00
|
|
|
client ?? Matrix.of(context).client,
|
2020-01-01 19:10:13 +01:00
|
|
|
width: size * MediaQuery.of(context).devicePixelRatio,
|
|
|
|
height: size * MediaQuery.of(context).devicePixelRatio,
|
|
|
|
);
|
2020-05-13 15:58:59 +02:00
|
|
|
var fallbackLetters = '@';
|
2020-10-19 16:56:40 +02:00
|
|
|
if ((name?.runes?.length ?? 0) >= 2) {
|
2020-10-17 09:29:27 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-10-17 09:29:27 +02:00
|
|
|
final textWidget = Center(
|
|
|
|
child: Text(
|
|
|
|
fallbackLetters,
|
|
|
|
style: TextStyle(
|
2021-07-31 20:12:18 +02:00
|
|
|
color: Colors.white,
|
2020-10-17 09:29:27 +02:00
|
|
|
fontSize: 18,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2021-07-31 20:12:18 +02:00
|
|
|
final noPic = mxContent == null ||
|
|
|
|
mxContent.toString().isEmpty ||
|
|
|
|
mxContent.toString() == 'null';
|
2020-10-28 07:35:38 +01:00
|
|
|
final borderRadius = BorderRadius.circular(size / 2);
|
2020-01-17 11:08:12 +01:00
|
|
|
return InkWell(
|
|
|
|
onTap: onTap,
|
2020-10-28 07:35:38 +01:00
|
|
|
borderRadius: borderRadius,
|
2020-10-17 09:29:27 +02:00
|
|
|
child: ClipRRect(
|
2020-10-28 07:35:38 +01:00
|
|
|
borderRadius: borderRadius,
|
2020-10-17 09:29:27 +02:00
|
|
|
child: Container(
|
|
|
|
width: size,
|
|
|
|
height: size,
|
2021-07-31 20:12:18 +02:00
|
|
|
color: name?.color,
|
2020-10-17 09:29:27 +02:00
|
|
|
child: noPic
|
|
|
|
? textWidget
|
|
|
|
: CachedNetworkImage(
|
2021-04-21 14:19:54 +02:00
|
|
|
imageUrl: src.toString(),
|
2020-10-17 09:29:27 +02:00
|
|
|
fit: BoxFit.cover,
|
|
|
|
width: size,
|
|
|
|
height: size,
|
2021-06-20 14:32:12 +02:00
|
|
|
placeholder: (c, s) => textWidget,
|
2020-10-25 18:36:47 +01:00
|
|
|
errorWidget: (c, s, d) => Stack(
|
|
|
|
children: [
|
|
|
|
textWidget,
|
|
|
|
],
|
|
|
|
),
|
2020-10-17 09:29:27 +02:00
|
|
|
),
|
|
|
|
),
|
2020-01-17 11:08:12 +01:00
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|