English
Avatar: avatars
Build avatars with Avatar. This page includes a runnable example, all props, and behavior constraints.
Complete example
Complete environment setup first. Copy this complete main.go and run go run ..
go
package main
import (
"github.com/dxui-org/dxui"
"image"
"image/color"
"log"
)
func main() {
pixels := image.NewRGBA(image.Rect(0, 0, 48, 48))
for y := 0; y < 48; y++ {
for x := 0; x < 48; x++ {
pixels.SetRGBA(x, y, color.RGBA{
R: 40,
G: 160,
B: 120,
A: 255,
})
}
}
source := dxui.ImageFromGo(pixels)
app := dxui.NewApp(dxui.AppOptions{
Title: "Avatar",
Width: 640,
Height: 480,
Background: dxui.RGBA(248, 250, 252, 255),
})
if err := app.Run(func() dxui.View {
return dxui.Box(
dxui.BoxProps{
Gap: 16,
Style: dxui.Style{
Padding: dxui.Padding(24),
},
},
dxui.Avatar(dxui.AvatarProps{
Source: source,
Shape: dxui.AvatarCircle,
Size: 64,
}),
)
}); err != nil {
log.Fatal(err)
}
}Parameters and API
go
func Avatar(props AvatarProps) View| Field | Type | Purpose, defaults, and constraints |
|---|---|---|
Key | string | Stable unique identity within one parent; empty by default. Use business IDs in dynamic lists, not changing indices. |
Style | Style | Local layout, paint, and text styling; zero uses intrinsic sizes and theme defaults. |
Token | ComponentToken | Component theme entry; empty selects its default. |
States | StateStyles | Paint patches for actual interaction states; cannot fabricate state or change layout. |
Pointer | PointerBehavior | Defaults to PointerAuto; PointerNone excludes the entire subtree from pointer participation, unlike Disabled. |
Source | ImageSource | Stable image handle; reuse it rather than allocating during every build. |
Shape | AvatarShape | Defaults to AvatarCircle; AvatarSquare selects a square. |
Size | float32 | Logical size; 0 uses the theme default. |
OnLoad | func(Size) | Success notification with decoded size; nil still loads. |
OnError | func(error) | Resource error notification; the app may render a fallback. Nil still attempts loading. |
Behavior and limitations
Shares ImageSource and OnLoad/OnError, with centered Cover fitting. Shape defaults to AvatarCircle; AvatarSquare is optional. Size=0 uses the theme default of 40; positive values are logical units, overridden by explicit Style dimensions. No URLs, initials, automatic fallback, or status dots. Render fallback content in response to OnError.
All components share identity, style, and pointer rules. See full declarations for referenced enums, structs, and comments. Running examples explains build verification.