flutter_android_widgets

Installation

Prerequisites

  • Flutter >=3.10.0
  • Dart >=3.0.0
  • An existing Flutter project targeting Android

1. Add dependencies

# pubspec.yaml
dependencies:
  flutter_android_widgets: ^0.0.1

dev_dependencies:
  build_runner: ^2.4.0
flutter pub get

2. Add manifest markers

Open android/app/src/main/AndroidManifest.xml and add two marker comments inside <application>:

<application ...>
    <activity ...>
        <!-- your existing activity -->
    </activity>

    <!-- flutter_android_widgets:start -->
    <!-- flutter_android_widgets:end -->
</application>

The generator injects all <receiver> entries between these markers. They must be spelled exactly as above (spaces included) and must be inside <application>.


3. Define your first widget

Create a Dart file anywhere under lib/ (e.g. lib/my_widgets.dart):

import 'package:flutter_android_widgets/flutter_android_widgets.dart';

final myWidget = AndroidWidget(
  info: WidgetInfo(
    widgetClassName: 'MyWidgetProvider',
    widgetName: 'My Widget',
    minWidth: 250,
    minHeight: 100,
    updateInterval: Duration(hours: 1),
  ),
  layout: WColumn(
    backgroundColor: '#1A1A2E',
    padding: 16,
    children: [
      WText('\${title}', textSize: 18, bold: true, textColor: '#FFFFFF'),
      WText('\${subtitle}', textSize: 12, textColor: '#AAAAAA'),
      WButton(label: 'Refresh', actionKey: 'refresh'),
    ],
  ),
  dataKeys: ['title', 'subtitle'],
);

Rules for detection:

  • The variable must be final or const (not var, not inside a function)
  • The type must be AndroidWidget
  • The file must be under lib/ (not bin/, not test/)

4. Run the generator

dart run build_runner build

You will see output like:

[INFO] Found 1 AndroidWidget(s) in lib/my_widgets.dart
[INFO] Writing res/layout/widget_my_widget_provider.xml
[INFO] Writing res/xml/appwidget_provider_my_widget_provider.xml
[INFO] Writing kotlin/.../MyWidgetProvider.kt
[INFO] Patching AndroidManifest.xml

Then build and run your app:

flutter run

Long-press your home screen → Widgets → find "My Widget" → add it.


Generated files

FilePurpose
res/layout/widget_my_widget_provider.xmlRemoteViews-compatible XML layout
res/xml/appwidget_provider_my_widget_provider.xmlWidget size, update interval, resize mode
kotlin/.../MyWidgetProvider.ktFull AppWidgetProvider with data bindings
AndroidManifest.xml (patched)<receiver> entry injected between markers
FlutterAndroidWidgetsChannel.ktMethodChannel for live update triggering
MainActivity.kt (patched)Channel registration

All generated files are deterministic — safe to commit to version control and re-run at any time.


Updating dependencies

To upgrade to a newer version of the package, update pubspec.yaml then re-run:

flutter pub get
dart run build_runner build --delete-conflicting-outputs

On this page

No Headings