fl_select_genui 0.8.0
fl_select_genui: ^0.8.0 copied to clipboard
GenUI SDK (A2UI) integration for fl_select: ready-made CatalogItems so AI agents can render fl_select selection components in chat and get selections back as structured query data.
import 'dart:convert';
import 'package:fl_select_genui/fl_select_genui.dart';
import 'package:genui/genui.dart';
import 'package:material_ui/material_ui.dart';
import 'agent_page.dart';
import 'llm_client.dart';
/// A payload exactly like one an AI agent would emit for `Select`
/// (see `FlSelectCatalogItems.systemPromptFragment`).
const agentPayload = '''
{
"delegate": "sideNav",
"selectionMode": "multiple",
"entries": [
{
"type": "category",
"id": "price",
"name": "Price",
"children": [
{"type": "any", "name": "Any"},
{"type": "range", "id": "0-100", "name": "\$0 - \$100", "min": 0, "max": 100},
{"type": "range", "id": "100-300", "name": "\$100 - \$300", "min": 100, "max": 300},
{"type": "custom", "name": "Custom", "min": 0, "max": 1000,
"minHintText": "Min", "maxHintText": "Max"}
]
},
{
"type": "category",
"id": "amenities",
"name": "Amenities",
"children": [
{"type": "text", "id": "wifi", "name": "Wi-Fi"},
{"type": "text", "id": "parking", "name": "Parking"},
{"type": "text", "id": "pool", "name": "Pool"}
]
}
]
}
''';
// Provide these with `--dart-define`, e.g.
// flutter run --dart-define=LLM_API_KEY=sk-... --dart-define=LLM_MODEL=gpt-4o-mini
const _llmApiKey = String.fromEnvironment('LLM_API_KEY');
const _llmBaseUrl = String.fromEnvironment(
'LLM_BASE_URL',
defaultValue: 'https://api.openai.com/v1',
);
const _llmModel = String.fromEnvironment(
'LLM_MODEL',
defaultValue: 'gpt-4o-mini',
);
void main() {
runApp(const FlSelectGenuiExample());
}
class FlSelectGenuiExample extends StatefulWidget {
const FlSelectGenuiExample({super.key});
@override
State<FlSelectGenuiExample> createState() => _FlSelectGenuiExampleState();
}
class _FlSelectGenuiExampleState extends State<FlSelectGenuiExample> {
final DataModel _model = InMemoryDataModel();
late final DataContext _dataContext = DataContext(_model, DataPath('root'));
@override
void dispose() {
_model.dispose();
super.dispose();
}
void _openAgent(BuildContext context) {
if (_llmApiKey.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Run with --dart-define=LLM_API_KEY=<your key>'),
),
);
return;
}
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => AgentPage(
client: ChatCompletionsClient(
apiKey: _llmApiKey,
baseUrl: _llmBaseUrl,
model: _llmModel,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Scaffold(
appBar: AppBar(
title: const Text('fl_select_genui example'),
actions: [
Builder(
builder: (context) => TextButton.icon(
icon: const Icon(Icons.chat_bubble_outline),
label: const Text('Agent chat'),
onPressed: () => _openAgent(context),
),
),
],
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
_Section(
title: '1. Agent payload (AI-generated JSON)',
child: Text(
agentPayload,
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
height: 1.4,
),
),
),
_Section(
title: '2. Rendered Select',
child: Builder(
builder: (buildContext) {
final item = FlSelectCatalogItems.select;
return item.widgetBuilder(
CatalogItemContext(
data: jsonDecode(agentPayload) as JsonMap,
id: 'example',
type: item.name,
buildChild: (_, [_]) => const SizedBox.shrink(),
dispatchEvent: (_) {},
buildContext: buildContext,
dataContext: _dataContext,
getComponent: (_) => null,
getCatalogItem: (_) => null,
surfaceId: 'example-surface',
reportError: (error, _) => debugPrint('$error'),
),
);
},
),
),
_Section(
title: '3. Selection written back to the data model',
child: ValueListenableBuilder(
// Subscribe through the same DataContext the Select writes to;
// DataPath separates segments with "/", not ".".
valueListenable: _dataContext.subscribe<dynamic>(
DataPath('example.value'),
),
builder: (context, value, _) => SelectableText(
value == null
? '(nothing selected yet — tap an option above)'
: const JsonEncoder.withIndent(' ').convert(value),
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
height: 1.4,
),
),
),
),
],
),
),
);
}
}
class _Section extends StatelessWidget {
const _Section({required this.title, required this.child});
final String title;
final Widget child;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Card(
margin: const EdgeInsets.only(bottom: 16),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: theme.textTheme.titleSmall),
const SizedBox(height: 8),
child,
],
),
),
);
}
}