4
2
Fork 0

adding color generation from name in user avatar

pull/31/head
Sudharshan S. 2019-02-17 19:51:51 +08:00
parent ca1e3d5d83
commit b4a4b6e322
Signed by: sudharshan
GPG Key ID: C861C97AAF3D9559
1 changed files with 19 additions and 2 deletions

View File

@ -18,8 +18,12 @@ class UserAvatar extends StatelessWidget {
padding: padding,
child: Stack(alignment: Alignment.bottomRight, children: <Widget>[
CircleAvatar(
backgroundColor: Colors.brown.shade800,
child: Text(user.firstName[0] + user.lastName[0]),
backgroundColor: _stringToColor(user.lastName),
child: Text(
user.firstName[0].toUpperCase() +
user.lastName[0].toUpperCase(),
style: Theme.of(context).accentTextTheme.title,
),
radius: radius),
/*active
? Container(
@ -31,4 +35,17 @@ class UserAvatar extends StatelessWidget {
: Container(),*/
]));
}
// Hashing username into a pastel color
Color _stringToColor(String str) {
int hash = 0;
str.runes.forEach((int rune) {
hash = rune + ((hash << 5) - hash);
});
hash = hash % 360;
return HSLColor.fromAHSL(1.0, hash.toDouble(), 0.8, 0.4).toColor();
}
}