Files
PiliPlus/lib/common/widgets/imageview.dart
2024-09-06 20:10:24 +08:00

62 lines
1.8 KiB
Dart

import 'dart:math';
import 'package:PiliPalaX/common/widgets/network_img_layer.dart';
import 'package:PiliPalaX/common/widgets/nine_grid_view.dart';
import 'package:PiliPalaX/pages/preview/view.dart';
import 'package:flutter/material.dart';
Widget image(
double maxWidth,
List<dynamic> picArr,
) {
double imageWidth = (maxWidth - 2 * 5) / 3;
double imageHeight = imageWidth;
if (picArr.length == 1) {
dynamic width = picArr[0]['img_width'];
dynamic height = picArr[0]['img_height'];
double ratioWH = width / height;
double ratioHW = height / width;
double maxRatio = 22 / 9;
imageWidth = ratioWH > 1.5
? maxWidth
: (ratioWH >= 1 || (height > width && ratioHW < 1.5))
? 2 * imageWidth
: imageWidth;
imageHeight = imageWidth * min(ratioHW, maxRatio);
}
return NineGridView(
type: NineGridType.weiBo,
margin: const EdgeInsets.only(top: 6),
bigImageWidth: imageWidth,
bigImageHeight: imageHeight,
space: 5,
height: picArr.length == 1 ? imageHeight : null,
width: picArr.length == 1 ? imageWidth : maxWidth,
itemCount: picArr.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
showDialog(
useSafeArea: false,
context: context,
builder: (context) {
return ImagePreview(
initialPage: index,
imgList: picArr.map((item) => item['img_src'] as String).toList(),
);
},
);
},
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: NetworkImgLayer(
src: picArr[index]['img_src'],
width: imageWidth,
height: imageWidth,
origAspectRatio:
picArr[index]['img_width'] / picArr[index]['img_height'],
),
),
),
);
}