Skip to main content
While Stac provides 70+ built-in widgets, you may need to create custom widgets for your specific use cases. This guide walks you through creating custom StacWidget classes that work seamlessly with Stac’s JSON serialization and parser system.

What is a Custom StacWidget?

A custom StacWidget is a Dart class that:
  • Extends the StacWidget base class
  • Can be serialized to and deserialized from JSON
  • Works with Stac’s parser system to render Flutter widgets
  • Can be used in your /stac folder and deployed to Stac Cloud
Custom widgets enable you to:
  • Wrap third-party Flutter packages
  • Create reusable UI components specific to your app
  • Extend Stac’s functionality beyond built-in widgets
  • Build domain-specific widgets for your business logic

Prerequisites

Before creating a custom widget, ensure you have:
  1. Dependencies: stac and json_annotation packages
  2. Code Generation: build_runner for generating JSON serialization code
  3. Parser: A corresponding parser to render your widget.

Step-by-Step Guide

Step 1: Define Your Widget Class

Create a new file (e.g., lib/widgets/stac_custom_badge.dart) and define your widget class:

Step 2: Required Components

Every custom StacWidget must include:

1. Part File Declaration

This enables code generation for JSON serialization.

2. JsonSerializable Annotation

For nested widgets, use explicitToJson: true:

3. Type Getter

This unique identifier is used in JSON: {"type": "customBadge"}.

4. fromJson Factory Constructor

5. toJson Method

Step 3: Generate Code

Run code generation to create the *.g.dart file:
This generates stac_custom_badge.g.dart with the serialization logic.

Step 4: Create a Parser

To render your widget, create a parser:

Step 5: Register the Parser

Register your parser during Stac initialization:

Advanced Patterns

Using Converters

Stac provides converters for special types. Use them when needed:

DoubleConverter

For double fields that may come as integers in JSON:

StacWidgetConverter

For child widgets in your custom widget:

Using Stac Types

Prefer Stac types over primitive Dart types for consistency:

Using Custom Widgets

In Dart (stac/ folder)
After stac build or stac deploy, your generated json looks like this: