Browse Source

message list image async resize

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

105
lib/im/chat_details_page.dart

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

Loading…
Cancel
Save