2022-08-26 15:31:42 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:profile/src/widgets/avatar/avatar_style.dart';
|
|
|
|
|
|
|
|
class Avatar extends StatelessWidget {
|
|
|
|
const Avatar({
|
|
|
|
Key? key,
|
|
|
|
this.image,
|
2022-09-19 12:11:58 +02:00
|
|
|
this.firstName,
|
|
|
|
this.lastName,
|
2022-08-26 15:31:42 +02:00
|
|
|
this.avatar,
|
|
|
|
this.style = const AvatarStyle(),
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final Uint8List? image;
|
2022-09-19 12:11:58 +02:00
|
|
|
final String? firstName;
|
|
|
|
final String? lastName;
|
2022-08-26 15:31:42 +02:00
|
|
|
final Widget? avatar;
|
|
|
|
final AvatarStyle style;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
_avatar(),
|
2022-09-19 12:11:58 +02:00
|
|
|
const SizedBox(
|
|
|
|
height: 16,
|
|
|
|
),
|
|
|
|
if (firstName != null || lastName != null)
|
2022-08-26 15:31:42 +02:00
|
|
|
Text(
|
2022-09-19 12:11:58 +02:00
|
|
|
'${firstName ?? ''} ${lastName ?? ''}',
|
2022-08-26 15:31:42 +02:00
|
|
|
style: style.displayNameStyle,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _avatar() {
|
|
|
|
if (avatar != null) {
|
|
|
|
return avatar!;
|
|
|
|
}
|
|
|
|
if (image != null && image!.isNotEmpty) {
|
|
|
|
return Container(
|
|
|
|
width: style.width,
|
|
|
|
height: style.height,
|
2022-09-19 12:11:58 +02:00
|
|
|
decoration: BoxDecoration(
|
2022-08-26 15:31:42 +02:00
|
|
|
shape: BoxShape.circle,
|
2022-09-19 12:11:58 +02:00
|
|
|
image: DecorationImage(
|
|
|
|
image: MemoryImage(image!),
|
|
|
|
fit: BoxFit.fill,
|
|
|
|
),
|
2022-08-26 15:31:42 +02:00
|
|
|
),
|
|
|
|
);
|
2022-09-19 12:11:58 +02:00
|
|
|
} else if (firstName != null || lastName != null) {
|
2022-08-26 15:31:42 +02:00
|
|
|
return Container(
|
|
|
|
width: style.width,
|
|
|
|
height: style.height,
|
|
|
|
decoration: BoxDecoration(
|
2022-09-19 12:11:58 +02:00
|
|
|
color: _generateColorWithIntials(firstName, lastName),
|
2022-08-26 15:31:42 +02:00
|
|
|
shape: BoxShape.circle,
|
|
|
|
),
|
|
|
|
child: Center(
|
|
|
|
child: Text(
|
2022-09-19 12:11:58 +02:00
|
|
|
style: const TextStyle(fontSize: 40),
|
|
|
|
_getInitials(firstName, lastName),
|
2022-08-26 15:31:42 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return SizedBox(
|
|
|
|
width: style.width,
|
|
|
|
height: style.height,
|
|
|
|
// TODO(anyone) child fallback image
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 12:11:58 +02:00
|
|
|
String _getInitials(String? firstName, String? lastName) {
|
|
|
|
return (firstName?[0] ?? '') + (lastName?[0] ?? '');
|
2022-08-26 15:31:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 12:11:58 +02:00
|
|
|
Color _generateColorWithIntials(String? firstName, String? lastName) {
|
|
|
|
var uniqueInitialId = (firstName?.toLowerCase().codeUnitAt(0) ?? 0) +
|
|
|
|
(lastName?.toLowerCase().codeUnitAt(0) ?? 0);
|
2022-08-26 15:31:42 +02:00
|
|
|
|
|
|
|
return Colors.primaries[uniqueInitialId % Colors.primaries.length];
|
|
|
|
}
|
|
|
|
}
|