Solian/lib/widgets/attachments/attachment_item.dart

182 lines
6.1 KiB
Dart
Raw Normal View History

2024-06-01 13:39:28 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2024-05-27 15:07:01 +00:00
import 'package:chewie/chewie.dart';
2024-05-18 14:23:36 +00:00
import 'package:flutter/material.dart';
2024-05-20 15:39:23 +00:00
import 'package:get/get.dart';
2024-05-18 14:23:36 +00:00
import 'package:solian/models/attachment.dart';
2024-06-01 13:39:28 +00:00
import 'package:solian/platform.dart';
2024-05-18 14:23:36 +00:00
import 'package:solian/services.dart';
2024-05-27 15:07:01 +00:00
import 'package:url_launcher/url_launcher_string.dart';
import 'package:video_player/video_player.dart';
2024-05-18 14:23:36 +00:00
2024-05-27 15:07:01 +00:00
class AttachmentItem extends StatefulWidget {
2024-05-25 16:11:00 +00:00
final String parentId;
2024-05-18 14:23:36 +00:00
final Attachment item;
2024-05-20 16:02:39 +00:00
final bool showBadge;
final bool showHideButton;
final BoxFit fit;
2024-05-20 15:39:23 +00:00
final String? badge;
2024-05-20 16:02:39 +00:00
final Function? onHide;
2024-05-18 14:23:36 +00:00
2024-05-20 15:39:23 +00:00
const AttachmentItem({
super.key,
2024-05-25 16:11:00 +00:00
required this.parentId,
2024-05-20 15:39:23 +00:00
required this.item,
this.badge,
2024-05-20 16:02:39 +00:00
this.fit = BoxFit.cover,
this.showBadge = true,
this.showHideButton = true,
this.onHide,
2024-05-20 15:39:23 +00:00
});
2024-05-18 14:23:36 +00:00
2024-05-27 15:07:01 +00:00
@override
State<AttachmentItem> createState() => _AttachmentItemState();
}
class _AttachmentItemState extends State<AttachmentItem> {
VideoPlayerController? _videoPlayerController;
ChewieController? _chewieController;
void ensureInitVideo() {
if (_videoPlayerController != null) return;
_videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
));
_videoPlayerController!.initialize();
_chewieController = ChewieController(
aspectRatio: widget.item.metadata?['ratio'] ?? 16 / 9,
videoPlayerController: _videoPlayerController!,
customControls: const MaterialControls(showPlayButton: true),
materialProgressColors: ChewieProgressColors(
playedColor: Theme.of(context).colorScheme.primary,
handleColor: Theme.of(context).colorScheme.primary,
),
);
}
2024-05-18 14:23:36 +00:00
@override
Widget build(BuildContext context) {
2024-05-27 15:07:01 +00:00
switch (widget.item.mimetype.split('/').first) {
case 'image':
return Hero(
tag: Key('a${widget.item.uuid}p${widget.parentId}'),
child: Stack(
fit: StackFit.expand,
children: [
2024-06-01 13:39:28 +00:00
if (PlatformInfo.canCacheImage)
CachedNetworkImage(
2024-06-03 15:36:46 +00:00
fit: widget.fit,
2024-06-01 13:39:28 +00:00
imageUrl:
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
progressIndicatorBuilder: (context, url, downloadProgress) =>
2024-06-01 16:25:12 +00:00
Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
2024-06-01 13:39:28 +00:00
),
)
else
Image.network(
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
fit: widget.fit,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
);
},
),
2024-05-27 15:07:01 +00:00
if (widget.showBadge && widget.badge != null)
Positioned(
right: 12,
bottom: 8,
child: Material(
color: Colors.transparent,
child: Chip(label: Text(widget.badge!)),
),
),
if (widget.showHideButton && widget.item.isMature)
Positioned(
top: 8,
left: 12,
child: Material(
color: Colors.transparent,
child: ActionChip(
visualDensity:
const VisualDensity(vertical: -4, horizontal: -4),
2024-06-02 06:45:19 +00:00
avatar: Icon(
Icons.visibility_off,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
2024-05-27 15:07:01 +00:00
label: Text('hide'.tr),
onPressed: () {
if (widget.onHide != null) widget.onHide!();
},
),
),
),
],
),
);
case 'video':
ensureInitVideo();
return Chewie(controller: _chewieController!);
default:
return Center(
child: Container(
constraints: const BoxConstraints(
maxWidth: 280,
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.file_present, size: 32),
const SizedBox(height: 6),
Text(
widget.item.mimetype,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 2),
Text(
widget.item.alt,
style: const TextStyle(fontSize: 13),
textAlign: TextAlign.center,
),
const SizedBox(height: 12),
TextButton.icon(
icon: const Icon(Icons.launch),
label: Text('openInBrowser'.tr),
style: const ButtonStyle(
visualDensity: VisualDensity(vertical: -2, horizontal: -4),
),
2024-05-20 16:02:39 +00:00
onPressed: () {
2024-05-27 15:07:01 +00:00
launchUrlString(
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
);
2024-05-20 16:02:39 +00:00
},
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
],
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
),
);
}
}
@override
void dispose() {
_videoPlayerController?.dispose();
_chewieController?.dispose();
super.dispose();
2024-05-18 14:23:36 +00:00
}
2024-05-20 15:39:23 +00:00
}