Getting Started
Install home_widget_generator, write a widget schema, and generate code.
Getting Started
This page walks through setting up home_widget_generator, writing your first widget schema, generating native code, and using the generated Dart helper from your Flutter app.
Add dependencies
Add
home_widgetto your app'spubspec.yaml, and addhome_widget_generatorandhome_widget_clias dev dependencies:dependencies: home_widget: ^0.9.0 dev_dependencies: home_widget_generator: ^0.0.1 home_widget_cli: ^0.0.1Write a widget schema
Create a Dart file describing your widget. By default
home_widget_clipicks up files under a top-levelhome_widget/folder. Here is a minimal schema:import 'package:home_widget_generator/home_widget_generator.dart'; @HomeWidget( name: 'Basic Creation', android: HomeWidgetAndroidConfiguration(), iOS: HomeWidgetIOSConfiguration( groupId: 'group.com.example.app', ), ) class BasicCreation {}The class itself stays empty - it only exists as an attachment point for the annotation. See the Annotation reference for every available parameter, and the Widgets section for the UI primitives you can pass via
widget:.Add data and styling
The
widget:parameter accepts a tree of widgets. See the Widgets section for the available primitives, Data Types for binding values fromhome_widgetstorage, and Styling for colors, text styles and insets.import 'package:home_widget_generator/home_widget_generator.dart'; @HomeWidget( name: 'Greeting', android: HomeWidgetAndroidConfiguration(), iOS: HomeWidgetIOSConfiguration( groupId: 'group.com.example.app', ), widget: HWColumn( crossAxisAlignment: HWCrossAxisAlignment.start, children: [ HWText.fixed( 'Hello', style: HWRoleTextStyle(role: HWTextStyleRole.caption), ), HWText( HWString('name', defaultValue: 'world'), style: HWRoleTextStyle( role: HWTextStyleRole.title, fontWeight: HWFontWeight.bold, ), ), ], ), ) class Greeting {}Every
HWString/HWInt/HWBool/HWDoubleyou reference insidewidget:becomes a typed field on the generated helper, so you can update it without juggling string keys.Generate the code
Run the CLI from your project root:
dart run home_widget_cli generateThe CLI scans for annotated classes and writes the generated Dart helper next to your other source under
lib/src/home_widget/<name>.home_widget.dart, plus the iOS and Android widget sources into the corresponding native folders. See thegeneratecommand for all options.Use the generated Dart helper
Import the generated file and call its static methods. For the schema above, the generator produces a
GreetingHomeWidgetclass with the following static API:import 'src/home_widget/greeting.home_widget.dart'; void main() { runApp(const MyApp()); } Future<void> setName(String name) async { await GreetingHomeWidget.saveData(name: name); await GreetingHomeWidget.updateWidget(); }saveData(...)writes any of the declared data fields throughhome_widgetin a type-safe way. Each field is an optional named parameter, so you can update one or many at a time.getData()reads the current values back as a typed record.deleteData(...)clears the given fields.updateWidget()triggers a refresh of the native widget on both platforms, using the correctandroidName/iOSNameautomatically.
See Sync data for the underlying
home_widgetbehavior.Native setup
No manual native setup is needed: the CLI registers the widget receiver in
AndroidManifest.xml, adds the iOS widget extension to the Xcode project, writes the entitlements file, and bumps the deployment target as required. You only need an App Group ID configured for iOS — provide it via theiOS: HomeWidgetIOSConfiguration(groupId: ...)field on the annotation.