CamoAvatar
Concept
1. Why
Avatars represent a person or entity with a photo, initials or an icon, in lists, top bars, chips and comments. Fluent has Avatar and Persona. Material and Apple don't have a named component but use avatars everywhere. Getting the fallbacks right (a broken image shows initials, and initials get a stable color) is exactly the boilerplate apps repeat.
2. Shape
Material mapping preview: there's no M3 component. The Material skin draws:
- a circle;
- initials in
titleMedium; - a background picked from the brand/tone containers by a hash of the name;
- sizes S 24, M 40, L 56.
3. API
Contract
fun CamoAvatar(
name: String? = null, // used for initials, color and the accessible name
image: Widget? = null, // the app's image widget (Coil/Kamel AsyncImage, Flutter Image), loaded by the app
size: AvatarSize = Medium, // Small | Medium | Large (or a custom Dp via the theme)
shape: AvatarShape? = null, // null → AvatarTheme.shape; Circle | Rounded | Square
presence: Presence? = null, // Online | Away | Busy | Offline → a small status dot
)
fun CamoAvatarGroup(avatars: List<Widget>, max: Int = 3) // overlapping stack with "+N"
The image is a widget (the same rule as icons, ADR-12): the app passes its own image-loading widget. Core doesn't depend on an image library. If the image fails or is null, the initials show.
State → renderer: AvatarState(initials?, colorSeed, size, shape, presence?). Slots: AvatarSlots(image?).
Tokens used
- brand/status container colors for the initials background, and their on-colors
typography.labelLarge(S),titleMedium(M),titleLarge(L)shapes.full(Circle),AvatarTheme.roundedShape(Rounded, defaultshapes.small),shapes.none(Square)success/warning/error/outlinefor the presence dot
Component theme
theme.components.avatar: AvatarTheme?. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.
| Field | Type | Material default | What it's for |
|---|---|---|---|
shape | AvatarShape | Circle | the avatar shape for the whole brand (ADR-28; Carbon: Square) |
roundedShape | Shape | shapes.small | corners of Rounded avatars |
sizeSmall / sizeMedium / sizeLarge | Dp | 24 / 40 / 56 | sizes |
initialsPalette | List of color pairs | the 5 container families (primary, secondary, tertiary, info, success) | backgrounds picked by a stable hash of name |
presenceSize | Dp | 10 | status dot |
groupOverlap | Dp | 8 | overlap in groups |
Accessibility
- The accessible name is
name("Dwiki Riyadi"), plus the presence ("online") when set. With no name, it's decorative. - In lists, the avatar merges into the row (the row already says the name, so the avatar is decorative there, which the list item handles).
4. Build steps
- Initials extraction (first letters of the first and last words; handles single words and emoji safely), a stable color hash, the presence dot.
CamoAvatar,CamoAvatarGroup, and the renderers.- Tests: "Dwiki Riyadi" → "DR"; the same name always gets the same color; a failed image shows the initials; "+2" for 5 avatars with max 3.
Done when
- A contacts list with photos, initials and presence renders identically on every platform.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Names in non-Latin scripts (CJK, Arabic) | The first grapheme of the name. No two-letter split for scripts without word spaces. |
| Emoji or empty names | The icon fallback. |
| Image loading state | The initials show until the image widget reports success. The app's image widget decides the placeholder behavior. |
AvatarThemegainstextStyleSmall / Medium / Large,neutralColors(the person icon and a group's "+N"),presenceColors(one per presence, plus thering),ringWidth,personIconandstrings(the presence words, "2 more").AvatarState.colorSeedis the 32-bit FNV-1a hash of the name's UTF-8 bytes,nullfor the icon and the "+N"; both libraries test the same vectors ("Dwiki Riyadi" →0xCDC853E9).- Initials need a letter: a name that starts with an emoji, a digit or a symbol gets the icon. Both platforms split graphemes with the same small rules (combining marks, variation selectors, skin tones, zero-width joins, flags).
- In a
CamoListItem's leading slot the avatar is decorative: the list item marks its slot, so the row says the name once. CamoAvatarGroupgainssize(for the "+N").- Minimal: neutral circles, 24 / 36 / 48. Fluent: brand-tinted initials, 24 / 32 / 48.
Implementation
1. Why
Avatars show an image, or initials when there's no image or it fails. Core doesn't depend on an image library (ADR-12), so the app passes its own image composable (Coil 3's AsyncImage is multiplatform). Kotlin's trick for the fallback: draw the initials underneath and the image on top, so a missing or failed image simply leaves the initials visible.
2. Shape
3. API
@Composable
public fun CamoAvatar(
modifier: Modifier = Modifier,
name: String? = null,
image: (@Composable () -> Unit)? = null, // e.g. { AsyncImage(url, contentDescription = null, contentScale = ContentScale.Crop) }
size: AvatarSize = AvatarSize.Medium,
shape: AvatarShape? = null, // null → AvatarTheme.shape
presence: Presence? = null,
)
@Composable public fun CamoAvatarGroup(avatars: List<@Composable () -> Unit>, modifier: Modifier = Modifier, max: Int = 3)
- Initials: the first grapheme of the first and last words of
name("Dwiki Riyadi"→ "DR"), uppercased with the locale. Graphemes, not chars, so emoji and combined characters stay whole (a smallBreakIterator-free splitter in core, sincejava.textisn't in common code). - Color: picked from
style.initialsPaletteby a stable FNV-1a hash ofname, the same on every platform (notString.hashCode(), which isn't guaranteed identical across Kotlin targets). - Clipping:
Modifier.clip(shape)on the stack; the presence dot is placed outside the clip with asurface-colored ring. - Semantics: the avatar is one node with
contentDescription = name(plus presence, "Dwiki Riyadi, online"); the image composable's own description is cleared. - Group: a
Rowwith negative spacing-style.groupOverlap, each avatar with a ring, and a "+N" avatar for the rest.
4. Build steps
AvatarTheme/Style(withshape,roundedShape),CamoAvatar,CamoAvatarGroup, the DebugSkin renderer.commonTest: initials for one word, two words, emoji names; the same color for the same name on JVM and native tests; a null image shows initials; presence in the description; the "+N" count.
Done when
- A contact list with photos, broken URLs and no photos looks right in every shape, and each row announces the person once.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Image loading slowly | Initials show until the image paints over them (no flicker to blank). |
No name and no image | A generic person icon from the skin. |
| Transparent PNG | Initials show through; apps with transparent logos set a background in their image composable. |
avatarInitials(name)andavatarColorSeed(name): UIntare public;AvatarStyle.colorsFor(state),sizeOf,textStyleOfandshapeOfresolve the rest.- The list item marks its leading slot with an internal composition local; the avatar then sets no description.