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

83 lines
2.1 KiB
Dart

6 years ago
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
6 years ago
import 'package:fluffychat/utils/string_color.dart';
import 'package:fluffychat/widgets/mxc_image.dart';
6 years ago
class Avatar extends StatelessWidget {
final Uri? mxContent;
final String? name;
6 years ago
final double size;
final void Function()? onTap;
5 years ago
static const double defaultSize = 44;
final Client? client;
final double fontSize;
6 years ago
const Avatar({
5 years ago
this.mxContent,
this.name,
5 years ago
this.size = defaultSize,
this.onTap,
this.client,
this.fontSize = 18,
Key? key,
5 years ago
}) : super(key: key);
6 years ago
@override
Widget build(BuildContext context) {
5 years ago
var fallbackLetters = '@';
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;
}
6 years ago
}
final noPic = mxContent == null ||
mxContent.toString().isEmpty ||
mxContent.toString() == 'null';
final textWidget = Center(
child: Text(
fallbackLetters,
style: TextStyle(
color: noPic ? Colors.white : null,
fontSize: fontSize,
),
),
);
final borderRadius = BorderRadius.circular(size / 2);
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
: MxcImage(
uri: mxContent,
fit: BoxFit.cover,
width: size,
height: size,
placeholder: (_) => textWidget,
),
),
),
6 years ago
);
if (onTap == null) return container;
return InkWell(
onTap: onTap,
borderRadius: borderRadius,
child: container,
);
6 years ago
}
}