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.0flutter pub get2. 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
finalorconst(notvar, not inside a function) - The type must be
AndroidWidget - The file must be under
lib/(notbin/, nottest/)
4. Run the generator
dart run build_runner buildYou 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.xmlThen build and run your app:
flutter runLong-press your home screen → Widgets → find "My Widget" → add it.
Generated files
| File | Purpose |
|---|---|
res/layout/widget_my_widget_provider.xml | RemoteViews-compatible XML layout |
res/xml/appwidget_provider_my_widget_provider.xml | Widget size, update interval, resize mode |
kotlin/.../MyWidgetProvider.kt | Full AppWidgetProvider with data bindings |
AndroidManifest.xml (patched) | <receiver> entry injected between markers |
FlutterAndroidWidgetsChannel.kt | MethodChannel 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