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.

74 lines
2.1KB

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