Surface/lib/widgets/attachment/attachment_item.dart

157 lines
4.5 KiB
Dart
Raw Normal View History

2024-11-13 13:36:28 +00:00
import 'dart:ui';
2024-11-10 09:21:57 +00:00
import 'package:dismissible_page/dismissible_page.dart';
2024-11-13 13:36:28 +00:00
import 'package:easy_localization/easy_localization.dart';
2024-11-09 04:04:03 +00:00
import 'package:flutter/material.dart';
2024-11-13 13:36:28 +00:00
import 'package:gap/gap.dart';
import 'package:material_symbols_icons/symbols.dart';
2024-11-09 04:04:03 +00:00
import 'package:provider/provider.dart';
2024-11-13 13:36:28 +00:00
import 'package:styled_widget/styled_widget.dart';
2024-11-09 04:04:03 +00:00
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 {
2024-11-17 16:55:39 +00:00
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-17 16:55:39 +00:00
if (data == null) {
return const Icon(Symbols.cancel).center();
}
final tp = data!.mimetype.split('/').firstOrNull;
2024-11-09 04:04:03 +00:00
final sn = context.read<SnNetworkProvider>();
switch (tp) {
case 'image':
2024-11-10 09:21:57 +00:00
return Hero(
2024-11-17 16:55:39 +00:00
tag: 'attachment-${data!.rid}-$heroTag',
2024-11-13 13:36:28 +00:00
child: AutoResizeUniversalImage(
2024-11-17 16:55:39 +00:00
sn.getAttachmentUrl(data!.rid),
key: Key('attachment-${data!.rid}-$heroTag'),
2024-11-13 13:36:28 +00:00
fit: BoxFit.cover,
),
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();
2024-11-17 16:55:39 +00:00
if (data!.isMature) {
2024-11-13 13:36:28 +00:00
return _AttachmentItemSensitiveBlur(
child: _buildContent(context, heroTag),
);
}
2024-11-10 09:21:57 +00:00
if (isExpandable) {
return GestureDetector(
child: _buildContent(context, heroTag),
onTap: () {
context.pushTransparentRoute(
2024-11-17 16:55:39 +00:00
AttachmentDetailPopup(data: data!, heroTag: heroTag),
2024-11-10 09:21:57 +00:00
rootNavigator: true,
);
},
);
}
return _buildContent(context, heroTag);
}
2024-11-09 04:04:03 +00:00
}
2024-11-13 13:36:28 +00:00
class _AttachmentItemSensitiveBlur extends StatefulWidget {
final Widget child;
const _AttachmentItemSensitiveBlur({super.key, required this.child});
@override
State<_AttachmentItemSensitiveBlur> createState() =>
_AttachmentItemSensitiveBlurState();
}
class _AttachmentItemSensitiveBlurState
extends State<_AttachmentItemSensitiveBlur> {
bool _doesShow = false;
@override
Widget build(BuildContext context) {
return Stack(
children: [
widget.child,
ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 40, sigmaY: 40),
child: Container(
color: Colors.black.withOpacity(0.5),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Symbols.visibility_off,
color: Colors.white,
size: 32,
),
const Gap(8),
Text('sensitiveContent')
.tr()
.fontSize(20)
.textColor(Colors.white)
.bold(),
Text('sensitiveContentDescription')
.tr()
.fontSize(14)
.textColor(Colors.white.withOpacity(0.8)),
const Gap(16),
InkWell(
child: Text('sensitiveContentReveal')
.tr()
.textColor(Colors.white),
onTap: () {
setState(() => _doesShow = !_doesShow);
},
),
],
),
),
),
)
.opacity(_doesShow ? 0 : 1, animate: true)
.animate(const Duration(milliseconds: 300), Curves.easeInOut),
if (_doesShow)
Positioned(
top: 0,
left: 0,
child: InkWell(
child: Icon(
Symbols.visibility_off,
color: Colors.white,
shadows: [
BoxShadow(
color: Colors.black.withOpacity(0.5),
blurRadius: 3,
offset: Offset(0, 1.5),
),
],
).padding(all: 12),
onTap: () {
setState(() => _doesShow = !_doesShow);
},
),
),
],
);
}
}