You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/widgets/avatar.dart

89 lines
2.6 KiB
Dart

5 years ago
import 'package:famedlysdk/famedlysdk.dart';
5 years ago
import 'package:fluffychat/utils/string_color.dart';
5 years ago
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
5 years ago
import 'matrix.dart';
class Avatar extends StatelessWidget {
5 years ago
final Uri mxContent;
5 years ago
final String name;
5 years ago
final double size;
final Function onTap;
5 years ago
static const double defaultSize = 44;
final Client client;
5 years ago
5 years ago
const Avatar(
this.mxContent,
this.name, {
this.size = defaultSize,
this.onTap,
this.client,
5 years ago
Key key,
}) : super(key: key);
5 years ago
@override
Widget build(BuildContext context) {
final src = mxContent?.getThumbnail(
client ?? Matrix.of(context).client,
5 years ago
width: size * MediaQuery.of(context).devicePixelRatio,
height: size * MediaQuery.of(context).devicePixelRatio,
);
5 years ago
var fallbackLetters = '@';
if ((name?.runes?.length ?? 0) >= 2) {
fallbackLetters = String.fromCharCodes(name.runes, 0, 2);
} else if ((name?.runes?.length ?? 0) == 1) {
5 years ago
fallbackLetters = name;
}
final textWidget = Center(
child: Text(
fallbackLetters,
style: TextStyle(
color: Theme.of(context).brightness == Brightness.light
? name?.darkColor
: name?.lightColor ?? Colors.white,
fontSize: 18,
),
),
);
5 years ago
final noPic = mxContent == null || mxContent.toString().isEmpty;
final borderRadius = BorderRadius.circular(size / 2);
return InkWell(
onTap: onTap,
borderRadius: borderRadius,
child: ClipRRect(
borderRadius: borderRadius,
child: Container(
width: size,
height: size,
color: noPic
? Theme.of(context).brightness == Brightness.light
? name?.lightColor
: name?.darkColor ?? Theme.of(context).secondaryHeaderColor
: Theme.of(context).secondaryHeaderColor,
child: noPic
? textWidget
: CachedNetworkImage(
imageUrl: src.toString(),
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,
],
),
),
),
),
5 years ago
);
}
}