Skip to main content

Image & SVG

Display raster images from assets, files, or the network, and render SVG content with flexible sizing.

Image

Display a decoded image:

widgets.Image{
Source: myImage, // image.Image
Width: 200,
Height: 150,
Fit: widgets.ImageFitCover,
}

Image Properties

PropertyTypeDescription
Sourceimage.ImageDecoded image to render
Widthfloat64Display width (zero uses intrinsic width)
Heightfloat64Display height (zero uses intrinsic height)
FitImageFitScaling mode: Contain (default), Fill, Cover, None, ScaleDown
Alignmentlayout.AlignmentPosition within bounds (default: center)
SemanticLabelstringAccessibility description
ExcludeFromSemanticsboolHide from screen readers (for decorative images)

NetworkImage

Load and display an image from a URL with automatic caching and fade-in:

widgets.NetworkImage{
URL: "https://example.com/avatar.jpg",
Width: 200,
Height: 200,
Fit: widgets.ImageFitCover,
}

While loading, a CircularProgressIndicator is shown. On failure, a fallback error message is displayed. Both are customizable:

widgets.NetworkImage{
URL: "https://example.com/photo.jpg",
Width: 300,
Height: 200,
Placeholder: widgets.Center{Child: widgets.Text{Content: "Loading..."}},
ErrorBuilder: func(err error) core.Widget {
return widgets.Center{Child: widgets.Text{Content: "Failed to load"}}
},
}

Authenticated Requests

Pass headers for APIs that require authorization. Requests with different headers are cached independently:

widgets.NetworkImage{
URL: "https://api.example.com/user/avatar",
Width: 48,
Height: 48,
Fit: widgets.ImageFitCover,
Headers: map[string]string{"Authorization": "Bearer " + token},
}

NetworkImage Properties

PropertyTypeDescription
URLstringHTTP(S) image URL
Headersmap[string]stringOptional HTTP headers (e.g., authorization)
Widthfloat64Display width (zero uses intrinsic width)
Heightfloat64Display height (zero uses intrinsic height)
FitImageFitScaling mode (default: Cover)
Alignmentlayout.AlignmentPosition within bounds
Placeholdercore.WidgetWidget shown while loading (default: spinner)
ErrorBuilderfunc(error) core.WidgetWidget builder for load failures
FadeDuration*time.DurationFade-in duration (default: 300ms, nil pointer for default, zero to disable)
Loader*image.LoaderCustom loader (default: shared global loader)
SemanticLabelstringAccessibility description

Caching

NetworkImage uses a two-tier cache by default:

  • Memory: LRU cache (100 entries, 100 MB) for instant display of recently viewed images
  • Disk: Filesystem cache (500 MB) under the platform cache directory for persistence across app restarts

Images are decoded from PNG, JPEG, GIF, and WebP formats. Concurrent requests for the same URL and headers are deduplicated into a single HTTP request.

To evict or clear the cache programmatically:

loader := image.DefaultLoader()
loader.Evict("https://example.com/avatar.jpg") // Remove one URL
loader.ClearCache() // Clear everything

SvgImage

Renders an SVG with flexible sizing:

widgets.SvgImage{
Source: myIcon,
Width: 120,
Height: 80,
TintColor: colors.Primary, // Optional tint
}

SvgImage Properties

PropertyTypeDescription
Source*svg.IconLoaded SVG icon
Widthfloat64Display width
Heightfloat64Display height
TintColorgraphics.ColorOptional tint color

SvgIcon

A convenience wrapper around SvgImage for square icons:

widgets.SvgIcon{
Source: myIcon,
Size: 24,
TintColor: colors.OnSurface,
}

SvgIcon Properties

PropertyTypeDescription
Source*svg.IconLoaded SVG icon
Sizefloat64Width and height (square)
TintColorgraphics.ColorOptional tint color

Caching Static SVGs

For static SVG assets (logos, icons), cache loaded icons so rebuilds reuse the same underlying SVG DOM:

var svgCache = svg.NewIconCache()

func loadIcon(name string) *svg.Icon {
icon, err := svgCache.Get(name, func() (*svg.Icon, error) {
f, err := assetFS.Open("assets/" + name)
if err != nil {
return nil, err
}
defer f.Close()
return svg.Load(f)
})
if err != nil {
return nil
}
return icon
}