2020-01-01 19:10:13 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2020-09-07 17:08:01 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/utils/string_color.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'matrix.dart';
|
|
|
|
|
|
|
|
class Avatar extends StatelessWidget {
|
2021-11-19 20:38:16 +01:00
|
|
|
final Uri? mxContent;
|
|
|
|
final String? name;
|
2020-01-01 19:10:13 +01:00
|
|
|
final double size;
|
2021-11-19 20:38:16 +01:00
|
|
|
final void Function()? onTap;
|
2020-03-13 21:42:05 +01:00
|
|
|
static const double defaultSize = 44;
|
2021-11-19 20:38:16 +01:00
|
|
|
final Client? client;
|
2021-11-13 18:56:34 +01:00
|
|
|
final double fontSize;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-11-20 10:42:23 +01:00
|
|
|
const Avatar({
|
2020-03-13 21:42:05 +01:00
|
|
|
this.mxContent,
|
2021-11-20 10:42:23 +01:00
|
|
|
this.name,
|
2020-03-13 21:42:05 +01:00
|
|
|
this.size = defaultSize,
|
|
|
|
this.onTap,
|
2021-01-20 20:27:09 +01:00
|
|
|
this.client,
|
2021-11-13 18:56:34 +01:00
|
|
|
this.fontSize = 18,
|
2021-11-19 20:38:16 +01:00
|
|
|
Key? key,
|
2020-03-13 21:42:05 +01:00
|
|
|
}) : 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 = '@';
|
2021-11-19 20:38:16 +01:00
|
|
|
final name = this.name;
|
|
|
|
if (name != null) {
|
|
|
|
if (name.runes.length >= 2) {
|
|
|
|
fallbackLetters = String.fromCharCodes(name.runes, 0, 2);
|
|
|
|
} else if (name.runes.length == 1) {
|
|
|
|
fallbackLetters = name;
|
|
|
|
}
|
2020-01-18 13:22:22 +01:00
|
|
|
}
|
2021-07-31 22:17:18 +02:00
|
|
|
final noPic = mxContent == null ||
|
|
|
|
mxContent.toString().isEmpty ||
|
|
|
|
mxContent.toString() == 'null';
|
2020-10-17 09:29:27 +02:00
|
|
|
final textWidget = Center(
|
|
|
|
child: Text(
|
|
|
|
fallbackLetters,
|
|
|
|
style: TextStyle(
|
2022-07-12 19:39:18 +02:00
|
|
|
color: noPic ? Colors.white : null,
|
2021-11-13 18:56:34 +01:00
|
|
|
fontSize: fontSize,
|
2020-10-17 09:29:27 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2020-10-28 07:35:38 +01:00
|
|
|
final borderRadius = BorderRadius.circular(size / 2);
|
2022-07-08 10:41:36 +02:00
|
|
|
final container = Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border.all(color: Theme.of(context).dividerColor),
|
|
|
|
borderRadius: borderRadius,
|
|
|
|
),
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: borderRadius,
|
|
|
|
child: Container(
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
color:
|
|
|
|
noPic ? name?.lightColor : Theme.of(context).secondaryHeaderColor,
|
|
|
|
child: noPic
|
|
|
|
? textWidget
|
|
|
|
: CachedNetworkImage(
|
|
|
|
imageUrl: src.toString(),
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
placeholder: (c, s) => textWidget,
|
|
|
|
errorWidget: (c, s, d) => Stack(
|
|
|
|
children: [
|
|
|
|
textWidget,
|
|
|
|
],
|
2020-10-25 18:36:47 +01:00
|
|
|
),
|
2022-07-08 10:41:36 +02:00
|
|
|
),
|
2020-10-17 09:29:27 +02:00
|
|
|
),
|
2020-01-17 11:08:12 +01:00
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
);
|
2022-07-08 10:41:36 +02:00
|
|
|
if (onTap == null) return container;
|
|
|
|
return InkWell(
|
|
|
|
onTap: onTap,
|
|
|
|
borderRadius: borderRadius,
|
|
|
|
child: container,
|
|
|
|
);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
}
|