What is a Custom StacAction?
A custom StacAction is a Dart class that:- Extends the StacActionbase class
- Can be serialized to and deserialized from JSON
- Works with Stac’s action parser system to execute behaviors
- Can be triggered from widgets in your JSON definitions
- Integrate third-party services and APIs
- Implement business-specific logic
- Extend Stac’s action functionality beyond built-in actions
- Create reusable behaviors for your application
Prerequisites
Before creating a custom action, ensure you have:- Dependencies: stac_coreandjson_annotationpackages
- Code Generation: build_runnerfor generating JSON serialization code
- Parser: A corresponding action parser to execute your action.
Step-by-Step Guide
Step 1: Define Your Action Class
Create a new file (e.g.,lib/actions/stac_share_action.dart) and define your action class:
Step 2: Required Components
Every custom StacAction must include:1. Part File Declaration
2. JsonSerializable Annotation
explicitToJson: true:
3. Action Type Getter
{"actionType": "share"}.
4. Constructor
StacAction constructor handles JSON data internally.
5. fromJson Factory Constructor
6. toJson Method
Step 3: Generate Code
Run code generation to create the*.g.dart file:
stac_share_action.g.dart with the serialization logic.
Step 4: Create an Action Parser
To execute your action, create an action parser:Step 5: Register the Action Parser
Register your action parser during Stac initialization:Advanced Patterns
Using Converters
Similar to widgets, actions can use converters for special types:DoubleConverter
Fordouble fields that may come as integers in JSON:
Using Custom Actions
In Dart (stac/ folder)stac build or stac deploy, your generated json looks like this:
onPressed, onTap, and other callback properties. For example: