play_btn.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. part of 'control_board.dart';
  2. class _PlayButton extends StatefulWidget {
  3. @override
  4. State<StatefulWidget> createState() => _PlayButtonState();
  5. }
  6. class _PlayButtonState extends State<_PlayButton> {
  7. final playerController = Get.find<IPlayerController>() as VidPlayerController;
  8. @override
  9. void initState() {
  10. WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
  11. if (mounted) {
  12. playerController.eventHandler.addListener(onControllerEvent);
  13. }
  14. });
  15. super.initState();
  16. }
  17. @override
  18. void dispose() {
  19. playerController.eventHandler.removeListener(onControllerEvent);
  20. super.dispose();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. final playing = playerController.playing;
  25. const iconSize = 36.0;
  26. return IconButton(
  27. splashRadius: 2,
  28. padding: EdgeInsets.zero,
  29. onPressed: () {
  30. if (playing) {
  31. playerController.pause();
  32. } else {
  33. playerController.play();
  34. }
  35. },
  36. icon: playing
  37. ? const Icon(Icons.stop_circle_outlined, size: iconSize)
  38. : const Icon(Icons.play_circle_outline, size: iconSize),
  39. );
  40. }
  41. void onControllerEvent(Object sender, VidPlayerEvent e) {
  42. if (e is VidPlayerStatusChangeEvent) {
  43. onPlayStatusChanged(e);
  44. }
  45. }
  46. void onPlayStatusChanged(VidPlayerStatusChangeEvent e) {
  47. if (mounted) {
  48. setState(() {});
  49. }
  50. }
  51. }