You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3KB

  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:video_player/video_player.dart';
  5. class VideoWidget extends StatefulWidget {
  6. final bool play;
  7. final bool isServerFile;
  8. final String pathFile;
  9. const VideoWidget({Key? key, required this.pathFile, required this.play, required this.isServerFile}) : super(key: key);
  10. @override
  11. _VideoWidgetState createState() => _VideoWidgetState();
  12. }
  13. class _VideoWidgetState extends State<VideoWidget> {
  14. late VideoPlayerController videoPlayerController;
  15. late Future<void> _initializeVideoPlayerFuture;
  16. @override
  17. void initState() {
  18. super.initState();
  19. if (widget.isServerFile) {
  20. videoPlayerController = VideoPlayerController.network(widget.pathFile);
  21. } else {
  22. videoPlayerController = VideoPlayerController.file(File(widget.pathFile));
  23. }
  24. _initializeVideoPlayerFuture = videoPlayerController.initialize().then((_) {
  25. // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
  26. videoPlayerController.play();
  27. videoPlayerController.setVolume(0.0);
  28. Timer.periodic(const Duration(seconds: 1), (_) {
  29. videoPlayerController.pause();
  30. });
  31. setState(() {});
  32. });
  33. }
  34. @override
  35. void dispose() {
  36. videoPlayerController.dispose();
  37. print("dispose video item");
  38. super.dispose();
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return FutureBuilder(
  43. future: _initializeVideoPlayerFuture,
  44. builder: (context, snapshot) {
  45. if (snapshot.connectionState == ConnectionState.done) {
  46. return Container(
  47. child: Container(
  48. key: PageStorageKey(widget.pathFile),
  49. child: Container(
  50. padding: const EdgeInsets.all(1),
  51. width: 100,
  52. height: 100,
  53. decoration: BoxDecoration(
  54. color: Colors.white, border: Border.all(color: Colors.red), borderRadius: const BorderRadius.all(Radius.circular(8.0))),
  55. child: VideoPlayer(videoPlayerController),
  56. ),
  57. ),
  58. );
  59. } else {
  60. return const Center(
  61. child: CircularProgressIndicator(),
  62. );
  63. }
  64. },
  65. );
  66. }
  67. }