From 5998d20881f93a30fabca876ce8840b3a24d0992 Mon Sep 17 00:00:00 2001 From: duong2417 Date: Wed, 13 Nov 2024 00:17:23 +0700 Subject: [PATCH] fix chat_buble_widget --- lib/_shared/widgets/chat_bubble_widget.dart | 71 +++++++++------------ 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/lib/_shared/widgets/chat_bubble_widget.dart b/lib/_shared/widgets/chat_bubble_widget.dart index 08faf49..c78dfb2 100644 --- a/lib/_shared/widgets/chat_bubble_widget.dart +++ b/lib/_shared/widgets/chat_bubble_widget.dart @@ -5,13 +5,14 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:image_network/image_network.dart'; import '../../features/translate_settings/trans_bloc.dart'; import '../../features/translate_settings/widgets/translate_settings_button.dart'; +import '../../service_locator/service_locator.dart'; import '../../utils/send_to_gemini.dart'; import '../button/button_with_popup.dart'; import '../dialog/loading_dialog.dart'; import 'translations_widget.dart'; // ignore: must_be_immutable -class ChatBubble extends StatefulWidget { +class ChatBubble extends StatelessWidget { final bool isMine; final String message; final String? photoUrl; @@ -19,7 +20,7 @@ class ChatBubble extends StatefulWidget { final Map translations; final String id; - const ChatBubble( + ChatBubble( {required this.isMine, required this.message, required this.photoUrl, @@ -28,25 +29,19 @@ class ChatBubble extends StatefulWidget { super.key, required this.id}); - @override - State createState() => _ChatBubbleState(); -} - -class _ChatBubbleState extends State { final double _iconSize = 24.0; @override Widget build(BuildContext context) { - print('build ChatBubble: ${widget.displayName}'); // user avatar final List widgets = []; //cp at here widgets.add(Padding( padding: const EdgeInsets.all(8.0), child: ClipRRect( borderRadius: BorderRadius.circular(_iconSize), - child: widget.photoUrl == null + child: photoUrl == null ? const _DefaultPersonWidget() : ImageNetwork( - image: widget.photoUrl!, + image: photoUrl!, width: _iconSize, height: _iconSize, fitAndroidIos: BoxFit.fitWidth, @@ -62,36 +57,35 @@ class _ChatBubbleState extends State { BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.8), decoration: BoxDecoration( borderRadius: BorderRadius.circular(16.0), - color: widget.isMine ? Colors.black26 : Colors.black87), + color: isMine ? Colors.black26 : Colors.black87), padding: const EdgeInsets.all(8.0), child: BlocBuilder(builder: (context, state) { return Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: widget.isMine - ? CrossAxisAlignment.end - : CrossAxisAlignment.start, + crossAxisAlignment: + isMine ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ // display name Text( - widget.displayName ?? 'Unknown', + displayName ?? 'Unknown', style: Theme.of(context).textTheme.bodyMedium?.copyWith( - color: widget.isMine ? Colors.black87 : Colors.grey, + color: isMine ? Colors.black87 : Colors.grey, fontWeight: FontWeight.bold), ), // original language Text( - widget.message, + message, style: Theme.of(context) .textTheme .bodyMedium ?.copyWith(color: Colors.white), ), // english version (if there is) - if (state is ChangeLangState && !widget.isMine) + if (state is ChangeLangState && !isMine) FutureBuilder>( future: getTranslations( - context, widget.message, state.selectedLanguages), + context, message, state.selectedLanguages), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Text('Đang dịch...'); @@ -99,8 +93,8 @@ class _ChatBubbleState extends State { if (snapshot.hasData) { print('snapshot.data: ${snapshot.data}'); return TranslationsWidget( - widget: widget, translations: snapshot.data!, + widget: this, ); } return const LoadingState(); @@ -114,7 +108,7 @@ class _ChatBubbleState extends State { DropdownMenuItem( child: const Text('Copy'), onTap: () { - Clipboard.setData(ClipboardData(text: widget.message)); + Clipboard.setData(ClipboardData(text: message)); }, ), DropdownMenuItem( @@ -135,7 +129,7 @@ class _ChatBubbleState extends State { //TODO }, ), - if (widget.isMine) + if (isMine) DropdownMenuItem( child: const Text('Xóa'), onTap: () { @@ -153,43 +147,40 @@ class _ChatBubbleState extends State { child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: - widget.isMine ? MainAxisAlignment.end : MainAxisAlignment.start, - children: widget.isMine ? widgets.reversed.toList() : widgets, + isMine ? MainAxisAlignment.end : MainAxisAlignment.start, + children: isMine ? widgets.reversed.toList() : widgets, ), ); } Map _resultTranslations = {}; + List _previousLanguages = []; + Future> getTranslations(BuildContext context, String message, List selectedLanguages) async { - print('getTranslations'); + final _firebaseInstance = FirebaseFirestore.instance; + // final _firebaseInstance = ServiceLocator.get(); //nếu có lang dịch rồi, lang chưa: vẫn lấy "translations" từ firestore vì lang dịch rồi đó sẽ có trên firebase if (_previousLanguages.equal(selectedLanguages)) { - print('selectedLanguages is equal: $selectedLanguages'); return _resultTranslations; } else { - print( - 'selectedLanguages is not equal: $selectedLanguages, _previousLanguages:$_previousLanguages'); _previousLanguages = selectedLanguages; _resultTranslations = {}; //cp reset } //get translations from firestore - DocumentSnapshot> data = await FirebaseFirestore - .instance + //TODO: use ServiceLocator + DocumentSnapshot> data = await _firebaseInstance .collection('translations') - .doc(widget.id) + .doc(id) .get(); //neu id chua exist, them vao firestore if (!data.exists) { - await FirebaseFirestore.instance + await _firebaseInstance .collection('translations') - .doc(widget.id) + .doc(id) .set({'id ko ton tai': 'id ko ton tai => them moi'}); - data = await FirebaseFirestore.instance - .collection('translations') - .doc(widget.id) - .get(); + data = await _firebaseInstance.collection('translations').doc(id).get(); } Map translations = data.data() ?? {}; print('translations: $translations'); @@ -200,8 +191,6 @@ class _ChatBubbleState extends State { if (lang.toLowerCase() == e.key.toLowerCase()) { _resultTranslations[lang] = "translated: " + e.value; //đã có trên firestore rồi, ko cần dịch nữa - print( - 'da co tren firestore: translations[${e.key}]: ${translations[e.key]}'); hasTranslated = true; break; } @@ -211,13 +200,13 @@ class _ChatBubbleState extends State { } } if (listLangSendToGemini.isNotEmpty) { - await sendToGenmini(msg: widget.message, languages: listLangSendToGemini) + await sendToGenmini(msg: message, languages: listLangSendToGemini) .then((map) { for (var e in map.entries) { //_resultTranslations add them data _resultTranslations[e.key] = e.value; } - FirebaseFirestore.instance.collection('translations').doc(widget.id).set( + _firebaseInstance.collection('translations').doc(id).set( map, SetOptions( merge: true)); //nếu key đã có rồi thì ko thay đổi, chưa thì add