Surface/lib/widgets/attachment/attachment_item.dart

153 lines
4.4 KiB
Dart
Raw Permalink 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 {
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-13 13:36:28 +00:00
child: AutoResizeUniversalImage(
sn.getAttachmentUrl(data.rid),
key: Key('attachment-${data.rid}-$heroTag'),
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-13 13:36:28 +00:00
if (data.isMature) {
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(
AttachmentDetailPopup(data: data, heroTag: heroTag),
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);
},
),
),
],
);
}
}