Advertisement
Guest User

Untitled

a guest
Oct 29th, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.14 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:video_player/video_player.dart';
  5.  
  6. void main() => runApp(const StoryDetailPage());
  7.  
  8. class StoryDetailPage extends StatelessWidget {
  9.   const StoryDetailPage({Key? key}) : super(key: key);
  10.  
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return const MaterialApp(
  14.       title: 'Video Player Demo',
  15.       home: VideoPlayerScreen(),
  16.     );
  17.   }
  18. }
  19.  
  20. class VideoPlayerScreen extends StatefulWidget {
  21.   const VideoPlayerScreen({Key? key}) : super(key: key);
  22.  
  23.   @override
  24.   _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
  25. }
  26.  
  27. class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  28.   late VideoPlayerController _controller;
  29.   late Future<void> _initializeVideoPlayerFuture;
  30.  
  31.   @override
  32.   void initState() {
  33.     _controller = VideoPlayerController.asset("assets/video_1.mp4");
  34.  
  35.     _initializeVideoPlayerFuture = _controller.initialize();
  36.  
  37.     _controller.setLooping(true);
  38.  
  39.     super.initState();
  40.   }
  41.  
  42.   @override
  43.   void dispose() {
  44.     _controller.dispose();
  45.  
  46.     super.dispose();
  47.   }
  48.  
  49.   @override
  50.   Widget build(BuildContext context) {
  51.     return Scaffold(
  52.       appBar: AppBar(
  53.         title: const Text('Video'),
  54.       ),
  55.       body: FutureBuilder(
  56.         future: _initializeVideoPlayerFuture,
  57.         builder: (context, snapshot) {
  58.           if (snapshot.connectionState == ConnectionState.done) {
  59.             return AspectRatio(
  60.               aspectRatio: _controller.value.aspectRatio,
  61.               child: VideoPlayer(_controller),
  62.             );
  63.           } else {
  64.             return const Center(
  65.               child: CircularProgressIndicator(),
  66.             );
  67.           }
  68.         },
  69.       ),
  70.       floatingActionButton: FloatingActionButton(
  71.         onPressed: () {
  72.           setState(() {
  73.             if (_controller.value.isPlaying) {
  74.               _controller.pause();
  75.             } else {
  76.               _controller.play();
  77.             }
  78.           });
  79.         },
  80.         child: Icon(
  81.           _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
  82.         ),
  83.       ),
  84.     );
  85.   }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement