82 lines
1.9 KiB
Dart
Executable File
82 lines
1.9 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
class NetworkVideoPlayer extends StatefulWidget {
|
|
final String videoUrl;
|
|
|
|
const NetworkVideoPlayer({
|
|
super.key,
|
|
required this.videoUrl,
|
|
});
|
|
|
|
@override
|
|
State<NetworkVideoPlayer> createState() => _NetworkVideoPlayerState();
|
|
}
|
|
|
|
class _NetworkVideoPlayerState extends State<NetworkVideoPlayer> {
|
|
late VideoPlayerController _controller;
|
|
bool isInitialized = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
_controller = VideoPlayerController.networkUrl(
|
|
Uri.parse(widget.videoUrl),
|
|
)..initialize().then((_) {
|
|
if (!mounted) return;
|
|
setState(() => isInitialized = true);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return AspectRatio(
|
|
aspectRatio: isInitialized
|
|
? _controller.value.aspectRatio
|
|
: 16 / 9,
|
|
child: isInitialized
|
|
? Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
VideoPlayer(_controller),
|
|
|
|
IconButton(
|
|
iconSize: 60,
|
|
icon: Icon(
|
|
_controller.value.isPlaying
|
|
? Icons.pause_circle
|
|
: Icons.play_circle,
|
|
color: Colors.white,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_controller.value.isPlaying
|
|
? _controller.pause()
|
|
: _controller.play();
|
|
});
|
|
},
|
|
),
|
|
|
|
Positioned(
|
|
bottom: 0,
|
|
left: 0,
|
|
right: 0,
|
|
child: VideoProgressIndicator(
|
|
_controller,
|
|
allowScrubbing: true,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
}
|