Browse Source

message list image async resize

wr_202303
zsw 4 months ago
parent
commit
e912ab1fb4
  1. 135
      lib/im/chat_details_page.dart

135
lib/im/chat_details_page.dart

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:ui'; import 'dart:ui';
@ -107,6 +108,7 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
Future refresh() async { Future refresh() async {
List<Message> messagePage = List<Message> messagePage =
await hxDatabase.queryUList(conversation, page: page + 1, pageSize: 10); await hxDatabase.queryUList(conversation, page: page + 1, pageSize: 10);
debugPrint("refresh-message-list: ${messagePage.length} page: $page");
if (messagePage.isEmpty) { if (messagePage.isEmpty) {
refreshController.loadNoData(); refreshController.loadNoData();
return; return;
@ -855,33 +857,31 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
SizedBox( SizedBox(
width: 12.w, width: 12.w,
), ),
Expanded( GestureDetector(
// flex: 3, onLongPress: () {
child: Container( setState(() {});
// decoration: BoxDecoration( },
// borderRadius: BorderRadius.circular(6), onTap:(){},
// color: Color(0xFFFFFFFF), child: FutureBuilder(
// boxShadow: [ future: fetchImageSize(messages[position].attach),
// BoxShadow( builder: (BuildContext context, AsyncSnapshot snapshot) {
// color: Color(0xFFA8A3A3).withAlpha(12), if (snapshot.connectionState == ConnectionState.waiting || snapshot.hasError) {
// offset: Offset(0, 4), return Image.file(
// blurRadius: 4, File(messages[position].attach),
// spreadRadius: 0, width: 180.w,
// ), height: 200.h,
// ], fit: BoxFit.contain,
// ), );
child: GestureDetector( } else {
onLongPress: () { return Image.file(
setState(() {}); File(messages[position].attach),
}, width: snapshot.data.width,
child: Image.file( height: snapshot.data.height,
File(messages[position].attach), fit: BoxFit.cover,
width: 180.h, );
height: 200.h, }
fit: BoxFit.contain, },
), ),
),
),
), ),
Spacer(), Spacer(),
], ],
@ -895,34 +895,31 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
child: Row( child: Row(
children: [ children: [
Spacer(), Spacer(),
Expanded( GestureDetector(
// flex: 3, onLongPress: () {
child: Container( setState(() {});
// decoration: BoxDecoration( },
// borderRadius: BorderRadius.circular(6), onTap:(){},
// color: Color(0xFFFFFFFF), child: FutureBuilder(
// boxShadow: [ future: fetchImageSize(messages[position].attach),
// BoxShadow( builder: (BuildContext context, AsyncSnapshot snapshot) {
// color: Color(0xFFA8A3A3).withAlpha(12), if (snapshot.connectionState == ConnectionState.waiting || snapshot.hasError) {
// offset: Offset(0, 4), return Image.file(
// blurRadius: 4, File(messages[position].attach),
// spreadRadius: 0, width: 180.w,
// ), height: 200.h,
// ], fit: BoxFit.contain,
// ), );
child: GestureDetector( } else {
onLongPress: () { return Image.file(
setState(() {}); File(messages[position].attach),
}, width: snapshot.data.width,
onTap:(){}, height: snapshot.data.height,
child: Image.file( fit: BoxFit.cover,
File(messages[position].attach), );
width: 180.h, }
height: 200.h, },
fit: BoxFit.contain, ),
),
),
),
), ),
SizedBox( SizedBox(
width: 12.w, width: 12.w,
@ -959,6 +956,34 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
); );
} }
Future<Size> fetchImageSize(String imagePath) async {
Size size = Size.zero;
Completer<Size> completer = Completer();
Image.file(
File(imagePath),
).image.resolve(ImageConfiguration())
.addListener(ImageStreamListener((image, synchronousCall) {
size = Size((image.image.width ?? 0).toDouble(), (image.image.height ?? 0).toDouble());
size = resizeImage(size);
completer.complete(size);
}));
return completer.future;
}
//
resizeImage(Size size) {
if (size.width > 180 || size.height > 200) {
if (size.width > size.height) {
int height = size.height ~/ (size.width ~/ 180);
size = Size(180, height.toDouble());
} else {
int width = size.width ~/ (size.height ~/ 200);
size = Size(width.toDouble(), 200);
}
}
return size;
}
/// ///
Widget input() { Widget input() {
return Container( return Container(

Loading…
Cancel
Save