Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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 Oct 24, 2025
7ddd102
making navigation and style changes to improve look and feel
apeynado Oct 24, 2025
3cf3f49
added the free trial promotion to the blaze warning banner
apeynado Oct 27, 2025
0a64cb8
updating the hero image to reflect the current UX
apeynado Oct 27, 2025
94a3ec9
actually making the UI responsive
apeynado Oct 27, 2025
fb32b52
make the banana bigger
apeynado Oct 27, 2025
e836a0e
updated hero image
apeynado Oct 27, 2025
6136796
removed the headers from each demo app
apeynado Oct 27, 2025
6c45eb4
adding vertical padding to a few demos
apeynado Oct 27, 2025
76f043f
update audio input sample rate
khanhnwin Oct 28, 2025
c9d47a7
putting back message input field in bottom navigation bar
khanhnwin Oct 28, 2025
4f00882
deleting unused imagen demo
khanhnwin Oct 28, 2025
b4f51bf
remove unused imports
khanhnwin Oct 28, 2025
5cd46db
remove padding from top of multimodal & live api demos
khanhnwin Oct 28, 2025
764d8c5
replace bottom nav bar with a hamburger menu + drawer
khanhnwin Oct 28, 2025
c6e9af4
Merge pull request #1 from apeynado/apeynado/main
apeynado Oct 28, 2025
961bc59
refactor the common UI and service code
cynthiajoan Nov 11, 2025
03ab41b
add a dialog to trigger the function call
cynthiajoan Nov 13, 2025
4d65d1a
refactor the repeat logic for different layout
cynthiajoan Nov 13, 2025
91272eb
more layout refactor
cynthiajoan Nov 13, 2025
0d933c3
update the initialize logic for live api demo
cynthiajoan Nov 13, 2025
15b3835
keep firebase_options.dart out of gitignore
cynthiajoan Nov 13, 2025
108e8f7
remove the start dialog for chat's function call
cynthiajoan Nov 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 33 additions & 78 deletions firebase_ai_logic_showcase/lib/demos/chat/chat_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ class _ChatDemoState extends ConsumerState<ChatDemo> {
Uint8List? _attachment;
final ScrollController _scrollController = ScrollController();
bool _loading = false;
OverlayPortalController opController = OverlayPortalController();

@override
void initState() {
super.initState();
_chatService = ChatService(ref);
final model = geminiModels.selectModel('gemini-2.5-flash');
_chatService = ChatService(ref, model);
_chatService.init();
_userTextInputController.text = geminiModels.selectedModel.defaultPrompt;

_userTextInputController.text =
'Hey Gemini! Can you set the app color to purple?';
WidgetsBinding.instance.addPostFrameCallback((_) {
opController.show();
sendMessage(_userTextInputController.text);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to hijack the UI and automatically send the first message. As someone launching the demo for the first time, sending the message automatically kind of takes away the user's agency? Generally a bad UX pattern to execute an action without user consent. Maybe an alternative is to add a pop up or something that says "click send message" to see a tool call! or something that encourages the user to send the initial message, but still lets them decide.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a dialog to ask the user to make the decision

});
}

Expand Down Expand Up @@ -162,86 +162,41 @@ class _ChatDemoState extends ConsumerState<ChatDemo> {
}
}

void showModelPicker() {
opController.hide();
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(
appBar: AppBar(
backgroundColor: Colors.transparent,
title: const Text('Chat Demo'),
actions: [
OverlayPortal(
controller: opController,
child: IconButton(
onPressed: showModelPicker,
icon: Icon(Icons.settings_outlined),
),
overlayChildBuilder: (context) {
return Positioned(
right: 0,
top: 40,
child: Dialog(
insetAnimationDuration: Duration(milliseconds: 2000),
constraints: BoxConstraints(maxWidth: 500),
insetPadding: EdgeInsets.all(8),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [Text('Try another model!')],
),
),
),
);
},
),
],
),
body: 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,
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),
],
),
),
if (_loading) const LinearProgressIndicator(),
AttachmentPreview(attachment: _attachment),
],
),
),
),
),
),
bottomNavigationBar: MessageInputBar(
textController: _userTextInputController,
loading: _loading,
sendMessage: sendMessage,
onPickImagePressed: _pickImage,
MessageInputBar(
textController: _userTextInputController,
loading: _loading,
sendMessage: sendMessage,
onPickImagePressed: _pickImage,
),
],
),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ import './models/models.dart';
/// https://firebase.google.com/docs/ai-logic/chat?api=dev
class ChatService {
final WidgetRef _ref;
ChatService(this._ref);
final GeminiModel _gemini;

ChatService(this._ref, this._gemini);

GeminiModel? _gemini = geminiModels.selectedModel;
late ChatSession _chat;

void init() {
var gemini = _gemini;
if (gemini != null) {
_chat = gemini.model.startChat();
}
_chat = _gemini.model.startChat();
}

void changeModel(String modelName) {
_gemini = geminiModels.selectModel(modelName);
init();
// This is now handled in the UI layer
}

Future<ChatResponse> sendMessage(Content message) async {
Expand Down
219 changes: 219 additions & 0 deletions firebase_ai_logic_showcase/lib/demos/chat_nano/chat_nano_demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// Copyright 2025 Google LLC
Copy link
Contributor

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!

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.

//
// 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 './ui_components/ui_components.dart';
import './firebaseai_chat_service.dart';
import 'ui_components/model_picker.dart';
import './models/gemini_model_nano.dart';

class ChatDemoNano extends ConsumerStatefulWidget {
const ChatDemoNano({super.key});

@override
ConsumerState<ChatDemoNano> createState() => ChatDemoNanoState();
}

class ChatDemoNanoState extends ConsumerState<ChatDemoNano> {
// Service for interacting with the Gemini API.
late final ChatServiceNano _chatService;

// UI State
final List<MessageData> _messages = <MessageData>[];
final TextEditingController _userTextInputController =
TextEditingController();
Uint8List? _attachment;
final ScrollController _scrollController = ScrollController();
bool _loading = false;
OverlayPortalController opController = OverlayPortalController();

@override
void initState() {
super.initState();
_chatService = ChatServiceNano(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.';
}

@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),
],
),
),
),
),
),
MessageInputBar(
textController: _userTextInputController,
loading: _loading,
sendMessage: sendMessage,
onPickImagePressed: _pickImage,
),
],
),
);
}
}
Loading