131 lines
3.9 KiB
Dart
131 lines
3.9 KiB
Dart
import 'dart:ui';
|
|
import 'package:chewie/chewie.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
class VideoPopup extends StatefulWidget {
|
|
final String videoUrl;
|
|
const VideoPopup({super.key, required this.videoUrl});
|
|
|
|
@override
|
|
VideoPopupState createState() => VideoPopupState();
|
|
}
|
|
|
|
class VideoPopupState extends State<VideoPopup> {
|
|
late VideoPlayerController _videoPlayerController;
|
|
ChewieController? _chewieController;
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializeVideo();
|
|
}
|
|
|
|
Future<void> _initializeVideo() async {
|
|
// Initialize with auto-play and preload high quality
|
|
_videoPlayerController = VideoPlayerController.networkUrl(
|
|
Uri.parse(widget.videoUrl),
|
|
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: false),
|
|
);
|
|
|
|
// Wait for initialization and prepare video for instant playback
|
|
await _videoPlayerController.initialize();
|
|
await _videoPlayerController.setPlaybackSpeed(1.0);
|
|
await _videoPlayerController.setVolume(1.0);
|
|
|
|
// Pre-buffer video data
|
|
await _videoPlayerController.play();
|
|
await _videoPlayerController.pause();
|
|
|
|
// Create chewie controller after video is ready
|
|
if (mounted) {
|
|
setState(() {
|
|
_chewieController = ChewieController(
|
|
videoPlayerController: _videoPlayerController,
|
|
autoPlay: true,
|
|
looping: false,
|
|
allowFullScreen: true,
|
|
allowPlaybackSpeedChanging: true,
|
|
showControls: true,
|
|
aspectRatio: _videoPlayerController.value.aspectRatio,
|
|
errorBuilder: (context, errorMessage) {
|
|
return Center(
|
|
child: Text(
|
|
'Error loading video: $errorMessage',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_chewieController?.dispose();
|
|
_videoPlayerController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: () => Navigator.of(context).pop(),
|
|
child: Scaffold(
|
|
backgroundColor: Colors.transparent,
|
|
body: Stack(
|
|
children: [
|
|
// Blurred background
|
|
BackdropFilter(
|
|
filter: ImageFilter.blur(sigmaX: 8, sigmaY: 8),
|
|
child: Container(color: Colors.black.withValues(alpha: 0.5)),
|
|
),
|
|
|
|
// Video player
|
|
Center(
|
|
child: AspectRatio(
|
|
aspectRatio:
|
|
_videoPlayerController.value.isInitialized
|
|
? _videoPlayerController.value.aspectRatio
|
|
: 16 / 9,
|
|
child: AnimatedSwitcher(
|
|
duration: Duration(milliseconds: 300),
|
|
child:
|
|
_isLoading
|
|
? Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
CircularProgressIndicator(color: Colors.white),
|
|
SizedBox(height: 16),
|
|
Text(
|
|
'Loading video...',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: Chewie(controller: _chewieController!),
|
|
),
|
|
),
|
|
),
|
|
|
|
// Close button
|
|
Positioned(
|
|
top: 40,
|
|
right: 20,
|
|
child: IconButton(
|
|
icon: Icon(Icons.close, color: Colors.white, size: 32),
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|