2024-05-18 14:23:36 +00:00
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
|
import 'package:carousel_slider/carousel_slider.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/models/attachment.dart';
|
|
|
|
import 'package:solian/providers/content/attachment_item.dart';
|
|
|
|
import 'package:solian/providers/content/attachment_list.dart';
|
2024-05-20 16:02:39 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_list_fullscreen.dart';
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
class AttachmentList extends StatefulWidget {
|
2024-05-20 15:11:26 +00:00
|
|
|
final List<int> attachmentsId;
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
const AttachmentList({super.key, required this.attachmentsId});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AttachmentList> createState() => _AttachmentListState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AttachmentListState extends State<AttachmentList> {
|
|
|
|
bool _isLoading = true;
|
|
|
|
bool _showMature = false;
|
|
|
|
|
|
|
|
double _aspectRatio = 1;
|
|
|
|
|
|
|
|
List<Attachment?> _attachmentsMeta = List.empty();
|
|
|
|
|
|
|
|
void getMetadataList() {
|
|
|
|
final AttachmentListProvider provider = Get.find();
|
|
|
|
|
|
|
|
if (widget.attachmentsId.isEmpty) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_attachmentsMeta = List.filled(widget.attachmentsId.length, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
int progress = 0;
|
|
|
|
for (var idx = 0; idx < widget.attachmentsId.length; idx++) {
|
|
|
|
provider.getMetadata(widget.attachmentsId[idx]).then((resp) {
|
|
|
|
progress++;
|
|
|
|
_attachmentsMeta[idx] = Attachment.fromJson(resp.body);
|
|
|
|
if (progress == widget.attachmentsId.length) {
|
|
|
|
setState(() {
|
|
|
|
calculateAspectRatio();
|
|
|
|
_isLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculateAspectRatio() {
|
2024-05-20 15:11:26 +00:00
|
|
|
bool isConsistent = true;
|
|
|
|
double? consistentValue;
|
2024-05-18 14:23:36 +00:00
|
|
|
int portrait = 0, square = 0, landscape = 0;
|
|
|
|
for (var entry in _attachmentsMeta) {
|
|
|
|
if (entry!.metadata?['ratio'] != null) {
|
2024-05-20 15:11:26 +00:00
|
|
|
consistentValue ??= entry.metadata?['ratio'];
|
|
|
|
if (isConsistent && entry.metadata?['ratio'] != consistentValue) {
|
|
|
|
isConsistent = false;
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
if (entry.metadata!['ratio'] > 1) {
|
|
|
|
landscape++;
|
|
|
|
} else if (entry.metadata!['ratio'] == 1) {
|
|
|
|
square++;
|
|
|
|
} else {
|
|
|
|
portrait++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-20 15:11:26 +00:00
|
|
|
if (isConsistent && consistentValue != null) {
|
|
|
|
_aspectRatio = consistentValue;
|
2024-05-18 14:23:36 +00:00
|
|
|
} else {
|
2024-05-20 15:11:26 +00:00
|
|
|
if (portrait > square && portrait > landscape) {
|
|
|
|
_aspectRatio = 9 / 16;
|
|
|
|
}
|
|
|
|
if (landscape > square && landscape > portrait) {
|
|
|
|
_aspectRatio = 16 / 9;
|
|
|
|
} else {
|
|
|
|
_aspectRatio = 1;
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
getMetadataList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (widget.attachmentsId.isEmpty) {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_isLoading) {
|
|
|
|
return AspectRatio(
|
|
|
|
aspectRatio: _aspectRatio,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(color: Theme.of(context).colorScheme.surfaceVariant),
|
|
|
|
child: const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CarouselSlider.builder(
|
|
|
|
options: CarouselOptions(
|
|
|
|
aspectRatio: _aspectRatio,
|
|
|
|
viewportFraction: 1,
|
|
|
|
enableInfiniteScroll: false,
|
|
|
|
),
|
|
|
|
itemCount: _attachmentsMeta.length,
|
|
|
|
itemBuilder: (context, idx, _) {
|
|
|
|
final element = _attachmentsMeta[idx];
|
|
|
|
return GestureDetector(
|
|
|
|
child: Container(
|
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
decoration: BoxDecoration(color: Theme.of(context).colorScheme.surfaceVariant),
|
|
|
|
child: Stack(
|
|
|
|
fit: StackFit.expand,
|
|
|
|
children: [
|
2024-05-20 15:39:23 +00:00
|
|
|
AttachmentItem(
|
|
|
|
key: Key('a${element!.uuid}'),
|
|
|
|
item: element,
|
2024-05-20 16:02:39 +00:00
|
|
|
badge: _attachmentsMeta.length > 1 ? '${idx + 1}/${_attachmentsMeta.length}' : null,
|
|
|
|
showHideButton: !element.isMature || _showMature,
|
2024-05-20 15:39:23 +00:00
|
|
|
onHide: () {
|
|
|
|
setState(() => _showMature = false);
|
|
|
|
},
|
|
|
|
),
|
2024-05-18 14:23:36 +00:00
|
|
|
if (element.isMature && !_showMature)
|
|
|
|
BackdropFilter(
|
2024-05-20 16:02:39 +00:00
|
|
|
filter: ImageFilter.blur(sigmaX: 100, sigmaY: 100),
|
2024-05-18 14:23:36 +00:00
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.black.withOpacity(0.5),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (element.isMature && !_showMature)
|
|
|
|
Center(
|
|
|
|
child: Container(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 280),
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.visibility_off, color: Colors.white, size: 32),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
Text(
|
|
|
|
'matureContent'.tr,
|
|
|
|
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 16),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'matureContentCaption'.tr,
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
if (!_showMature && _attachmentsMeta.any((e) => e!.isMature)) {
|
|
|
|
setState(() => _showMature = true);
|
|
|
|
} else {
|
2024-05-20 16:02:39 +00:00
|
|
|
Navigator.of(context, rootNavigator: true).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => AttachmentListFullscreen(
|
|
|
|
attachment: element,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|