About create-catalog-item
A development skill that guides implementation of GenUI CatalogItem components from a json_schema_builder Schema. It specifies the data class, JSON parsing, CatalogItem wiring, and Flutter widget behavior, including schema-defined actions.
Key features
- 01Generate a data class from schema properties
- 02Parse JSON with typed fields and error handling
- 03Wire a schema-backed CatalogItem
- 04Build a Flutter widget for displayed fields
- 05Resolve and dispatch schema actions
Use cases
- 01Implement a GenUI CatalogItem from a Schema
- 02Add action handling to a schema-defined component
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ flutter/ genui --skill create-catalog-itemAfter installing, type/create-catalog-itemin Claude Code to confirm it loaded.
Details
From the project READMECreate CatalogItem
Goal
To correctly implement a GenUI CatalogItem based on a provided json_schema_builder Schema, including its corresponding data class, CatalogItem instance, and Widget class. This ensures the AI model can properly generate and interact with the UI component.
Instructions
When tasked with creating a CatalogItem from a Schema, follow these steps:
-
Create the Data Class:
- Name it
_<SchemaName>Data(e.g., if schema ismyCardSchema, data class is_MyCardData). - Add final fields for each property defined in the schema.
- Create a
factory _<SchemaName>Data.fromJson(Map<String, Object?> json)method. - Use a
try-catchblock to parse the properties and return a new instance. - Cast each property from the
jsonmap to its expected type, e.g.,title: json['title'] as String,oraction: json['action'] as JsonMap?,. - Throw an
Exception('Invalid JSON for _<SchemaName>Data')in thecatchblock if an error occurs.
- Name it
-
Create the CatalogItem Instance:
- Name it identical to the schema name but without the "Schema" suffix (e.g.,
myCardformyCardSchema). - Declare as a
final CatalogItem. - Set
nameto the capitalized version of the name (e.g.,'MyCard'). - Set
dataSchemato the provided schema. - Implement the
widgetBuilder: (itemContext):- Cast
itemContext.datatoMap<String, Object?>. - Parse the data using the data class
fromJsonmethod:_<SchemaName>Data.fromJson(json). - Return the corresponding Widget class and pass the required data.
- If the schema includes an action callback (like
onCompleted), implement it here. You must parse the action context usingresolveContextand dispatch an event usingitemContext.dispatchEvent(...).
- Cast
- Name it identical to the schema name but without the "Schema" suffix (e.g.,
-
Create the Widget Class:
- Name it
_<CapitalizedSchemaName>(e.g.,_MyCard). - Inherit from
StatelessWidgetorStatefulWidgetdepending on state requirements. - Add the Data Class as a required property (e.g.,
final _<SchemaName>Data data;). - Add any required callback properties (e.g.,
final void Function(int) onCompleted;). - Implement the
buildmethod using Flutter Material components (e.g., Card, Column, Text). Make sure each data field in the data class is displayed, and that actions are represented by buttons or other interactive elements.
- Name it
Examples
Input Schema
final basicCardSchema = S.object(
properties: {
'component': S.string(enumValues: ['BasicCard']),
'title': S.string(),
'description':
'action': A2uiSchemas.action(),
},
required: ['title'],
);
Expected Output
class _BasicCardData {
final String title;
final JsonMap? action;
_BasicCardData({required this.title, this.action});
factory _BasicCardData.fromJson(Map<String, Object?> json) {
try {
return _BasicCardData(
title: json['title'] as String,
action: json['action'] as JsonMap?,
);
} catch (e) {
throw Exception('Invalid JSON for _BasicCardData: $e');
}
}
}
final basicCard = CatalogItem(
name: 'BasicCard',
dataSchema: basicCardSchema,
widgetBuilder: (itemContext) {
final json = itemContext.data as Map<String, Object?>;
final data = _BasicCardData.fromJson(json);
return _BasicCard(
data: data,
onTap: () async {
final action = data.action;
if (action == null) return;
final event = action['event'] as JsonMap?;
final name = (event?['name'] as String?) ?? '';
final JsonMap contextDefinition =
(event?['context'] as JsonMap?) ?? <String, Object?>{};
final JsonMap resolvedContext = await resolveContext(
itemContext.dataContext,
contextDefinition,
);
itemContext.dispatchEvent(
UserActionEvent(
name: name,
sourceComponentId: itemContext.id,
context: resolvedContext,
),
);
}
);
},
);
class _BasicCard extends StatelessWidget {
final _BasicCardData data;
final VoidCallback onTap;
const _BasicCard({super.key, required this.data, required this.onTap});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(data.title),
onTap: onTap,
),
);
}
}
Constraints
- Ensure proper use of
try-catchblocks and type casting when parsing JSON infromJson. - Make sure action resolution accurately fetches variables via
resolveContextand usesitemContext.dispatchEventwhen actions are present in the Schema.