It’s simple to create a custom actions in Beagle Flutter, you need to:
Map<String, ActionHandler>
;BeagleSdk.init
.This struct tells Beagle which action to trigger when a given identifier comes from a JSON.
The String key of the map is the _beagleAction_
property in JSON that identifies the action type. You can name it whatever you like, as long as the prefix is custom:
.
The ActionHandler
value of the map is a function that receives a:
BeagleAction
;BeagleView
;BeagleUIElement
;BuildContext
.It follows the strcut void Function({BeagleAction action, BeagleView view, BeagleUIElement element, BuildContext context})
. These parameters are available because they provide all the information needed to trigger an event, such as context and the action coming from JSON.
Follow these steps to learn how to create and use your own custom actions:
You have to define the identifier. Here, let’s name it custom:log
.
Create your action regardless of Beagle implementation. For this example we used a simple function, see below:
void debug(String message) {
debugPrint(message);
}
Create the struct that it will pass to Beagle:
Map<String, ActionHandler> myCustomActions = {
'custom:log': ({action, view, element, context}) {
debug(action.getAttributeValue('message'));
}
};
For Beagle to be able to trigger your action, pass the map you just created through the Beagle init method:
BeagleSdk.init(
actions: myCustomActions,
);
That’s it! Now you can use your custom action with Beagle Flutter!
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.