genui_gen 0.2.0
genui_gen: ^0.2.0 copied to clipboard
Annotations and runtime helpers to generate genui CatalogItems from your own Flutter widgets. Derive the schema from the constructor so the catalog can never drift.
example/main.dart
// A minimal annotated widget. Run `dart run build_runner build` with
// `genui_gen_builder` in dev_dependencies and add
// `part 'main.genui.dart';` below the imports to get `productCardCatalogItem`.
//
// The complete, runnable app lives in the repository's `example/` directory:
// https://github.com/diegolopezrm/genui_gen/tree/main/example
import 'package:flutter/material.dart';
import 'package:genui/genui.dart';
import 'package:genui_gen/genui_gen.dart';
@GenUiWidget(description: 'A product card with price and optional image.')
class ProductCard extends StatelessWidget {
const ProductCard({
super.key,
required this.title,
required this.price,
this.imageUrl,
this.onTap,
});
/// Product name.
final String title;
/// Price in USD.
final double price;
/// Optional image URL.
final String? imageUrl;
/// Fired when the card is tapped.
final VoidCallback? onTap;
@override
Widget build(BuildContext context) {
return Card(
child: InkWell(
onTap: onTap,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (imageUrl != null) Image.network(imageUrl!),
Text(title),
Text('\$$price'),
],
),
),
);
}
}
/// Registers the generated item next to genui's basic catalog.
///
/// `productCardCatalogItem` is declared by the generated part.
Catalog buildCatalog(CatalogItem productCardCatalogItem) {
return BasicCatalogItems.asCatalog().copyWith(
newItems: [productCardCatalogItem],
);
}