|
| 1 | +```dart |
| 2 | +import 'dart:math'; |
| 3 | +
|
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_chat_core/flutter_chat_core.dart'; |
| 6 | +import 'package:flutter_chat_ui/flutter_chat_ui.dart'; |
| 7 | +import 'package:flyer_chat_text_stream_message/flyer_chat_text_stream_message.dart'; |
| 8 | +
|
| 9 | +class Basic extends StatefulWidget { |
| 10 | + const Basic({super.key}); |
| 11 | +
|
| 12 | + @override |
| 13 | + BasicState createState() => BasicState(); |
| 14 | +} |
| 15 | +
|
| 16 | +class BasicState extends State<Basic> { |
| 17 | + final _chatController = InMemoryChatController(); |
| 18 | +
|
| 19 | + @override |
| 20 | + void dispose() { |
| 21 | + _chatController.dispose(); |
| 22 | + super.dispose(); |
| 23 | + } |
| 24 | +
|
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + return Scaffold( |
| 28 | + body: Chat( |
| 29 | + builders: Builders( |
| 30 | + textStreamMessageBuilder: (context, message, index) { |
| 31 | + // To see a practical implementation of the stream manager, check the main example app at |
| 32 | + // https://github.com/flyerhq/flutter_chat_ui/tree/main/examples/flyer_chat |
| 33 | + final streamState = context.watch<GeminiStreamManager>().getState( |
| 34 | + message.streamId, |
| 35 | + ); |
| 36 | + // Return the stream message widget, passing the state |
| 37 | + return FlyerChatTextStreamMessage( |
| 38 | + message: message, |
| 39 | + index: index, |
| 40 | + streamState: streamState, |
| 41 | + ); |
| 42 | + }, |
| 43 | + ), |
| 44 | + chatController: _chatController, |
| 45 | + currentUserId: 'user1', |
| 46 | + onMessageSend: (text) { |
| 47 | + _chatController.insert( |
| 48 | + // For a comprehensive example of how to implement the stream manager and work with streamIds, refer to the main example application available at |
| 49 | + // https://github.com/flyerhq/flutter_chat_ui/tree/main/examples/flyer_chat |
| 50 | + TextStreamMessage( |
| 51 | + id: streamId, |
| 52 | + authorId: 'agent1', |
| 53 | + createdAt: DateTime.now().toUtc(), |
| 54 | + streamId: streamId, |
| 55 | + ) |
| 56 | + ); |
| 57 | + }, |
| 58 | + resolveUser: (UserID id) async { |
| 59 | + return User(id: id, name: 'John Doe'); |
| 60 | + }, |
| 61 | + ), |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
0 commit comments