### Update System Instruction for New Widget Source: https://pub.dev/packages/genui/versions/0.5 Shows how to update the `systemInstruction` for a `ContentGenerator` to inform the agent about a new widget. This example instructs the agent to use the `RiddleCard` to generate riddles. ```dart final contentGenerator = FirebaseAiContentGenerator( systemInstruction: ''' You are an expert in creating funny riddles. Every time I give you a word, you should generate a RiddleCard that displays one new riddle related to that word. Each riddle should have both a question and an answer. ''', tools: _genUiManager.getTools(), ); ``` -------------------------------- ### Configure GenUI Logging Source: https://pub.dev/packages/genui/versions/0.5 Provides an example of how to configure logging for the GenUI package in the `main` method. This allows observing communication between the app and the agent by setting the log level and listening to log records. ```dart import 'package:logging/logging.dart'; import 'package:genui/genui.dart'; final logger = configureGenUiLogging(level: Level.ALL); void main() async { logger.onRecord.listen((record) { debugPrint('${record.loggerName}: ${record.message}'); }); // Additional initialization of bindings and Firebase. } ``` -------------------------------- ### Add genui Dependency to Flutter Project Source: https://pub.dev/packages/genui/versions/0.5.0/install This command adds the genui package as a dependency to your Flutter project. It automatically updates your pubspec.yaml file and runs `flutter pub get`. Ensure you have the Flutter SDK installed and configured. ```bash flutter pub add genui ``` -------------------------------- ### Create a CatalogItem Widget Source: https://pub.dev/packages/genui/versions/0.5 Defines a `CatalogItem` which represents a type of widget the agent can generate. It combines a name, a schema, and a builder function to produce the widget. This example demonstrates creating a `RiddleCard` widget. ```dart final riddleCard = CatalogItem( name: 'RiddleCard', dataSchema: _schema, widgetBuilder: ({ required data, required id, required buildChild, required dispatchEvent, required context, required dataContext, }) { final json = data as Map; final question = json['question'] as String; final answer = json['answer'] as String; return Container( constraints: const BoxConstraints(maxWidth: 400), decoration: BoxDecoration(border: Border.all()), padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(question, style: Theme.of(context).textTheme.headlineMedium), const SizedBox(height: 8.0), Text(answer, style: Theme.of(context).textTheme.headlineSmall), ], ), ); }, ); ``` -------------------------------- ### Create a CatalogItem Widget (Dart) Source: https://pub.dev/packages/genui/index Defines a CatalogItem, which represents a widget type for UI generation. It combines a name, a data schema, and a builder function to create the widget. This example shows how to create a 'RiddleCard' widget. ```dart final riddleCard = CatalogItem( name: 'RiddleCard', dataSchema: _schema, widgetBuilder: ({ required data, required id, required buildChild, required dispatchEvent, required context, required dataContext, }) { final json = data as Map; final question = json['question'] as String; final answer = json['answer'] as String; return Container( constraints: const BoxConstraints(maxWidth: 400), decoration: BoxDecoration(border: Border.all()), padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(question, style: Theme.of(context).textTheme.headlineMedium), const SizedBox(height: 8.0), Text(answer, style: Theme.of(context).textTheme.headlineSmall), ], ), ); }, ); ``` -------------------------------- ### Initialize Firebase in Flutter App Source: https://pub.dev/packages/genui/versions/0.5 Demonstrates the required initialization steps for Firebase within a Flutter application's main method, ensuring that widget bindings are set up before initializing Firebase. ```dart void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); runApp(const MyApp()); } ``` -------------------------------- ### Flutter: Initialize GenUI Components for Agent Interaction Source: https://pub.dev/packages/genui/versions/0.5 This Dart code demonstrates the initialization of GenUI components within a Flutter widget's state. It covers creating a GenUiManager with a widget catalog, a FirebaseAiContentGenerator with system instructions and tools, and a GenUiConversation to manage interactions. Ensure necessary imports for GenUiManager, CoreCatalogItems, FirebaseAiContentGenerator, and GenUiConversation are present. ```dart class _MyHomePageState extends State { late final GenUiManager _genUiManager; late final GenUiConversation _genUiConversation; @override void initState() { super.initState(); // Create a GenUiManager with a widget catalog. // The CoreCatalogItems contain basic widgets for text, markdown, and images. _genUiManager = GenUiManager(catalog: CoreCatalogItems.asCatalog()); // Create a ContentGenerator to communicate with the LLM. // Provide system instructions and the tools from the GenUiManager. final contentGenerator = FirebaseAiContentGenerator( systemInstruction: ''' You are an expert in creating funny riddles. Every time I give you a word, you should generate UI that displays one new riddle related to that word. Each riddle should have both a question and an answer. ''', tools: _genUiManager.getTools(), ); // Create the GenUiConversation to orchestrate everything. _genUiConversation = GenUiConversation( genUiManager: _genUiManager, contentGenerator: contentGenerator, onSurfaceAdded: _onSurfaceAdded, // Added in the next step. onSurfaceDeleted: _onSurfaceDeleted, // Added in the next step. ); } @override void dispose() { _textController.dispose(); _genUiConversation.dispose(); super.dispose(); } } ``` -------------------------------- ### Update System Instruction for New Widget (Dart) Source: https://pub.dev/packages/genui/index Shows how to update the system instruction for the AI content generator to include the new widget. This ensures the agent knows to use the custom widget, referencing it by its CatalogItem name. ```dart final contentGenerator = FirebaseAiContentGenerator( systemInstruction: ''' You are an expert in creating funny riddles. Every time I give you a word, you should generate a RiddleCard that displays one new riddle related to that word. Each riddle should have both a question and an answer. ''', tools: _genUiManager.getTools(), ); ``` -------------------------------- ### Initialize Firebase in main() for GenUI Source: https://pub.dev/packages/genui/index This code demonstrates the necessary steps to initialize Firebase in your Flutter application's main method before running the app. This is a prerequisite for using Firebase AI Logic with the genui package. ```dart void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); runApp(const MyApp()); } ``` -------------------------------- ### GenUI Conversation Management Flow Source: https://pub.dev/packages/genui/versions/0.5 Illustrates the lifecycle of a user interaction within the GenUI framework, showing how user input is processed, sent to the AI, and results in UI updates. It involves components like GenUiConversation, ContentGenerator, and GenUiManager. ```mermaid graph TD subgraph "User" UserInput("Provide Prompt") UserInteraction("Interact with UI") end subgraph "GenUI Framework" GenUiConversation("GenUiConversation") ContentGenerator("ContentGenerator") GenUiManager("GenUiManager") GenUiSurface("GenUiSurface") end UserInput -- "calls sendRequest()" --> GenUiConversation; GenUiConversation -- "sends prompt" --> ContentGenerator; ContentGenerator -- "returns A2UI messages" --> GenUiConversation; GenUiConversation -- "handles messages" --> GenUiManager; GenUiManager -- "notifies of updates" --> GenUiSurface; GenUiSurface -- "renders UI" --> UserInteraction; UserInteraction -- "creates event" --> GenUiSurface; GenUiSurface -- "sends event to host" --> GenUiManager; GenUiManager -- "sends user input to" --> GenUiConversation; ``` -------------------------------- ### Add CatalogItem to Catalog Source: https://pub.dev/packages/genui/versions/0.5 Demonstrates how to include a custom `CatalogItem` into the main catalog when instantiating `GenUiManager`. This makes the new widget available for the agent to use. ```dart _genUiManager = GenUiManager( catalog: CoreCatalogItems.asCatalog().copyWith([riddleCard]), ); ``` -------------------------------- ### Add JSON Schema Builder Dependency for GenUI Custom Widgets Source: https://pub.dev/packages/genui/versions/0.5 This snippet shows how to add the `json_schema_builder` package as a dependency in your `pubspec.yaml` file. Ensure you use the same commit reference for `json_schema_builder` as you do for `genui` to maintain compatibility. This package is essential for defining schemas for custom widgets that the agent can generate. ```yaml dependencies: # ... json_schema_builder: git: url: https://github.com/flutter/genui.git path: packages/json_schema_builder ``` -------------------------------- ### Bind Image URL to Literal String Source: https://pub.dev/packages/genui/versions/0.5 Demonstrates binding an image widget's `url` property to a static URL using `literalString`. This allows the AI to specify a fixed image to be displayed. ```json { "Image": { "url": { "literalString": "https://example.com/image.png" }, "hint": "mediumFeature" } } ``` -------------------------------- ### Add genui Dependency in pubspec.yaml Source: https://pub.dev/packages/genui/versions/0.5.0/install This snippet shows how the genui dependency is declared in the pubspec.yaml file. It specifies the package name and version constraint. This is the manual way to add the dependency if `flutter pub add` is not used. ```yaml dependencies: genui: ^0.5.0 ``` -------------------------------- ### Add genui to Flutter Project Dependencies Source: https://pub.dev/packages/genui/versions/0.5 Specifies the necessary dependencies to include in your Flutter project's pubspec.yaml file to integrate the genui package and its Firebase AI provider. ```yaml dependencies: # ... genui: 0.5.0 genui_firebase_ai: 0.5.0 ``` -------------------------------- ### Enable Network Requests for iOS/macOS in Flutter Source: https://pub.dev/packages/genui/versions/0.5 This configuration snippet enables outbound network requests for Flutter projects targeting iOS or macOS. It requires adding a specific key-value pair to the application's entitlements file. Ensure this key is correctly placed within the dictionary structure of your entitlement file. ```xml ... com.apple.security.network.client ``` -------------------------------- ### Send User Messages and Display Agent UI in Flutter Source: https://pub.dev/packages/genui/versions/0.5 This Flutter code demonstrates how to send user messages to a GenUI agent and display the agent's generated UI. It utilizes `GenUiConversation` for communication and manages UI surfaces through callbacks like `_onSurfaceAdded` and `_onSurfaceDeleted`. The UI is rendered using `GenUiSurface` widgets within a `ListView.builder`, and user input is handled via a `TextField` and `ElevatedButton`. ```dart class _MyHomePageState extends State { // ... final _textController = TextEditingController(); final _surfaceIds = []; // Send a message containing the user's text to the agent. void _sendMessage(String text) { if (text.trim().isEmpty) return; _genUiConversation.sendRequest(UserMessage.text(text)); } // A callback invoked by the [GenUiConversation] when a new UI surface is generated. // Here, the ID is stored so the build method can create a GenUiSurface to // display it. void _onSurfaceAdded(SurfaceAdded update) { setState(() { _surfaceIds.add(update.surfaceId); }); } // A callback invoked by GenUiConversation when a UI surface is removed. void _onSurfaceDeleted(SurfaceRemoved update) { setState(() { _surfaceIds.remove(update.surfaceId); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Column( children: [ Expanded( child: ListView.builder( itemCount: _surfaceIds.length, itemBuilder: (context, index) { // For each surface, create a GenUiSurface to display it. final id = _surfaceIds[index]; return GenUiSurface(host: _genUiConversation.host, surfaceId: id); }, ), ), SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0), child: Row( children: [ Expanded( child: TextField( controller: _textController, decoration: const InputDecoration( hintText: 'Enter a message', ), ), ), const SizedBox(width: 16), ElevatedButton( onPressed: () { // Send the user's text to the agent. _sendMessage(_textController.text); _textController.clear(); }, child: const Text('Send'), ), ], ), ), ), ], ), ); } } ``` -------------------------------- ### Add genui and genui_firebase_ai to pubspec.yaml Source: https://pub.dev/packages/genui/index This snippet shows how to add the genui and genui_firebase_ai dependencies to your Flutter project's pubspec.yaml file. These are necessary for integrating genui with Firebase AI Logic. ```yaml dependencies: # ... genui: 0.5.0 genui_firebase_ai: 0.5.0 ``` -------------------------------- ### Import genui Package in Dart Code Source: https://pub.dev/packages/genui/versions/0.5.0/install This import statement makes the genui package's functionalities available in your Dart code. It's a prerequisite for using any of the package's features. Ensure the package is correctly added as a dependency. ```dart import 'package:genui/genui.dart'; ``` -------------------------------- ### Bind Widget Property to Literal String Source: https://pub.dev/packages/genui/versions/0.5 Illustrates how to bind a widget's property to a static string value using `literalString` within the AI-generated JSON data. This is used for properties like the text content of a `Text` widget. ```json { "Text": { "text": { "literalString": "Welcome to GenUI" }, "hint": "h1" } } ``` -------------------------------- ### Define Custom Widget Schema using JSON Schema Builder in Dart Source: https://pub.dev/packages/genui/versions/0.5 This Dart code defines a JSON schema for a custom widget using the `json_schema_builder` package. The schema specifies an object with two properties: 'question' and 'answer', both of type string and marked as required. This schema allows the GenUI agent to generate instances of this custom widget with the necessary data. ```dart import 'package:json_schema_builder/json_schema_builder.dart'; import 'package:flutter/material.dart'; import 'package:genui/genui.dart'; final _schema = S.object( properties: { 'question': S.string(description: 'The question part of a riddle.'), 'answer': S.string(description: 'The answer part of a riddle.'), }, required: ['question', 'answer'], ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.