@@ -6,7 +6,7 @@ import 'package:fji_example/firebase_options.dart';
66import 'package:fji_example_core_flutter/model.dart' ;
77import 'package:flutter/material.dart' ;
88import 'package:flutter_chat_ui/flutter_chat_ui.dart' ;
9- import 'package:flutter_chat_types/flutter_chat_types .dart' as types ;
9+ import 'package:flutter_chat_core/flutter_chat_core .dart' as chat ;
1010
1111void main () async {
1212 WidgetsFlutterBinding .ensureInitialized ();
@@ -23,26 +23,31 @@ void main() async {
2323 await auth.currentUser? .delete ();
2424
2525 // This will call the `beforeUserCreated` function
26- await auth.createUserWithEmailAndPassword (
26+ final credential = await auth.createUserWithEmailAndPassword (
2727 email: 'test@example.com' ,
2828 password: 'password1234' ,
2929 );
3030
31- runApp (const MaterialApp (home: ChatsList ()));
31+ final uid = credential.user? .uid;
32+ if (uid == null ) throw 'Failed to create user' ;
33+
34+ runApp (MaterialApp (home: ChatsList (uid: uid)));
3235}
3336
3437class ChatsList extends StatelessWidget {
35- static final _auth = FirebaseAuth .instance ;
38+ final String uid ;
3639
37- const ChatsList ({super .key});
40+ const ChatsList ({super .key, required this .uid });
3841
3942 @override
4043 Widget build (BuildContext context) {
44+ final navigator = Navigator .of (context);
45+
4146 return Scaffold (
4247 appBar: AppBar (title: const Text ('Chats' )),
4348 body: FirestoreListView (
4449 query: chatsRef
45- .whereParticipants (arrayContains: _auth.currentUser ! . uid)
50+ .whereParticipants (arrayContains: uid)
4651 .orderByLastMessageTime (descending: true )
4752 .reference,
4853 emptyBuilder: (context) => const Center (child: Text ('No chats' )),
@@ -58,7 +63,7 @@ class ChatsList extends StatelessWidget {
5863 ],
5964 ),
6065 subtitle: lastMessage != null ? Text (lastMessage) : null ,
61- onTap: () => showChat (Navigator . of (context) , snap.id),
66+ onTap: () => showChat (navigator , snap.id),
6267 leading: const Icon (Icons .chat),
6368 );
6469 },
@@ -73,24 +78,24 @@ class ChatsList extends StatelessWidget {
7378 void createChat (BuildContext context) async {
7479 final navigator = Navigator .of (context);
7580 final doc = chatsRef.doc ();
76- final chat = FjiChat (id: doc.id, participants: {_auth.currentUser ! . uid});
81+ final chat = FjiChat (id: doc.id, participants: {uid});
7782 await doc.set (chat);
7883 showChat (navigator, doc.id);
7984 }
8085
8186 void showChat (NavigatorState navigator, String chatId) {
8287 navigator.push (
83- MaterialPageRoute (builder: (context) => ChatScreen (chatId: chatId)),
88+ MaterialPageRoute (
89+ builder: (context) => ChatScreen (uid: uid, chatId: chatId)),
8490 );
8591 }
8692}
8793
8894class ChatScreen extends StatelessWidget {
89- static final _auth = FirebaseAuth .instance;
90-
95+ final String uid;
9196 final String chatId;
9297
93- const ChatScreen ({super .key, required this .chatId});
98+ const ChatScreen ({super .key, required this .uid, required this . chatId});
9499
95100 @override
96101 Widget build (BuildContext context) {
@@ -106,39 +111,49 @@ class ChatScreen extends StatelessWidget {
106111 .messages
107112 .orderByTimestamp (descending: true )
108113 .reference,
109- builder: (context, snap, child ) {
114+ builder: (context, snap, _ ) {
110115 if (snap.hasError) {
111116 return Center (child: Text ('Error\n ${snap .error }' ));
112117 }
113118 if (snap.isFetching) {
114119 return const Center (child: CircularProgressIndicator ());
115120 }
116121 return Chat (
117- user: types.User (id: _auth.currentUser! .uid),
118- messages: snap.docs
119- .map (
120- (snap) => types.TextMessage (
121- id: snap.id,
122- createdAt: snap.data ().timestamp.millisecondsSinceEpoch,
123- author: types.User (id: snap.data ().author),
124- text: snap.data ().text,
125- ),
126- )
127- .toList (),
128- onEndReached: () async => snap.fetchMore (),
129- isLastPage: ! snap.hasMore,
130- onSendPressed: (partial) {
131- final text = partial.text;
132- if (text.isEmpty) return ;
133- // This will call the `onMessageCreated` function
134- chatsRef.doc (chatId).messages.doc ().set (
135- FjiMessage (
136- author: _auth.currentUser! .uid,
137- text: text,
138- ),
139- timestampFieldValue: FieldValue .serverTimestamp (),
140- );
122+ currentUserId: uid,
123+ resolveUser: (id) async {
124+ final snapshot = await usersRef.doc (id).get ();
125+ final data = snapshot.data;
126+ if (data == null ) return null ;
127+
128+ return chat.User (id: id, name: data.displayName);
141129 },
130+ chatController: chat.InMemoryChatController (
131+ messages: snap.docs
132+ .map (
133+ (e) => chat.Message .text (
134+ id: e.id,
135+ authorId: e.data ().author,
136+ createdAt: e.data ().timestamp,
137+ text: e.data ().text,
138+ ),
139+ )
140+ .toList (),
141+ ),
142+ onMessageSend: (text) =>
143+ chatsRef.doc (chatId).messages.doc ().set (
144+ FjiMessage (
145+ author: uid,
146+ text: text,
147+ ),
148+ timestampFieldValue: FieldValue .serverTimestamp (),
149+ ),
150+ builders: chat.Builders (
151+ chatAnimatedListBuilder: (context, itemBuilder) =>
152+ ChatAnimatedList (
153+ itemBuilder: itemBuilder,
154+ onEndReached: () async => snap.fetchMore (),
155+ ),
156+ ),
142157 );
143158 },
144159 );
0 commit comments