---
title: Interactive Widgets
description: Build Interactive Widgets that call Flutter Code from Native Widgets
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/ABausG/home_widget/llms.txt
> Use this file to discover all available pages before exploring further.

# Interactive Widgets

Android and iOS (starting with iOS 17) allow widgets to have interactive Elements like Buttons.
Using home_widget you can register Dart Callbacks that get invoked from the Native Widgets so you only have to write that logic once.

<center>
<img src="https://github.com/abausg/home_widget/raw/main/packages/home_widget/pub/screenshots/ios-counter.gif"/>
</center>

## Flutter Setup

1. Write a **static** function that takes a Uri as an argument. This will get called when a user clicks on the View
    ```dart
    @pragma("vm:entry-point")
    FutureOr<void> backgroundCallback(Uri? data) async {
      // do something with data
      ...
    }
    ```
   `@pragma('vm:entry-point')` must be placed above the `callback` function to avoid tree shaking in release mode.
   The callback runs in a separate background isolate that finds it only by its handle, so it must be a top-level or static function. Closures and instance methods make `registerInteractivityCallback` throw an `ArgumentError`.

2. Register the callback function by calling
    ```dart
    HomeWidget.registerInteractivityCallback(backgroundCallback);
    ```

## iOS Setup

### CocoaPods

If you manage your iOS dependencies with CocoaPods, adjust your Podfile to add `home_widget` as a dependency to your WidgetExtension:
```rb
target 'YourWidgetExtension' do
   use_frameworks!
   use_modular_headers!

   pod 'home_widget', :path => '.symlinks/plugins/home_widget/ios'
end
```

### Swift Package Manager

If you integrate plugins with Swift Package Manager, make sure your WidgetExtension links against the generated Flutter plugin package:

1. Open your Flutter project's `Runner.xcworkspace` in Xcode and select your WidgetExtension target.
2. On the `General` tab, locate the `Frameworks and Libraries` section and click the `+` button.
   ![Add dependency to the widget target](/assets/swift-package-manager/add-framework.png)
3. In the picker that appears, choose `FlutterGeneratedPluginSwiftPackage` and confirm. If it is not available you need to run `flutter pub get`
   ![Select the FlutterGeneratedPluginSwiftPackage entry](/assets/swift-package-manager/select-generated-flutter-plugin.png)

Once the dependency is present in your widget target, continue with the shared setup steps below.

### Common Steps

1. To be able to use plugins with the Background Callback add this to your AppDelegate's `application` function
   ```swift
   if #available(iOS 17, *) {
    HomeWidgetBackgroundWorker.setPluginRegistrantCallback { registry in
        GeneratedPluginRegistrant.register(with: registry)
    }
   }
   ```
