Respond to button actions in Flutter SDK
If you are building flows or paywalls using the Adapty builder, it’s crucial to set up buttons properly:
- Add a button in the builder and assign it either a pre-existing action or create a custom action ID.
- Write code in your app to handle each action you’ve assigned.
This guide shows how to handle custom and pre-existing actions in your code.
Closing the view and opening URLs are handled automatically by the default flowViewDidPerformAction implementation, and the SDK itself processes purchases and restorations. All the other button actions, such as logging in or opening another flow, require implementing proper responses in the app code. Note that responding to completed purchases and restores happens in the required observer callbacks — see Handle flow & paywall events.
Close flows and paywalls
To add a button that will close your flow or paywall, in the builder, add a button and assign it the Close action. No code is required: the default flowViewDidPerformAction implementation dismisses the view when it receives CloseAction.
The Android system Back button no longer closes the view by default. It is delivered to flowViewDidPerformAction as AndroidSystemBackAction — handle it yourself if you want the back button to close the flow or paywall.
Override flowViewDidPerformAction if you need custom behavior — for example, to also close the view on the Android system back button, as in v3:
void flowViewDidPerformAction(AdaptyUIFlowView view, AdaptyUIAction action) {
switch (action) {
case const CloseAction():
case const AndroidSystemBackAction():
view.dismiss();
break;
case OpenUrlAction(:final url, :final openIn):
AdaptyUI().openUrl(url, openIn: openIn);
break;
default:
break;
}
}Overriding flowViewDidPerformAction replaces the default implementation entirely — keep the CloseAction and OpenUrlAction cases if you want to preserve the default closing and URL-opening behavior.
Open URLs from flows and paywalls
If you want to add a group of links (e.g., terms of use and purchase restoration), add a Link element in the builder and handle it the same way as buttons with the Open URL action.
To add a button that opens a link (e.g., Terms of use or Privacy policy), in the builder, add a button, assign it the Open URL action, and enter the URL you want to open.
No code is required: the default flowViewDidPerformAction implementation opens the URL natively via AdaptyUI().openUrl, honoring the in-app or external browser setting from the dashboard.
The default behavior is enough for most cases. If you still want to open URLs yourself, override the handler:
void flowViewDidPerformAction(AdaptyUIFlowView view, AdaptyUIAction action) {
switch (action) {
case const CloseAction():
view.dismiss();
break;
case OpenUrlAction(url: final url):
// Open the URL in whatever way fits your app
break;
default:
break;
}
}Log into the app
To add a button that logs users into your app:
- In the builder, add a button and assign it the Login action.
- In your app code, implement a handler for the
loginaction that identifies your user.
void flowViewDidPerformAction(AdaptyUIFlowView view, AdaptyUIAction action) {
switch (action) {
case CustomAction(action: 'login'):
// Navigate to your login screen in whatever way fits your app
break;
default:
break;
}
}Handle custom actions
To add a button that handles any other actions:
- In the builder, add a button, assign it the Custom action, and assign it an ID.
- In your app code, implement a handler for the action ID you’ve created.
For example, if you have another set of subscription offers or one-time purchases, you can add a button that will display another flow or paywall:
void flowViewDidPerformAction(AdaptyUIFlowView view, AdaptyUIAction action) {
switch (action) {
case CustomAction(action: 'openNewPaywall'):
// Display another flow or paywall
break;
default:
break;
}
}If you are building paywalls using the Adapty paywall builder, it’s crucial to set up buttons properly:
- Add a button in the paywall builder and assign it either a pre-existing action or create a custom action ID.
- Write code in your app to handle each action you’ve assigned.
This guide shows how to handle custom and pre-existing actions in your code.
Only purchases and restorations are handled automatically. All the other button actions, such as closing paywalls or opening links, require implementing proper responses in the app code.
Close paywalls
To add a button that will close your paywall:
- In the paywall builder, add a button and assign it the Close action.
- In your app code, implement a handler for the
CloseActionandAndroidSystemBackActionactions.
In the Flutter SDK, the CloseAction and AndroidSystemBackAction actions trigger closing the paywall by default. However, you can override this behavior in your code if needed. For example, closing one paywall might trigger opening another.
void paywallViewDidPerformAction(AdaptyUIPaywallView view, AdaptyUIAction action) {
switch (action) {
case const CloseAction():
case const AndroidSystemBackAction():
view.dismiss();
break;
default:
break;
}
}Open URLs from paywalls
If you want to add a group of links (e.g., terms of use and purchase restoration), add a Link element in the paywall builder and handle it the same way as buttons with the Open URL action.
To add a button that opens a link from your paywall (e.g., Terms of use or Privacy policy):
- In the paywall builder, add a button, assign it the Open URL action, and enter the URL you want to open.
- In your app code, implement a handler for the
openUrlaction that opens the received URL in a browser.
// You have to install url_launcher plugin in order to handle urls:
// https://pub.dev/packages/url_launcher
import 'package:url_launcher/url_launcher.dart';
void paywallViewDidPerformAction(AdaptyUIPaywallView view, AdaptyUIAction action) {
switch (action) {
case OpenUrlAction(url: final url):
final Uri uri = Uri.parse(url);
launchUrl(uri, mode: LaunchMode.inAppBrowserView);
break;
default:
break;
}
}Log into the app
To add a button that logs users into your app:
- In the paywall builder, add a button and assign it the Login action.
- In your app code, implement a handler for the
loginaction that identifies your user.
void paywallViewDidPerformAction(AdaptyUIPaywallView view, AdaptyUIAction action) {
switch (action) {
case CustomAction(action: 'login'):
// Navigate to your login screen in whatever way fits your app
break;
default:
break;
}
}Handle custom actions
To add a button that handles any other actions:
- In the paywall builder, add a button, assign it the Custom action, and assign it an ID.
- In your app code, implement a handler for the action ID you’ve created.
For example, if you have another set of subscription offers or one-time purchases, you can add a button that will display another paywall:
void paywallViewDidPerformAction(AdaptyUIPaywallView view, AdaptyUIAction action) {
switch (action) {
case CustomAction(action: 'openNewPaywall'):
// Display another paywall
break;
default:
break;
}
}