Surface/lib/widgets/attachment/attachment_list.dart

239 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:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
2024-11-09 04:04:03 +00:00
import 'package:gap/gap.dart';
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 gridded;
2024-11-18 13:38:15 +00:00
final bool noGrow;
final BoxFit fit;
2024-11-13 16:20:59 +00:00
final double? maxHeight;
final double? minWidth;
2024-12-29 06:03:19 +00:00
final EdgeInsets? padding;
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.gridded = false,
2024-11-18 13:38:15 +00:00
this.noGrow = false,
this.fit = BoxFit.cover,
2024-11-13 16:20:59 +00:00
this.maxHeight,
this.minWidth,
2024-12-29 06:03:19 +00:00
this.padding,
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(),
);
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: widget.minWidth ?? 80,
2024-12-29 06:03:19 +00:00
maxHeight: widget.maxHeight ?? MediaQuery.of(context).size.height,
);
if (widget.data.isEmpty) return const SizedBox.shrink();
if (widget.data.length == 1) {
final singleAspectRatio = widget.data[0]?.data['ratio']?.toDouble() ??
2024-12-12 15:51:27 +00:00
switch (widget.data[0]?.mimetype.split('/').firstOrNull) {
'audio' => 16 / 9,
'video' => 16 / 9,
_ => 1,
}
.toDouble();
2024-12-29 07:30:31 +00:00
return Container(
2024-12-29 06:03:19 +00:00
padding: widget.padding ?? EdgeInsets.zero,
constraints: constraints,
child: GestureDetector(
child: AspectRatio(
aspectRatio: singleAspectRatio,
child: Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border.fromBorderSide(borderSide),
borderRadius: AttachmentList.kDefaultRadius,
),
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags[0],
fit: widget.fit,
),
),
),
2024-11-25 14:41:15 +00:00
),
onTap: () {
if (widget.data.firstOrNull?.mediaType != SnMediaType.image) return;
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: 0,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
),
);
}
if (widget.gridded) {
return Padding(
2024-12-29 06:03:19 +00:00
padding: widget.padding ?? EdgeInsets.zero,
child: Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(
top: borderSide,
bottom: borderSide,
),
borderRadius: AttachmentList.kDefaultRadius,
),
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: StaggeredGrid.count(
crossAxisCount: math.min(widget.data.length, 2),
crossAxisSpacing: 4,
mainAxisSpacing: 4,
children: widget.data
.mapIndexed(
(idx, ele) => GestureDetector(
child: Container(
constraints: constraints,
child: AttachmentItem(
data: ele,
heroTag: heroTags[idx],
2024-12-29 07:30:31 +00:00
fit: BoxFit.cover,
),
),
onTap: () {
if (widget.data[idx]!.mediaType != SnMediaType.image) return;
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: idx,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
2024-12-12 15:51:27 +00:00
),
)
.toList(),
),
),
),
2024-12-12 15:51:27 +00:00
);
}
return AspectRatio(
aspectRatio: (widget.data.firstOrNull?.data['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(
aspectRatio: (widget.data[idx]?.data['ratio'] ?? 1).toDouble(),
child: GestureDetector(
onTap: () {
if (widget.data[idx]?.mediaType != SnMediaType.image) return;
context.pushTransparentRoute(
AttachmentZoomView(
data:
widget.data.where((ele) => ele != null && ele.mediaType == SnMediaType.image).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),
2024-12-29 06:03:19 +00:00
padding: widget.padding,
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,
};
}