Flutter iOS Home Screen Widget With WidgetKit

· 5 min read

A home screen widget keeps your app in front of people without a notification. For a Flutter app on iOS, there is one fact to accept up front: the widget is not Flutter. Apple’s WidgetKit docs say plainly, “You build widgets using SwiftUI.” Your Flutter app’s job is to hand data to the widget and tell it to refresh.

This guide builds a simple widget that shows a value from your Flutter app, using the home_widget package. It is part of our guide to building a Flutter app with Claude Code.

How it fits together

  1. Your Flutter app saves data to shared storage with home_widget.
  2. Shared storage is a UserDefaults suite inside an App Group that both the app and the widget extension belong to.
  3. The SwiftUI widget reads that suite in its timeline provider.
  4. Flutter calls updateWidget, and WidgetKit asks the widget for a new timeline.

home_widget describes itself as “a plugin to make it easier to create HomeScreen Widgets on Android and iOS.” It does not draw the widget. It handles the data and the refresh call.

Step 1: add the widget extension

From the project folder, refresh the iOS config, then open the workspace:

flutter build ios --config-only
open ios/Runner.xcworkspace

In Xcode:

  1. Choose File, New, Target.
  2. Under Application Extension, select Widget Extension and click Next.
  3. Name it, for example StreakWidget.
  4. Clear Include Live Activity and Include Configuration App Intent for a simple static widget.
  5. Click Finish.

Xcode generates a TimelineProvider, a TimelineEntry, a SwiftUI view and the Widget configuration.

Step 2: create and attach an App Group

The app and the extension are separate processes, so they need shared storage.

  1. In your Apple Developer account, create an App Group, for example group.com.example.myapp. The home_widget docs note this needs a paid developer account.
  2. In Xcode, select the Runner target, open Signing & Capabilities, add App Groups and tick your group.
  3. Select the widget extension target and add the same group.

The extension’s bundle ID must start with the app’s bundle ID, for example com.example.myapp.StreakWidget.

Step 3: write data from Flutter

flutter pub add home_widget
import 'package:home_widget/home_widget.dart';

Future<void> initWidget() async {
  await HomeWidget.setAppGroupId('group.com.example.myapp');
}

Future<void> updateStreak(int days) async {
  await HomeWidget.saveWidgetData<int>('streak_days', days);
  await HomeWidget.updateWidget(iOSName: 'StreakWidget');
}

Call setAppGroupId once at startup. The package docs warn that without it, saveWidgetData and getWidgetData return an error. The iOSName must match the kind string in your Swift widget.

Step 4: read the data in SwiftUI

Replace the generated provider with one that reads the App Group:

import WidgetKit
import SwiftUI

struct StreakEntry: TimelineEntry {
    let date: Date
    let days: Int
}

struct Provider: TimelineProvider {
    func placeholder(in context: Context) -> StreakEntry {
        StreakEntry(date: Date(), days: 0)
    }

    func getSnapshot(in context: Context, completion: @escaping (StreakEntry) -> Void) {
        completion(readEntry())
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<StreakEntry>) -> Void) {
        completion(Timeline(entries: [readEntry()], policy: .atEnd))
    }

    private func readEntry() -> StreakEntry {
        let defaults = UserDefaults(suiteName: "group.com.example.myapp")
        let days = defaults?.integer(forKey: "streak_days") ?? 0
        return StreakEntry(date: Date(), days: days)
    }
}

struct StreakWidgetView: View {
    var entry: StreakEntry
    var body: some View {
        VStack {
            Text("\(entry.days)").font(.largeTitle).bold()
            Text("day streak").font(.caption)
        }
        .containerBackground(.fill.tertiary, for: .widget)
    }
}

@main
struct StreakWidget: Widget {
    let kind = "StreakWidget"
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            StreakWidgetView(entry: entry)
        }
        .configurationDisplayName("Streak")
        .description("Your current streak.")
        .supportedFamilies([.systemSmall])
    }
}

The single-entry timeline with an .atEnd policy follows the home_widget docs’ example, “since Flutter handles updates.” If your data changes on a schedule (a countdown, a daily quote), build several entries ahead of time instead. Apple recommends generating “a timeline for as many future dates as possible,” with entries “at least about 5 minutes apart.”

Step 5: run it

Build and run the app from Xcode or with flutter run, open it once, then long-press the home screen and add the widget. Apple notes that “a person must launch the app that contains the widget at least once after the app is installed” before its widget shows in the gallery.

If the build fails with a cycle error mentioning Thin Binary, the home_widget docs give the fix: select the Runner target, open Build Phases and drag “Thin Binary” to the bottom of the list.

Using Flutter visuals in the widget

If you want a chart or custom drawing that already exists in Flutter, home_widget can render a Flutter widget to a PNG in the shared container:

final path = await HomeWidget.renderFlutterWidget(
  const StreakChart(),
  key: 'streak_chart',
  logicalSize: const Size(300, 150),
  pixelRatio: MediaQuery.of(context).devicePixelRatio,
);

The SwiftUI side loads the image from that path with UIImage(contentsOfFile:). Keep simple text and numbers in SwiftUI, and use rendered images only for visuals that would be slow to rebuild natively.

Refresh budget: what to expect

WidgetKit does not refresh on demand forever. Apple explains that each widget gets a daily budget, and “For a widget the user frequently views, a daily budget typically includes from 40 to 70 refreshes,” roughly “every 15 to 60 minutes.” Several cases do not count against it, including when “The widget’s containing app is in the foreground.”

That last point is the useful one for Flutter apps: call updateWidget when the data changes while the app is open, and those reloads are free. For background changes, such as a push notification, expect delays.

Common mistakes

  • Mismatched App Group IDs between Dart, Runner and the extension. The widget shows zeros.
  • iOSName does not match kind. The update call does nothing.
  • Personal team signing. App Groups need a paid account.
  • Expecting minute-by-minute updates. Design for the budget.

Widgets pair well with streaks and reminders. See Flutter local notifications for the reminder side and Flutter deep links with go_router to open the right screen when someone taps the widget. If you are weighing how much native code your app will need overall, Flutter vs native for a solo developer covers that. MobileKit lists widgets among the features worth adding after launch, with this setup as a checklist.

Sources read on the day this post was written: Apple WidgetKit, Apple: creating a widget extension, Apple: keeping a widget up to date, home_widget on pub.dev, home_widget iOS setup and Google codelab: adding a home screen widget to your Flutter app.

Questions

Can I build an iOS home screen widget in Flutter code?

Not the widget itself. Apple's WidgetKit docs say you build widgets using SwiftUI. Flutter sends data to the widget, through the home_widget package and a shared App Group, and can render Flutter widgets to images the widget displays.

How often can an iOS widget refresh?

Apple says a frequently viewed widget typically gets 40 to 70 refreshes in a daily budget, roughly every 15 to 60 minutes. Reloads while your app is in the foreground do not count against the budget.

Why doesn't my widget appear in the widget gallery?

Apple notes that for an app's widget to appear in the gallery, a person must launch the app at least once after installing it.

Try it on your next idea

One payment, every update to this major version, and a refund window if it is not for you. Refund policy.