Surface/lib/widgets/attachment/attachment_list.dart

242 lines
8.8 KiB
Dart
Raw Normal View History

import 'dart:math' as math;
2024-12-12 15:51:27 +00:00
import 'package:collection/collection.dart';
2024-11-25 14:41:15 +00:00
import 'package:dismissible_page/dismissible_page.dart';
2024-11-09 04:04:03 +00:00
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-11-10 09:21:57 +00:00
import 'package:responsive_framework/responsive_framework.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/types/attachment.dart';
2024-12-07 15:40:26 +00:00
import 'package:surface/widgets/attachment/attachment_zoom.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/widgets/attachment/attachment_item.dart';
2024-11-25 14:41:15 +00:00
import 'package:uuid/uuid.dart';
2024-11-09 04:04:03 +00:00
2024-11-25 14:41:15 +00:00
class AttachmentList extends StatefulWidget {
2024-11-17 16:55:39 +00:00
final List<SnAttachment?> data;
2024-11-18 13:38:15 +00:00
final bool bordered;
final bool noGrow;
2024-12-12 15:51:27 +00:00
final bool isFlatted;
2024-11-13 16:20:59 +00:00
final double? maxHeight;
final EdgeInsets? listPadding;
2024-12-12 15:51:27 +00:00
2024-11-10 08:41:11 +00:00
const AttachmentList({
super.key,
required this.data,
2024-11-18 13:38:15 +00:00
this.bordered = false,
this.noGrow = false,
2024-12-12 15:51:27 +00:00
this.isFlatted = false,
2024-11-13 16:20:59 +00:00
this.maxHeight,
this.listPadding,
2024-11-10 08:41:11 +00:00
});
2024-11-09 04:04:03 +00:00
2024-12-12 15:51:27 +00:00
static const BorderRadius kDefaultRadius = BorderRadius.all(Radius.circular(8));
2024-11-10 09:21:57 +00:00
2024-11-25 14:41:15 +00:00
@override
State<AttachmentList> createState() => _AttachmentListState();
}
class _AttachmentListState extends State<AttachmentList> {
late final List<String> heroTags = List.generate(
widget.data.length,
(_) => const Uuid().v4(),
);
static const double kAttachmentMaxWidth = 640;
2024-11-09 04:04:03 +00:00
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, layoutConstraints) {
2024-12-12 15:51:27 +00:00
final borderSide =
widget.bordered ? BorderSide(width: 1, color: Theme.of(context).dividerColor) : BorderSide.none;
final backgroundColor = Theme.of(context).colorScheme.surfaceContainer;
final constraints = BoxConstraints(
minWidth: 80,
maxHeight: widget.maxHeight ?? double.infinity,
maxWidth: layoutConstraints.maxWidth - 20,
);
if (widget.data.isEmpty) return const SizedBox.shrink();
if (widget.data.length == 1) {
2024-12-12 15:51:27 +00:00
final singleAspectRatio = widget.data[0]?.metadata['ratio']?.toDouble() ??
switch (widget.data[0]?.mimetype.split('/').firstOrNull) {
'audio' => 16 / 9,
'video' => 16 / 9,
_ => 1,
}
.toDouble();
return Container(
constraints: ResponsiveBreakpoints.of(context).largerThan(MOBILE)
? constraints.copyWith(
maxWidth: math.min(
constraints.maxWidth,
kAttachmentMaxWidth,
),
)
: null,
child: AspectRatio(
aspectRatio: singleAspectRatio,
child: GestureDetector(
child: Builder(
builder: (context) {
2024-12-12 15:51:27 +00:00
if (ResponsiveBreakpoints.of(context).largerThan(MOBILE) || widget.noGrow) {
return Padding(
// Single child list-like displaying
padding: widget.listPadding ?? EdgeInsets.zero,
child: Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(top: borderSide, bottom: borderSide),
borderRadius: AttachmentList.kDefaultRadius,
),
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags[0],
),
),
),
);
}
return Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(top: borderSide, bottom: borderSide),
),
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags.first,
2024-11-25 14:41:15 +00:00
),
);
},
),
onTap: () {
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: 0,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
2024-11-25 14:41:15 +00:00
),
),
);
}
2024-12-12 15:51:27 +00:00
if (widget.isFlatted) {
return Wrap(
spacing: 4,
runSpacing: 4,
children: widget.data
.mapIndexed(
(idx, ele) => AspectRatio(
aspectRatio: (ele?.metadata['ratio'] ?? 1).toDouble(),
child: Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(
top: borderSide,
bottom: borderSide,
),
borderRadius: AttachmentList.kDefaultRadius,
),
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: ele,
heroTag: heroTags[idx],
),
),
),
),
)
.toList(),
);
}
return AspectRatio(
2024-12-11 14:28:16 +00:00
aspectRatio: (widget.data.firstOrNull?.metadata['ratio'] ?? 1).toDouble(),
child: Container(
constraints: BoxConstraints(maxHeight: constraints.maxHeight),
child: ScrollConfiguration(
behavior: _AttachmentListScrollBehavior(),
child: ListView.separated(
shrinkWrap: true,
itemCount: widget.data.length,
itemBuilder: (context, idx) {
return Container(
constraints: constraints,
child: AspectRatio(
2024-12-11 14:28:16 +00:00
aspectRatio: (widget.data[idx]?.metadata['ratio'] ?? 1).toDouble(),
child: GestureDetector(
onTap: () {
context.pushTransparentRoute(
AttachmentZoomView(
2024-12-12 15:51:27 +00:00
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: idx,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(
top: borderSide,
bottom: borderSide,
),
borderRadius: AttachmentList.kDefaultRadius,
),
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: widget.data[idx],
heroTag: heroTags[idx],
),
),
),
Positioned(
right: 8,
bottom: 8,
child: Chip(
label: Text('${idx + 1}/${widget.data.length}'),
),
),
],
),
),
2024-11-25 14:41:15 +00:00
),
);
},
separatorBuilder: (context, index) => const Gap(8),
padding: widget.listPadding,
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
2024-11-25 14:41:15 +00:00
),
),
),
);
},
2024-11-09 04:04:03 +00:00
);
}
}
class _AttachmentListScrollBehavior extends MaterialScrollBehavior {
@override
Set<PointerDeviceKind> get dragDevices => {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
};
}