adding color generation from name in user avatar
parent
ca1e3d5d83
commit
b4a4b6e322
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue