122 lines
3.7 KiB
Dart
122 lines
3.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:f0ckapp/controller/media_controller.dart';
|
|
import 'package:f0ckapp/service/media_service.dart';
|
|
import 'package:f0ckapp/controller/auth_controller.dart';
|
|
import 'package:f0ckapp/widgets/actiontag.dart';
|
|
import 'package:f0ckapp/widgets/favorite_avatars.dart';
|
|
import 'package:f0ckapp/widgets/video_widget.dart';
|
|
import 'package:f0ckapp/models/media_item.dart';
|
|
|
|
class DetailMediaContent extends StatelessWidget {
|
|
final int currentPage;
|
|
final int index;
|
|
final void Function(String tag) onTagTap;
|
|
|
|
const DetailMediaContent({
|
|
super.key,
|
|
required this.currentPage,
|
|
required this.index,
|
|
required this.onTagTap,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final MediaService mediaService = Get.find();
|
|
final MediaController controller = Get.find();
|
|
final AuthController authController = Get.find();
|
|
|
|
return SafeArea(
|
|
top: false,
|
|
child: SingleChildScrollView(
|
|
child: Obx(() {
|
|
final MediaItem currentItem = controller.mediaItems[index];
|
|
final bool isFavorite =
|
|
currentItem.favorites?.any(
|
|
(f) => f.userId == authController.userId.value,
|
|
) ??
|
|
false;
|
|
|
|
return Column(
|
|
children: [
|
|
_buildMedia(currentItem, index == currentPage),
|
|
const SizedBox(height: 10, width: double.infinity),
|
|
_buildTags(currentItem),
|
|
if (currentItem.favorites != null &&
|
|
authController.token.value != null) ...[
|
|
const SizedBox(height: 20),
|
|
_buildFavoritesRow(context, currentItem, isFavorite, () async {
|
|
final List<Favorite>? newFavorites = await mediaService
|
|
.toggleFavorite(currentItem, isFavorite);
|
|
if (newFavorites != null) {
|
|
controller.mediaItems[index] = currentItem.copyWith(
|
|
favorites: newFavorites,
|
|
);
|
|
controller.mediaItems.refresh();
|
|
}
|
|
}),
|
|
],
|
|
const SizedBox(height: 20),
|
|
],
|
|
);
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildMedia(MediaItem item, bool isActive) {
|
|
if (item.mime.startsWith('image')) {
|
|
return CachedNetworkImage(
|
|
imageUrl: item.mediaUrl,
|
|
fit: BoxFit.contain,
|
|
placeholder: (context, url) =>
|
|
const Center(child: CircularProgressIndicator()),
|
|
errorWidget: (context, url, error) =>
|
|
const Center(child: Icon(Icons.error)),
|
|
);
|
|
} else {
|
|
return VideoWidget(details: item, isActive: isActive);
|
|
}
|
|
}
|
|
|
|
Widget _buildTags(MediaItem item) {
|
|
return Wrap(
|
|
alignment: WrapAlignment.center,
|
|
spacing: 5.0,
|
|
children: item.tags
|
|
.map<Widget>((Tag tag) => ActionTag(tag, onTagTap))
|
|
.toList(),
|
|
);
|
|
}
|
|
|
|
Widget _buildFavoritesRow(
|
|
BuildContext context,
|
|
MediaItem item,
|
|
bool isFavorite,
|
|
VoidCallback onFavoritePressed,
|
|
) {
|
|
return Row(
|
|
children: [
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
child: FavoriteAvatars(
|
|
favorites: item.favorites ?? [],
|
|
brightness: Theme.of(context).brightness,
|
|
),
|
|
),
|
|
),
|
|
IconButton(
|
|
icon: isFavorite
|
|
? const Icon(Icons.favorite)
|
|
: const Icon(Icons.favorite_outline),
|
|
color: Colors.red,
|
|
onPressed: onFavoritePressed,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|