2. Create a custom `AppIntent` in your App Target (Runner) and make sure to select both your App and your WidgetExtension in the Target Membership panel

   ![Target Membership](https://github.com/ABausG/home_widget/blob/main/.github/assets/target_membership.png?raw=true)

   In this Intent you should import `home_widget` and call `HomeWidgetBackgroundWorker.run(url: url, appGroup: appGroup!)` in the perform method. `url` and `appGroup` can be either hardcoded or set as parameters from the Widget
   ```swift
   import AppIntents
   import Flutter
   import Foundation
   import home_widget

   @available(iOS 16, *)
   public struct BackgroundIntent: AppIntent {
      static public var title: LocalizedStringResource = "HomeWidget Background Intent"

      @Parameter(title: "Widget URI")
      var url: URL?

      @Parameter(title: "AppGroup")
      var appGroup: String?

      public init() {}

      public init(url: URL?, appGroup: String?) {
         self.url = url
         self.appGroup = appGroup
      }

      public func perform() async throws -> some IntentResult {
         await HomeWidgetBackgroundWorker.run(url: url, appGroup: appGroup!)

         return .result()
      }
   }
   ```
3. Add a Button to your Widget. This Button might be encapsulated by a Version check. Pass in an instance of the `AppIntent` created in the previous step
   ```swift
   Button(
      intent: BackgroundIntent(
        url: URL(string: "homeWidgetExample://titleClicked"), appGroup: widgetGroupId)
    ) {
      Text(entry.title).bold().font( /*@START_MENU_TOKEN@*/.title /*@END_MENU_TOKEN@*/)
    }.buttonStyle(.plain)
   ```
4. With the current setup the Widget is now Interactive as long as the App is still in the background. If you want to have the Widget be able to wake the App up you need to add the following to your `AppIntent` file
   ```swift
   @available(iOS 16, *)
   @available(iOSApplicationExtension, unavailable)
   extension BackgroundIntent: ForegroundContinuableIntent {}
   ```
   This code tells the system to always perform the Intent in the App and not in a process attached to the Widget. Note however that this will start your Flutter App using the normal main entrypoint meaning your full app might be run in the background. To counter this you should add checks in the very first Widget you build inside `runApp` to only perform necessary calls/setups while the App is launched in the background. The plugin starts its background engine from the callback that was registered earlier, so the callback also runs when the App does not register it again during that launch



## Android Setup

1. Add the necessary Receiver to your `AndroidManifest.xml` file
    ```xml
   <receiver android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver"  android:exported="false" />
    ```
   The receiver does not have to be exported and needs no intent-filter: `HomeWidgetBackgroundIntent.getBroadcast` addresses it by its class, and the launcher sends that `PendingIntent` as your app. With `android:exported="true"` any app on the device can send it a broadcast and run your callback with a Uri of its choice, so change an existing entry to `false`.
2. Create a custom Action
   ```kotlin
   class InteractiveAction : ActionCallback {
        override suspend fun onAction(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
         val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(context, Uri.parse("homeWidgetExample://titleClicked"))
         backgroundIntent.send()
       }
   }
   ```
3. Add the Action as a modifier to a view
   ```kotlin
   Text(
        title,
        style = TextStyle(fontSize = 36.sp, fontWeight = FontWeight.Bold),
        modifier = GlanceModifier.clickable(onClick = actionRunCallback<InteractiveAction>()),
   )
   ```

<Accordion title="Previous version required the Background Service to be registered in the Android Manifest. Starting with version 0.8.1 this is no longer necessary.">
```xml
    <service android:name="es.antonborri.home_widget.HomeWidgetBackgroundService"
        android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
```
</Accordion>

## Several Callbacks

Everything above uses one callback for the whole app. That is enough for most apps, because the Uri can tell the callback what was tapped.

If your app has several interactive Widgets, each of them can have its own callback instead. Register every callback with a `key`, and pass the same key from the native Widget.

```dart
HomeWidget.registerInteractivityCallback(counterCallback, key: 'counter');
HomeWidget.registerInteractivityCallback(todoCallback, key: 'todo');
```

- A callback registered without a `key` is the default callback. It receives every interaction that is sent without a key, and every interaction whose key has no callback registered.
- Registering a second callback with the same key replaces the first one.
- Each callback needs `@pragma('vm:entry-point')`, as before.

### App Group

On iOS the callbacks are stored in the App Group. By default that is the one set with `HomeWidget.setAppGroupId`. You can also name it per call with `appGroupId`, in the same way as for `saveWidgetData`. Registering then works without calling `setAppGroupId` first.

```dart
HomeWidget.registerInteractivityCallback(
  counterCallback,
  key: 'counter',
  appGroupId: 'group.com.example.app',
);
```

Pass the same App Group to `HomeWidgetBackgroundWorker.run` in your `AppIntent`. On Android `appGroupId` has no effect.

<Info>
  Below iOS 17 `registerInteractivityCallback` throws a `PlatformException` with code `-5`, with or without `key` and `appGroupId`, because Widgets cannot be interactive there. Without an App Group it throws one with code `-7`.
</Info>

### iOS

Give your `AppIntent` a parameter for the key and pass it to `HomeWidgetBackgroundWorker.run(url:appGroup:key:)`:

```swift
@Parameter(title: "Callback Key")
var key: String?

public func perform() async throws -> some IntentResult {
   await HomeWidgetBackgroundWorker.run(url: url, appGroup: appGroup!, key: key)

   return .result()
}
```

`run(url:appGroup:)` without a key keeps calling the default callback.

### Android

Pass the key as the third argument of `HomeWidgetBackgroundIntent.getBroadcast`:

```kotlin
val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(
    context,
    Uri.parse("homeWidgetExample://increment"),
    "counter",
)
backgroundIntent.send()
```

In a Glance Widget you do not need an `ActionCallback` of your own for this. The plugin ships `HomeWidgetInteractivityAction`, which reads the Uri and the key from its `ActionParameters` and sends the broadcast:

```kotlin
Text(
    "+",
    modifier = GlanceModifier.clickable(
        actionRunCallback<HomeWidgetInteractivityAction>(
            HomeWidgetInteractivityAction.parameters(
                Uri.parse("homeWidgetExample://increment"),
                "counter",
            )
        )
    ),
)
```

The key is optional there too. Without it the action calls the default callback. If you build the `ActionParameters` yourself, use `HomeWidgetInteractivityAction.UriKey` and `HomeWidgetInteractivityAction.CallbackKey`.

The `HomeWidgetBackgroundReceiver` entry in the `AndroidManifest.xml` is needed in both cases.

## Example Blog Article

A more in depth guide on how to build Interactive Widgets can be found [here](https://medium.com/@ABausG/83cb0706a417)
