import 'package:cached_network_image/cached_network_image.dart'; import 'package:chat/configs/app_colors.dart'; import 'package:chewie/chewie.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:photo_view/photo_view.dart'; import 'package:photo_view/photo_view_gallery.dart'; import 'package:video_player/video_player.dart'; class MomentMediaPreview extends StatelessWidget { final List mediaSourceList; final int? initialPage; const MomentMediaPreview({ Key? key, required this.mediaSourceList, this.initialPage, }) : super(key: key); @override Widget build(BuildContext context) { return GestureDetector( onTap: () => Navigator.pop(context), child: Material( color: AppColors.black, child: SafeArea( child: Stack(children: [ Positioned.fill( child: PhotoViewGallery.builder( pageController: PageController(initialPage: initialPage ?? 0), itemCount: mediaSourceList.length, builder: (context, index) { final source = mediaSourceList[index]; if (source.split('?').first.isImageFileName) { return PhotoViewGalleryPageOptions( imageProvider: CachedNetworkImageProvider(source), minScale: PhotoViewComputedScale.contained, maxScale: PhotoViewComputedScale.covered * 2, ); } else if (source.split('?').first.isVideoFileName) { return PhotoViewGalleryPageOptions.customChild( disableGestures: true, child: _VideoPreview(source: source), ); } else { return PhotoViewGalleryPageOptions.customChild( child: const Center( child: Text( '格式不支持', style: TextStyle(color: AppColors.white), ), ), ); } }, ), ), const SafeArea( child: Padding( padding: EdgeInsets.all(4.0), child: BackButton(color: AppColors.white), ), ) ]), ), ), ); } } class _VideoPreview extends StatefulWidget { final String source; const _VideoPreview({Key? key, required this.source}) : super(key: key); @override State<_VideoPreview> createState() => __VideoPreviewState(); } class __VideoPreviewState extends State<_VideoPreview> { late final videoCtrl = VideoPlayerController.network(widget.source); @override void dispose() { videoCtrl.dispose(); super.dispose(); } Future initVideo() async { await videoCtrl.initialize(); } @override Widget build(BuildContext context) { return FutureBuilder( future: initVideo(), builder: (context, snapshot) { return Chewie( controller: ChewieController( showOptions: false, autoPlay: true, aspectRatio: videoCtrl.value.aspectRatio, videoPlayerController: videoCtrl, ), ); }, ); } }