What is a Custom StacWidget?
A custom StacWidget is a Dart class that:- Extends the StacWidgetbase class
- Can be serialized to and deserialized from JSON
- Works with Stac’s parser system to render Flutter widgets
- Can be used in your /stacfolder and deployed to Stac Cloud
- 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:- Dependencies: stac_coreandjson_annotationpackages
- Code Generation: build_runnerfor generating JSON serialization code
- 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
2. JsonSerializable Annotation
explicitToJson: true:
3. Type Getter
{"type": "customBadge"}.
4. fromJson Factory Constructor
5. toJson Method
Step 3: Generate Code
Run code generation to create the*.g.dart file:
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
Fordouble 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)stac build or stac deploy, your generated json looks like this: