Surface/lib/widgets/attachment/attachment_item.dart

60 lines
1.7 KiB
Dart
Raw Normal View History

2024-11-10 09:21:57 +00:00
import 'package:dismissible_page/dismissible_page.dart';
2024-11-09 04:04:03 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:surface/providers/sn_network.dart';
import 'package:surface/types/attachment.dart';
2024-11-10 09:21:57 +00:00
import 'package:surface/widgets/attachment/attachment_detail.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/widgets/universal_image.dart';
2024-11-10 09:21:57 +00:00
import 'package:uuid/uuid.dart';
2024-11-09 04:04:03 +00:00
class AttachmentItem extends StatelessWidget {
final SnAttachment data;
2024-11-10 09:21:57 +00:00
final bool isExpandable;
const AttachmentItem({
super.key,
required this.data,
this.isExpandable = false,
});
2024-11-09 04:04:03 +00:00
2024-11-10 09:21:57 +00:00
Widget _buildContent(BuildContext context, String heroTag) {
2024-11-09 04:04:03 +00:00
final tp = data.mimetype.split('/').firstOrNull;
final sn = context.read<SnNetworkProvider>();
switch (tp) {
case 'image':
2024-11-10 09:21:57 +00:00
return Hero(
tag: 'attachment-${data.rid}-$heroTag',
2024-11-10 14:56:18 +00:00
child: LayoutBuilder(builder: (context, constraints) {
return UniversalImage(
sn.getAttachmentUrl(data.rid),
fit: BoxFit.cover,
cacheHeight: constraints.maxHeight,
cacheWidth: constraints.maxWidth,
);
}),
2024-11-09 04:04:03 +00:00
);
default:
return const Placeholder();
}
}
2024-11-10 09:21:57 +00:00
@override
Widget build(BuildContext context) {
final uuid = Uuid();
final heroTag = uuid.v4();
if (isExpandable) {
return GestureDetector(
child: _buildContent(context, heroTag),
onTap: () {
context.pushTransparentRoute(
AttachmentDetailPopup(data: data, heroTag: heroTag),
rootNavigator: true,
);
},
);
}
return _buildContent(context, heroTag);
}
2024-11-09 04:04:03 +00:00
}