video_back.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:flutter/material.dart';
  2. import 'package:video_player/video_player.dart';
  3. class VideoBack extends StatefulWidget {
  4. VideoBack({Key? key}) : super(key: key);
  5. _VideoBackState createState() => _VideoBackState();
  6. }
  7. class _VideoBackState extends State<VideoBack> {
  8. late VideoPlayerController _controller;
  9. bool _isPlaying = false;
  10. String url =
  11. "https://www.guojio.com/video/07a7faa1-3696-4af7-aeac-2d6cf6bf25f9.mp4";
  12. @override
  13. void initState() {
  14. super.initState();
  15. _controller = VideoPlayerController.network(this.url)
  16. // 播放状态
  17. ..addListener(() {
  18. final bool isPlaying = _controller.value.isPlaying;
  19. if (isPlaying != _isPlaying) {
  20. setState(() {
  21. _isPlaying = isPlaying;
  22. });
  23. }
  24. })
  25. // 在初始化完成后必须更新界面
  26. ..initialize().then((_) {
  27. setState(() {});
  28. });
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Container(
  33. child: _controller.value.isInitialized
  34. // 加载成功
  35. ? AspectRatio(
  36. aspectRatio: _controller.value.aspectRatio,
  37. child: VideoPlayer(_controller),
  38. )
  39. : Container(),
  40. );
  41. }
  42. }