-
Notifications
You must be signed in to change notification settings - Fork 56
updating the flutter AI playground's behavior and layout #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apeynado
wants to merge
23
commits into
flutter:main
Choose a base branch
from
apeynado:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8182542
updating the flutter AI playground's behavior and layout
apeynado 7ddd102
making navigation and style changes to improve look and feel
apeynado 3cf3f49
added the free trial promotion to the blaze warning banner
apeynado 0a64cb8
updating the hero image to reflect the current UX
apeynado 94a3ec9
actually making the UI responsive
apeynado fb32b52
make the banana bigger
apeynado e836a0e
updated hero image
apeynado 6136796
removed the headers from each demo app
apeynado 6c45eb4
adding vertical padding to a few demos
apeynado 76f043f
update audio input sample rate
khanhnwin c9d47a7
putting back message input field in bottom navigation bar
khanhnwin 4f00882
deleting unused imagen demo
khanhnwin b4f51bf
remove unused imports
khanhnwin 5cd46db
remove padding from top of multimodal & live api demos
khanhnwin 764d8c5
replace bottom nav bar with a hamburger menu + drawer
khanhnwin c6e9af4
Merge pull request #1 from apeynado/apeynado/main
apeynado 961bc59
refactor the common UI and service code
cynthiajoan 03ab41b
add a dialog to trigger the function call
cynthiajoan 4d65d1a
refactor the repeat logic for different layout
cynthiajoan 91272eb
more layout refactor
cynthiajoan 0d933c3
update the initialize logic for live api demo
cynthiajoan 15b3835
keep firebase_options.dart out of gitignore
cynthiajoan 108e8f7
remove the start dialog for chat's function call
cynthiajoan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,4 +51,5 @@ ephemeral/ | |
| #firebase | ||
| google-services.json | ||
| GoogleService-Info.plist | ||
| firebase.json | ||
| .firebaserc | ||
Binary file modified
BIN
-203 KB
(76%)
firebase_ai_logic_showcase/README/flutter_firebase_ai_sample_hero.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
firebase_ai_logic_showcase/lib/demos/chat/models/chat_response.dart
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
241 changes: 241 additions & 0 deletions
241
firebase_ai_logic_showcase/lib/demos/chat_nano/chat_nano_demo.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'dart:typed_data'; | ||
| import 'dart:developer'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/foundation.dart' show kIsWeb; | ||
| import 'package:permission_handler/permission_handler.dart'; | ||
| import 'package:firebase_ai/firebase_ai.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:image_picker/image_picker.dart'; | ||
| import '../../shared/ui/app_frame.dart'; | ||
| import '../../shared/ui/app_spacing.dart'; | ||
| import '../../shared/ui/chat_components/ui_components.dart'; | ||
| import '../../shared/chat_service.dart'; | ||
| import '../../shared/ui/chat_components/model_picker.dart'; | ||
| import '../../shared/models/models.dart'; | ||
|
|
||
| class ChatDemoNano extends ConsumerStatefulWidget { | ||
| const ChatDemoNano({super.key, this.isSelected = false}); | ||
| final bool isSelected; | ||
|
|
||
| @override | ||
| ConsumerState<ChatDemoNano> createState() => ChatDemoNanoState(); | ||
| } | ||
|
|
||
| class ChatDemoNanoState extends ConsumerState<ChatDemoNano> { | ||
| // Service for interacting with the Gemini API. | ||
| late final ChatService _chatService; | ||
|
|
||
| // UI State | ||
| final List<MessageData> _messages = <MessageData>[]; | ||
| final TextEditingController _userTextInputController = | ||
| TextEditingController(); | ||
| Uint8List? _attachment; | ||
| final ScrollController _scrollController = ScrollController(); | ||
| bool _loading = false; | ||
| OverlayPortalController opController = OverlayPortalController(); | ||
| static bool _pickerHasBeenShown = false; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _chatService = ChatService(ref); | ||
| geminiModels.selectModel('gemini-2.5-flash-image-preview'); | ||
| _chatService.init(); | ||
| _userTextInputController.text = | ||
| 'Hot air balloons rising over the San Francisco Bay at golden hour with a view of the Golden Gate Bridge. Make it anime style.'; | ||
| _checkAndShowPicker(); | ||
| } | ||
|
|
||
| @override | ||
| void didUpdateWidget(ChatDemoNano oldWidget) { | ||
| super.didUpdateWidget(oldWidget); | ||
| if (widget.isSelected != oldWidget.isSelected) { | ||
| _checkAndShowPicker(); | ||
| } | ||
| } | ||
|
|
||
| void _checkAndShowPicker() { | ||
| if (widget.isSelected && !_pickerHasBeenShown) { | ||
| _pickerHasBeenShown = true; | ||
| WidgetsBinding.instance.addPostFrameCallback((_) { | ||
| if (mounted) { | ||
| showModelPicker(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void didChangeDependencies() { | ||
| requestPermissions(); | ||
| super.didChangeDependencies(); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _scrollController.dispose(); | ||
| _userTextInputController.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| Future<void> requestPermissions() async { | ||
| if (!kIsWeb) { | ||
| await Permission.manageExternalStorage.request(); | ||
| } | ||
| } | ||
|
|
||
| void _scrollToEnd() { | ||
| WidgetsBinding.instance.addPostFrameCallback((_) { | ||
| if (_scrollController.hasClients) { | ||
| _scrollController.animateTo( | ||
| _scrollController.position.maxScrollExtent, | ||
| duration: const Duration(milliseconds: 300), | ||
| curve: Curves.easeOut, | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void _pickImage() async { | ||
| final pickedImage = await ImagePicker().pickImage( | ||
| source: ImageSource.gallery, | ||
| ); | ||
|
|
||
| if (pickedImage != null) { | ||
| final imageBytes = await pickedImage.readAsBytes(); | ||
| setState(() { | ||
| _attachment = imageBytes; | ||
| }); | ||
| log('attachment saved!'); | ||
| } | ||
| } | ||
|
|
||
| void sendMessage(String text) async { | ||
| if (text.isEmpty) return; | ||
|
|
||
| setState(() { | ||
| _loading = true; | ||
| }); | ||
|
|
||
| // Add user message to UI | ||
| final userMessageText = text.trim(); | ||
| final userAttachment = _attachment; | ||
| _messages.add( | ||
| MessageData( | ||
| text: userMessageText, | ||
| image: userAttachment != null ? Image.memory(userAttachment) : null, | ||
| fromUser: true, | ||
| ), | ||
| ); | ||
| setState(() { | ||
| _attachment = null; | ||
| _userTextInputController.clear(); | ||
| }); | ||
| _scrollToEnd(); | ||
|
|
||
| // Construct the Content object for the service | ||
| final content = (userAttachment != null) | ||
| ? Content.multi([ | ||
| TextPart(userMessageText), | ||
| InlineDataPart('image/jpeg', userAttachment), | ||
| ]) | ||
| : Content.text(userMessageText); | ||
|
|
||
| // Call the service and handle the response | ||
| try { | ||
| final chatResponse = await _chatService.sendMessage(content); | ||
| _messages.add( | ||
| MessageData( | ||
| text: chatResponse.text, | ||
| image: chatResponse.image, | ||
| fromUser: false, | ||
| ), | ||
| ); | ||
| } catch (e) { | ||
| if (mounted) { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar( | ||
| content: Text(e.toString()), | ||
| backgroundColor: Theme.of(context).colorScheme.error, | ||
| ), | ||
| ); | ||
| } | ||
| } finally { | ||
| setState(() { | ||
| _loading = false; | ||
| }); | ||
| _scrollToEnd(); | ||
| } | ||
| } | ||
|
|
||
| void showModelPicker() { | ||
| showDialog( | ||
| context: context, | ||
| builder: (context) { | ||
| return ModelPicker( | ||
| selectedModel: geminiModels.selectedModel, | ||
| onSelected: (value) { | ||
| _chatService.changeModel(value); | ||
| setState(() { | ||
| _userTextInputController.text = | ||
| geminiModels.selectedModel.defaultPrompt; | ||
| _messages.clear(); | ||
| }); | ||
| }, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| body: Column( | ||
| children: [ | ||
| Expanded( | ||
| child: AppFrame( | ||
| child: Padding( | ||
| padding: const EdgeInsets.all(AppSpacing.s16), | ||
| child: Center( | ||
| child: Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: <Widget>[ | ||
| Expanded( | ||
| child: MessageListView( | ||
| messages: _messages, | ||
| scrollController: _scrollController, | ||
| ), | ||
| ), | ||
| if (_loading) const LinearProgressIndicator(), | ||
| AttachmentPreview(attachment: _attachment), | ||
| ], | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| bottomNavigationBar: MessageInputBar( | ||
| textController: _userTextInputController, | ||
| loading: _loading, | ||
| sendMessage: sendMessage, | ||
| onPickImagePressed: _pickImage, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also duplicated code that doesn't need to be duplicated! We should eb able to use the original chat demo, just swap out the model in that is being used!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cleaned up the chat demo so it will focus on the function calling part and always select the gemini 2.5 flash. The majority of the UI component is moved to shared folder and reuse between two demos. I think it's ok to have independent demo page for different purposes.