fluffychat/lib/components/avatar.dart

58 lines
1.6 KiB
Dart
Raw Normal View History

2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.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-04-08 12:38:52 +02:00
import 'package:flutter_advanced_networkimage/provider.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;
final Function onTap;
2020-03-13 21:42:05 +01:00
static const double defaultSize = 44;
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,
Key key,
}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
final String src = mxContent.getThumbnail(
Matrix.of(context).client,
width: size * MediaQuery.of(context).devicePixelRatio,
height: size * MediaQuery.of(context).devicePixelRatio,
method: ThumbnailMethod.scale,
);
2020-01-18 13:22:22 +01:00
String fallbackLetters = "@";
if ((name?.length ?? 0) >= 2) {
fallbackLetters = name.substring(0, 2);
} else if ((name?.length ?? 0) == 1) {
fallbackLetters = name;
}
return InkWell(
onTap: onTap,
child: CircleAvatar(
radius: size / 2,
2020-04-28 14:11:56 +02:00
backgroundImage: mxContent != null
2020-04-10 18:00:45 +02:00
? AdvancedNetworkImage(
src,
2020-04-10 18:52:40 +02:00
useDiskCache: !kIsWeb,
2020-04-10 18:00:45 +02:00
)
: null,
2020-04-28 14:11:56 +02:00
backgroundColor: mxContent == null
2020-01-19 15:07:42 +01:00
? name?.color ?? Theme.of(context).secondaryHeaderColor
: Theme.of(context).secondaryHeaderColor,
2020-04-28 14:11:56 +02:00
child: mxContent == null
2020-01-18 13:22:22 +01:00
? Text(fallbackLetters, style: TextStyle(color: Colors.white))
: null,
),
2020-01-01 19:10:13 +01:00
);
}
}