colorFor static method
- BuildContext context,
- String name
Deterministically picks a stable background color for name from the
theme's tonal palette.
The same name always maps to the same color, so avatars stay visually stable across rebuilds, sessions and devices.
Implementation
static Color colorFor(BuildContext context, String name) {
final scheme = Theme.of(context).colorScheme;
final index = name.trim().toLowerCase().hashCode.abs() % 6;
switch (index) {
case 0:
return scheme.primaryContainer;
case 1:
return scheme.secondaryContainer;
case 2:
return scheme.tertiaryContainer;
case 3:
return scheme.surfaceContainerHighest;
case 4:
return Color.alphaBlend(
scheme.primary.withValues(alpha: 0.24),
scheme.surfaceContainerHighest,
);
default:
return Color.alphaBlend(
scheme.tertiary.withValues(alpha: 0.24),
scheme.surfaceContainerHighest,
);
}
}