♻️ Better attachment list & zoom view

This commit is contained in:
2024-12-01 23:56:56 +08:00
parent 9588fc0475
commit b0790ea145
6 changed files with 361 additions and 159 deletions

View File

@ -38,140 +38,141 @@ class _AttachmentListState extends State<AttachmentList> {
@override
Widget build(BuildContext context) {
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,
);
final aspectRatio = widget.data[0]?.metadata['ratio']?.toDouble() ?? 1;
if (widget.data.isEmpty) return const SizedBox.shrink();
if (widget.data.length == 1) {
return GestureDetector(
child: Builder(
builder: (context) {
if (ResponsiveBreakpoints.of(context).largerThan(MOBILE) ||
widget.noGrow) {
return Padding(
// Single child list-like displaying
padding: widget.listPadding ?? EdgeInsets.zero,
child: Container(
constraints: constraints,
decoration: BoxDecoration(
color: backgroundColor,
border: Border(top: borderSide, bottom: borderSide),
borderRadius: AttachmentList.kDefaultRadius,
),
child: AspectRatio(
aspectRatio: widget.data[0]?.metadata['ratio']
?.toDouble() ??
switch (
widget.data[0]?.mimetype.split('/').firstOrNull) {
'audio' => 16 / 9,
'video' => 16 / 9,
_ => 1,
},
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags[0],
return LayoutBuilder(
builder: (context, layoutConstraints) {
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,
maxWidth: layoutConstraints.maxWidth - 20,
maxHeight: widget.maxHeight ?? double.infinity,
);
if (widget.data.isEmpty) return const SizedBox.shrink();
if (widget.data.length == 1) {
return AspectRatio(
aspectRatio: widget.data[0]?.metadata['ratio']?.toDouble() ??
switch (widget.data[0]?.mimetype.split('/').firstOrNull) {
'audio' => 16 / 9,
'video' => 16 / 9,
_ => 1,
},
child: GestureDetector(
child: Builder(
builder: (context) {
if (ResponsiveBreakpoints.of(context).largerThan(MOBILE) ||
widget.noGrow) {
return Padding(
// Single child list-like displaying
padding: widget.listPadding ?? EdgeInsets.zero,
child: Container(
constraints: constraints,
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),
),
),
),
);
}
return Container(
decoration: BoxDecoration(
color: backgroundColor,
border: Border(top: borderSide, bottom: borderSide),
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags.first,
),
);
},
),
child: AspectRatio(
aspectRatio: widget.data[0]?.metadata['ratio']?.toDouble() ?? 1,
child: AttachmentItem(
data: widget.data[0],
heroTag: heroTags.first,
),
),
);
},
),
onTap: () {
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: 0,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
);
}
return Container(
constraints: BoxConstraints(maxHeight: widget.maxHeight ?? 320),
child: ScrollConfiguration(
behavior: _AttachmentListScrollBehavior(),
child: ListView.separated(
shrinkWrap: true,
itemCount: widget.data.length,
itemBuilder: (context, idx) {
return GestureDetector(
onTap: () {
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: idx,
initialIndex: 0,
heroTags: heroTags,
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
child: Stack(
children: [
Container(
constraints: constraints,
decoration: BoxDecoration(
color: backgroundColor,
border: Border(top: borderSide, bottom: borderSide),
borderRadius: AttachmentList.kDefaultRadius,
),
child: AspectRatio(
aspectRatio:
widget.data[idx]?.metadata['ratio']?.toDouble() ?? 1,
child: ClipRRect(
borderRadius: AttachmentList.kDefaultRadius,
child: AttachmentItem(
data: widget.data[idx],
heroTag: heroTags[idx],
),
);
}
return AspectRatio(
aspectRatio: aspectRatio,
child: Container(
constraints: BoxConstraints(maxHeight: widget.maxHeight ?? 320),
child: ScrollConfiguration(
behavior: _AttachmentListScrollBehavior(),
child: ListView.separated(
shrinkWrap: true,
itemCount: widget.data.length,
itemBuilder: (context, idx) {
return GestureDetector(
onTap: () {
context.pushTransparentRoute(
AttachmentZoomView(
data: widget.data.where((ele) => ele != null).cast(),
initialIndex: idx,
heroTags: heroTags,
),
),
backgroundColor: Colors.black.withOpacity(0.7),
rootNavigator: true,
);
},
child: Stack(
children: [
Container(
constraints: constraints,
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: 12,
child: Chip(
label: Text('${idx + 1}/${widget.data.length}'),
),
),
],
),
),
Positioned(
right: 12,
bottom: 12,
child: Chip(
label: Text('${idx + 1}/${widget.data.length}'),
),
),
],
);
},
separatorBuilder: (context, index) => const Gap(8),
padding: widget.listPadding,
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
),
);
},
separatorBuilder: (context, index) => const Gap(8),
padding: widget.listPadding,
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
),
),
),
),
);
},
);
}
}