|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:typed_data'; |
| 16 | +import 'dart:developer'; |
| 17 | +import 'package:flutter/material.dart'; |
| 18 | +import 'package:flutter/foundation.dart' show kIsWeb; |
| 19 | +import 'package:permission_handler/permission_handler.dart'; |
| 20 | +import 'package:firebase_ai/firebase_ai.dart'; |
| 21 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 22 | +import 'package:image_picker/image_picker.dart'; |
| 23 | +import '../../shared/ui/app_frame.dart'; |
| 24 | +import '../../shared/ui/app_spacing.dart'; |
| 25 | +import '../../shared/ui/chat_components/ui_components.dart'; |
| 26 | +import '../../shared/chat_service.dart'; |
| 27 | +import '../../shared/ui/chat_components/model_picker.dart'; |
| 28 | +import '../../shared/models/models.dart'; |
| 29 | + |
| 30 | +class ChatDemoNano extends ConsumerStatefulWidget { |
| 31 | + const ChatDemoNano({super.key, this.isSelected = false}); |
| 32 | + final bool isSelected; |
| 33 | + |
| 34 | + @override |
| 35 | + ConsumerState<ChatDemoNano> createState() => ChatDemoNanoState(); |
| 36 | +} |
| 37 | + |
| 38 | +class ChatDemoNanoState extends ConsumerState<ChatDemoNano> { |
| 39 | + // Service for interacting with the Gemini API. |
| 40 | + late final ChatService _chatService; |
| 41 | + |
| 42 | + // UI State |
| 43 | + final List<MessageData> _messages = <MessageData>[]; |
| 44 | + final TextEditingController _userTextInputController = |
| 45 | + TextEditingController(); |
| 46 | + Uint8List? _attachment; |
| 47 | + final ScrollController _scrollController = ScrollController(); |
| 48 | + bool _loading = false; |
| 49 | + OverlayPortalController opController = OverlayPortalController(); |
| 50 | + static bool _pickerHasBeenShown = false; |
| 51 | + |
| 52 | + @override |
| 53 | + void initState() { |
| 54 | + super.initState(); |
| 55 | + _chatService = ChatService(ref); |
| 56 | + geminiModels.selectModel('gemini-2.5-flash-image-preview'); |
| 57 | + _chatService.init(); |
| 58 | + _userTextInputController.text = |
| 59 | + 'Hot air balloons rising over the San Francisco Bay at golden hour with a view of the Golden Gate Bridge. Make it anime style.'; |
| 60 | + _checkAndShowPicker(); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + void didUpdateWidget(ChatDemoNano oldWidget) { |
| 65 | + super.didUpdateWidget(oldWidget); |
| 66 | + if (widget.isSelected != oldWidget.isSelected) { |
| 67 | + _checkAndShowPicker(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void _checkAndShowPicker() { |
| 72 | + if (widget.isSelected && !_pickerHasBeenShown) { |
| 73 | + _pickerHasBeenShown = true; |
| 74 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 75 | + if (mounted) { |
| 76 | + showModelPicker(); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + void didChangeDependencies() { |
| 84 | + requestPermissions(); |
| 85 | + super.didChangeDependencies(); |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + void dispose() { |
| 90 | + _scrollController.dispose(); |
| 91 | + _userTextInputController.dispose(); |
| 92 | + super.dispose(); |
| 93 | + } |
| 94 | + |
| 95 | + Future<void> requestPermissions() async { |
| 96 | + if (!kIsWeb) { |
| 97 | + await Permission.manageExternalStorage.request(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + void _scrollToEnd() { |
| 102 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 103 | + if (_scrollController.hasClients) { |
| 104 | + _scrollController.animateTo( |
| 105 | + _scrollController.position.maxScrollExtent, |
| 106 | + duration: const Duration(milliseconds: 300), |
| 107 | + curve: Curves.easeOut, |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + void _pickImage() async { |
| 114 | + final pickedImage = await ImagePicker().pickImage( |
| 115 | + source: ImageSource.gallery, |
| 116 | + ); |
| 117 | + |
| 118 | + if (pickedImage != null) { |
| 119 | + final imageBytes = await pickedImage.readAsBytes(); |
| 120 | + setState(() { |
| 121 | + _attachment = imageBytes; |
| 122 | + }); |
| 123 | + log('attachment saved!'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + void sendMessage(String text) async { |
| 128 | + if (text.isEmpty) return; |
| 129 | + |
| 130 | + setState(() { |
| 131 | + _loading = true; |
| 132 | + }); |
| 133 | + |
| 134 | + // Add user message to UI |
| 135 | + final userMessageText = text.trim(); |
| 136 | + final userAttachment = _attachment; |
| 137 | + _messages.add( |
| 138 | + MessageData( |
| 139 | + text: userMessageText, |
| 140 | + image: userAttachment != null ? Image.memory(userAttachment) : null, |
| 141 | + fromUser: true, |
| 142 | + ), |
| 143 | + ); |
| 144 | + setState(() { |
| 145 | + _attachment = null; |
| 146 | + _userTextInputController.clear(); |
| 147 | + }); |
| 148 | + _scrollToEnd(); |
| 149 | + |
| 150 | + // Construct the Content object for the service |
| 151 | + final content = (userAttachment != null) |
| 152 | + ? Content.multi([ |
| 153 | + TextPart(userMessageText), |
| 154 | + InlineDataPart('image/jpeg', userAttachment), |
| 155 | + ]) |
| 156 | + : Content.text(userMessageText); |
| 157 | + |
| 158 | + // Call the service and handle the response |
| 159 | + try { |
| 160 | + final chatResponse = await _chatService.sendMessage(content); |
| 161 | + _messages.add( |
| 162 | + MessageData( |
| 163 | + text: chatResponse.text, |
| 164 | + image: chatResponse.image, |
| 165 | + fromUser: false, |
| 166 | + ), |
| 167 | + ); |
| 168 | + } catch (e) { |
| 169 | + if (mounted) { |
| 170 | + ScaffoldMessenger.of(context).showSnackBar( |
| 171 | + SnackBar( |
| 172 | + content: Text(e.toString()), |
| 173 | + backgroundColor: Theme.of(context).colorScheme.error, |
| 174 | + ), |
| 175 | + ); |
| 176 | + } |
| 177 | + } finally { |
| 178 | + setState(() { |
| 179 | + _loading = false; |
| 180 | + }); |
| 181 | + _scrollToEnd(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + void showModelPicker() { |
| 186 | + showDialog( |
| 187 | + context: context, |
| 188 | + builder: (context) { |
| 189 | + return ModelPicker( |
| 190 | + selectedModel: geminiModels.selectedModel, |
| 191 | + onSelected: (value) { |
| 192 | + _chatService.changeModel(value); |
| 193 | + setState(() { |
| 194 | + _userTextInputController.text = |
| 195 | + geminiModels.selectedModel.defaultPrompt; |
| 196 | + _messages.clear(); |
| 197 | + }); |
| 198 | + }, |
| 199 | + ); |
| 200 | + }, |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + @override |
| 205 | + Widget build(BuildContext context) { |
| 206 | + return Scaffold( |
| 207 | + body: Column( |
| 208 | + children: [ |
| 209 | + Expanded( |
| 210 | + child: AppFrame( |
| 211 | + child: Padding( |
| 212 | + padding: const EdgeInsets.all(AppSpacing.s16), |
| 213 | + child: Center( |
| 214 | + child: Column( |
| 215 | + mainAxisAlignment: MainAxisAlignment.center, |
| 216 | + children: <Widget>[ |
| 217 | + Expanded( |
| 218 | + child: MessageListView( |
| 219 | + messages: _messages, |
| 220 | + scrollController: _scrollController, |
| 221 | + ), |
| 222 | + ), |
| 223 | + if (_loading) const LinearProgressIndicator(), |
| 224 | + AttachmentPreview(attachment: _attachment), |
| 225 | + ], |
| 226 | + ), |
| 227 | + ), |
| 228 | + ), |
| 229 | + ), |
| 230 | + ), |
| 231 | + ], |
| 232 | + ), |
| 233 | + bottomNavigationBar: MessageInputBar( |
| 234 | + textController: _userTextInputController, |
| 235 | + loading: _loading, |
| 236 | + sendMessage: sendMessage, |
| 237 | + onPickImagePressed: _pickImage, |
| 238 | + ), |
| 239 | + ); |
| 240 | + } |
| 241 | +} |
0 commit comments