From 4366a49466de549d669aca264aa7b038ab247ab8 Mon Sep 17 00:00:00 2001 From: Linloir <3145078758@qq.com> Date: Thu, 20 Oct 2022 11:07:37 +0800 Subject: [PATCH] Improvement: - Precache image widget --- lib/chat/chat_page.dart | 3 +-- lib/chat/cubit/chat_cubit.dart | 15 ++++++++++----- lib/chat/model/chat_history.dart | 13 +++++++++---- lib/chat/view/common/image_box.dart | 4 ++-- lib/main.dart | 8 ++++---- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/chat/chat_page.dart b/lib/chat/chat_page.dart index bf65d03..d247041 100644 --- a/lib/chat/chat_page.dart +++ b/lib/chat/chat_page.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-13 14:03:16 - * @LastEditTime : 2022-10-20 00:48:43 + * @LastEditTime : 2022-10-20 10:52:30 * @Description : */ @@ -87,7 +87,6 @@ class ChatPage extends StatelessWidget { else { //Return history tile return Padding( - key: ValueKey(state.chatHistory[index].message.contentmd5), padding: const EdgeInsets.symmetric( horizontal: 24, vertical: 8 diff --git a/lib/chat/cubit/chat_cubit.dart b/lib/chat/cubit/chat_cubit.dart index fac13aa..b10d666 100644 --- a/lib/chat/cubit/chat_cubit.dart +++ b/lib/chat/cubit/chat_cubit.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-13 14:03:56 - * @LastEditTime : 2022-10-20 00:13:18 + * @LastEditTime : 2022-10-20 11:04:40 * @Description : */ @@ -11,6 +11,7 @@ import 'dart:convert'; import 'package:bloc/bloc.dart'; import 'package:convert/convert.dart'; import 'package:crypto/crypto.dart'; +import 'package:flutter/material.dart'; import 'package:open_file/open_file.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:tcp_client/chat/cubit/chat_state.dart'; @@ -78,7 +79,8 @@ class ChatCubit extends Cubit { var newHistory = ChatHistory( message: msg, type: ChatHistoryType.outcome, - status: ChatHistoryStatus.sending + status: ChatHistoryStatus.sending, + preCachedImage: msg.type == MessageType.image ? Image.memory(base64.decode(msg.contentDecoded)) : null ); if(msg.type == MessageType.file) { //Remove mock history @@ -112,7 +114,8 @@ class ChatCubit extends Cubit { var history = ChatHistory( message: message, type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, - status: ChatHistoryStatus.done + status: ChatHistoryStatus.done, + preCachedImage: message.type == MessageType.image ? Image.memory(base64.decode(message.contentDecoded)) : null ); newHistories.add(history); } @@ -208,7 +211,8 @@ class ChatCubit extends Cubit { var newHistory = ChatHistory( message: response.message, type: response.message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, - status: ChatHistoryStatus.done + status: ChatHistoryStatus.done, + preCachedImage: response.message.type == MessageType.image ? Image.memory(base64.decode(response.message.contentDecoded)) : null ); var newHistoryList = [newHistory, ...state.chatHistory]; emit(state.copyWith(chatHistory: newHistoryList)); @@ -223,7 +227,8 @@ class ChatCubit extends Cubit { var newHistory = ChatHistory( message: message, type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, - status: ChatHistoryStatus.done + status: ChatHistoryStatus.done, + preCachedImage: message.type == MessageType.image ? Image.memory(base64.decode(message.contentDecoded)) : null ); fetchedHistories.insert(0, newHistory); } diff --git a/lib/chat/model/chat_history.dart b/lib/chat/model/chat_history.dart index 9f0f691..090d9dc 100644 --- a/lib/chat/model/chat_history.dart +++ b/lib/chat/model/chat_history.dart @@ -1,11 +1,12 @@ /* * @Author : Linloir * @Date : 2022-10-14 14:55:20 - * @LastEditTime : 2022-10-18 15:19:46 + * @LastEditTime : 2022-10-20 11:01:03 * @Description : */ import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; import 'package:tcp_client/repositories/common_models/message.dart'; enum ChatHistoryType { outcome, income } @@ -15,22 +16,26 @@ class ChatHistory extends Equatable { final Message message; final ChatHistoryType type; final ChatHistoryStatus status; + final Image? preCachedImage; const ChatHistory({ required this.message, required this.type, - required this.status + required this.status, + this.preCachedImage, }); ChatHistory copyWith({ Message? message, ChatHistoryType? type, - ChatHistoryStatus? status + ChatHistoryStatus? status, + Image? preCachedImage }) { return ChatHistory( message: message ?? this.message, type: type ?? this.type, - status: status ?? this.status + status: status ?? this.status, + preCachedImage: preCachedImage ?? this.preCachedImage ); } diff --git a/lib/chat/view/common/image_box.dart b/lib/chat/view/common/image_box.dart index 9ffdd89..40c09e7 100644 --- a/lib/chat/view/common/image_box.dart +++ b/lib/chat/view/common/image_box.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-14 17:04:20 - * @LastEditTime : 2022-10-20 00:47:44 + * @LastEditTime : 2022-10-20 11:05:37 * @Description : */ @@ -24,7 +24,7 @@ class ImageBox extends StatelessWidget { onTap: (){}, child: Container( constraints: const BoxConstraints(maxWidth: 500, maxHeight: 250), - child: Image.memory(base64Decode(history.message.contentDecoded)), + child: history.preCachedImage ?? Image.memory(base64Decode(history.message.contentDecoded)), ), ); } diff --git a/lib/main.dart b/lib/main.dart index ded9194..92a2809 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-10 08:04:53 - * @LastEditTime : 2022-10-19 11:17:44 + * @LastEditTime : 2022-10-20 10:34:41 * @Description : */ import 'package:easy_debounce/easy_debounce.dart'; @@ -38,12 +38,12 @@ void main() async { skipTaskbar: false, titleBarStyle: TitleBarStyle.normal ); - windowManager.waitUntilReadyToShow(windowOptions, () async { - await windowManager.show(); - await windowManager.focus(); + await windowManager.waitUntilReadyToShow(windowOptions, () async { if(posX != null && posY != null) { await windowManager.setPosition(Offset(posX, posY)); } + await windowManager.show(); + await windowManager.focus(); }); //---------------------------------------------------------------------