Skills cover SDK integration, building flows and paywalls, pre-publish audits, placement migration, and more. Describe the task in plain language and your agent picks the skill.
Follow this recipe to display the user’s progress as a Lottie animation — for example, a ring that fills up to indicate their daily streak. Your app renders the animation as a native Lottie view in a custom element.
Before you start
Update your app to Adapty Flutter SDK 4.4 or later. Earlier SDK versions show the fallback content for every custom element.
Set up the native code of your app for custom elements, as in the Flutter guide.
On iOS, add Lottie to the Runner target in Xcode with Swift Package Manager, from https://github.com/airbnb/lottie-spm.git.
On Android, set up Jetpack Compose in your app module. The animation view in this recipe is a composable. Add Compose Foundation and Lottie for Jetpack Compose to your app with Gradle, from androidx.compose.foundation:foundation and com.airbnb.android:lottie-compose.
Prepare a Lottie animation in .json format, or download the sample streak_ring.json. The app plays the animation from the start and stops it in proportion to the progress, such as halfway through for 50%. To show the exact progress, the animation needs:
No progress on its first frame, and full progress on its last frame.
A steady speed, with no easing. If the animation starts slowly and then speeds up, it shows less than half of the progress halfway through.
1. Add the custom element to the screen
In the Flow & Paywall Builder, add a custom element to a flow screen, and set it up for the animation:
Type: lottie_animation.
ID: streak_ring. In this recipe, the ID is also the name of the animation file.
Height: Keep the default Fixed height. The animation scales to fit the custom element.
Inside the custom element, add an Image with a still picture of the ring. Users see it as the fallback when their version of your app doesn’t play the animation.
2. Add the animation file to your app
Your native code reads the animation file from your iOS and Android projects, not from the Flutter assets folder:
iOS: Add the file to the Runner target in Xcode.
Android: Put the file in android/app/src/main/assets, the assets folder of your Android app module. Keep it at the root of this folder, not in a subfolder.
The file name without .json must match the ID of the custom element, such as streak_ring.json for streak_ring.
3. Create the animation view
Create a model for the streak, and a view that plays the animation. Your app updates the model when the streak changes. The view reads the model, so the ring shows the current streak when the screen opens:
import Lottieimport SwiftUI// The streak that your app keeps up to date@MainActorfinal class StreakStore: ObservableObject { @Published var currentStreak = 0 // days in a row}struct StreakRingView: View { let animation: LottieAnimation @ObservedObject var streakStore: StreakStore // 7 days fill the ring: from 0 for an empty ring to 1 for a full one private var progress: AnimationProgressTime { min(AnimationProgressTime(streakStore.currentStreak) / 7, 1) } var body: some View { LottieView(animation: animation) .resizable() .playing(.fromProgress(0, toProgress: progress, loopMode: .playOnce)) }}
.playing(.fromProgress(0, toProgress: progress, loopMode: .playOnce)) fills the ring once, from empty up to progress.
import androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.runtime.Composableimport androidx.compose.runtime.collectAsStateimport androidx.compose.runtime.getValueimport androidx.compose.ui.Modifierimport com.airbnb.lottie.compose.LottieAnimationimport com.airbnb.lottie.compose.LottieClipSpecimport com.airbnb.lottie.compose.LottieCompositionSpecimport com.airbnb.lottie.compose.rememberLottieCompositionimport kotlinx.coroutines.flow.MutableStateFlow// the streak that your app keeps up to dateclass StreakStore { val currentStreak = MutableStateFlow(0) // days in a row}@Composablefun StreakRing(animationFile: String, streakStore: StreakStore) { val composition by rememberLottieComposition(LottieCompositionSpec.Asset(animationFile)) val streak by streakStore.currentStreak.collectAsState() // 7 days fill the ring: from 0 for an empty ring to 1 for a full one val progress = minOf(streak / 7f, 1f) LottieAnimation( composition = composition, modifier = Modifier.fillMaxSize(), // scale the animation to fit the custom element clipSpec = LottieClipSpec.Progress(0f, progress), )}
clipSpec = LottieClipSpec.Progress(0f, progress) fills the ring once, from empty up to progress.
4. Match the animation to the custom element
In your app-wide resolver, add a property for the streak model. Then return the animation view for the lottie_animation Type and the streak_ring ID. The highlighted lines show what to add or change. If your app has no app-wide resolver yet, copy the whole code:
import AdaptyUIBuilderimport Lottieimport SwiftUIstruct CustomElementsResolver: AdaptyUICustomElementsResolver { let streakStore: StreakStore func view( forType type: String, id: String, context: AdaptyUICustomElementContext ) -> AnyView? { switch type { case "lottie_animation" where id == "streak_ring": guard let animation = LottieAnimation.named(id) else { return nil // no animation file: show the fallback } return AnyView(StreakRingView(animation: animation, streakStore: streakStore)) // the other cases of your resolver default: return nil } }}
Put the streak_ring case before any other lottie_animation case, because Swift only runs the first case that matches. If the app bundle has no file for the ID, the resolver returns nil, and users see the fallback.
import android.content.Contextimport com.adapty.ui.AdaptyCustomElementContentimport com.adapty.ui.AdaptyCustomElementsResolverimport java.io.IOExceptionclass CustomElementsResolver( context: Context, private val streakStore: StreakStore,) : AdaptyCustomElementsResolver { // the animation files in the assets folder, listed once private val animationFiles: Set<String> = try { context.assets.list("").orEmpty().toSet() } catch (e: IOException) { emptySet() // no list of files: every Lottie element shows the fallback } override fun resolve(type: String, id: String): AdaptyCustomElementContent? = when (type) { "lottie_animation" -> { // the ID names the animation file, such as streak_ring.json val file = "$id.json" when { file !in animationFiles -> null // no file for this ID: show the fallback id == "streak_ring" -> AdaptyCustomElementContent.compose { StreakRing(file, streakStore) } // the other animations of your resolver else -> null } } // the other Types of your resolver else -> null }}
If the assets folder has no file for the ID, the resolver returns null, and users see the fallback. If your resolver already returns a view for every animation file, return that view instead of null when the ID isn’t streak_ring. This way, users still see the animations of your other lottie_animation custom elements.
Create the resolver with the streak model, and set it as the app-wide resolver at app launch:
import AdaptyUIimport Flutterimport UIKit@main@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate { // the streak model that your app updates let streakStore = StreakStore() override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { AdaptyUI.setCustomElementsResolver(CustomElementsResolver(streakStore: streakStore)) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) }}
import android.app.Applicationimport com.adapty.ui.AdaptyUIclass App : Application() { // the streak model that your app updates val streakStore = StreakStore() override fun onCreate() { super.onCreate() AdaptyUI.setCustomElementsResolver(CustomElementsResolver(this, streakStore)) }}
Your Dart code keeps the streak, and the native views read it from the streak model. To update the model, send the streak to your native code through a platform channel. In your iOS and Android code, listen to the channel:
In the didInitializeImplicitFlutterEngine method of your AppDelegate class, add the highlighted lines:
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) let streakChannel = FlutterMethodChannel( name: "custom_elements/streak", binaryMessenger: engineBridge.applicationRegistrar.messenger() ) streakChannel.setMethodCallHandler { [streakStore] call, result in guard call.method == "setStreak", let days = call.arguments as? Int else { return result(FlutterMethodNotImplemented) } // the channel calls this handler on the main thread MainActor.assumeIsolated { streakStore.currentStreak = days } result(nil) }}
Note
The AppDelegate code in this recipe follows the iOS template of Flutter 3.41 and later, which adopts FlutterImplicitEngineDelegate. If your AppDelegate class doesn’t adopt it, leave out the protocol and the didInitializeImplicitFlutterEngine method. Create the channel in application(_:didFinishLaunchingWithOptions:) instead, before return super.application(...), with the messenger of the root view controller. Keep the GeneratedPluginRegistrant call of your template:
In your MainActivity class, override the configureFlutterEngine method:
import io.flutter.embedding.android.FlutterFragmentActivityimport io.flutter.embedding.engine.FlutterEngineimport io.flutter.plugin.common.MethodChannelclass MainActivity : FlutterFragmentActivity() { override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) val streakStore = (application as App).streakStore MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "custom_elements/streak") .setMethodCallHandler { call, result -> if (call.method == "setStreak") { streakStore.currentStreak.value = call.arguments as Int result.success(null) } else { result.notImplemented() } } }}
In your Dart code, send the streak at app launch, and each time the streak changes:
import 'package:flutter/services.dart';// your iOS and Android code listens to this channelconst streakChannel = MethodChannel('custom_elements/streak');// call at app launch, and each time the streak changesFuture<void> sendStreak(int days) async { await streakChannel.invokeMethod('setStreak', days);}
6. Test on a device
The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. To preview the animation:
Publish the flow. To see the latest published version instead of a cached one, register the device as a test device.
Run a build of your app that includes the resolver. Set the streak to 0 days, open the screen, and verify that the ring stays empty. Repeat with 4 and 7 days, and verify that the ring fills up to each value.
To test the fallback, remove the animation file from the build, and open the screen again.
To find out why the fallback appears instead of the animation, follow the instructions in the Test on a device section of the Flutter guide.