FurryChat/lib/components/avatar.dart

60 lines
1.7 KiB
Dart
Raw Normal View History

2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-01-18 12:22:22 +00:00
import 'package:fluffychat/utils/string_color.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2020-04-08 10:38:52 +00:00
import 'package:flutter_advanced_networkimage/provider.dart';
2020-01-01 18:10:13 +00:00
import 'matrix.dart';
class Avatar extends StatelessWidget {
2020-04-28 12:11:56 +00:00
final Uri mxContent;
2020-01-18 12:22:22 +00:00
final String name;
2020-01-01 18:10:13 +00:00
final double size;
final Function onTap;
2020-03-13 20:42:05 +00:00
static const double defaultSize = 44;
2020-01-01 18:10:13 +00:00
2020-03-13 20:42:05 +00:00
const Avatar(
this.mxContent,
this.name, {
this.size = defaultSize,
this.onTap,
Key key,
}) : super(key: key);
2020-01-01 18:10:13 +00:00
@override
Widget build(BuildContext context) {
2020-05-13 13:58:59 +00:00
var thumbnail = mxContent?.getThumbnail(
2020-01-01 18:10:13 +00:00
Matrix.of(context).client,
width: size * MediaQuery.of(context).devicePixelRatio,
height: size * MediaQuery.of(context).devicePixelRatio,
method: ThumbnailMethod.scale,
);
2020-05-13 13:58:59 +00:00
final src = thumbnail;
var fallbackLetters = '@';
2020-01-18 12:22:22 +00:00
if ((name?.length ?? 0) >= 2) {
fallbackLetters = name.substring(0, 2);
} else if ((name?.length ?? 0) == 1) {
fallbackLetters = name;
}
2020-05-01 10:14:49 +00:00
final noPic = mxContent == null || mxContent.toString().isEmpty;
return InkWell(
onTap: onTap,
child: CircleAvatar(
radius: size / 2,
2020-05-01 10:14:49 +00:00
backgroundImage: !noPic
2020-05-15 19:50:44 +00:00
? AdvancedNetworkImage(
src,
useDiskCache: !kIsWeb,
)
: null,
2020-05-01 10:14:49 +00:00
backgroundColor: noPic
2020-01-19 14:07:42 +00:00
? name?.color ?? Theme.of(context).secondaryHeaderColor
: Theme.of(context).secondaryHeaderColor,
2020-05-01 10:14:49 +00:00
child: noPic
2020-01-18 12:22:22 +00:00
? Text(fallbackLetters, style: TextStyle(color: Colors.white))
: null,
),
2020-01-01 18:10:13 +00:00
);
}
}