flutter flutter_screenutil视频播放

  1. 安装插件包
    在这里插入图片描述
  2. 使用
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class VideoPlayerScreen extends StatefulWidget {
  VideoPlayerScreen({Key key, this.file}) : super(key: key);
  final file;
  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;
  var dura;
  String fileName = '';
  @override
  void initState() {
    EasyLoading.show();
    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    // or the internet.
    fileName = widget.file["name"];
    _controller = VideoPlayerController.network('http://xxx.xx.com/file-api/file/file/download?url=group1/M00/00/34/rBo-5WDQC6OEdu4lAAAAAJbtHEc842.mp4&fileName=getFile.mp4'
        );

    // Initialize the controller and store the Future for later use.
    _initializeVideoPlayerFuture = _controller.initialize();

    // Use the controller to loop the video.
    _controller.setLooping(true);

    super.initState();
    //执行监听,只要有内容就会刷新
    _controller.addListener(() {
      setState(() {
        //进度条的播放进度,用当前播放时间除视频总长度得到
        dura = _controller.value.position.inSeconds /
            _controller.value.duration.inSeconds;
      });
    });
    // _controller.play();
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    EasyLoading.dismiss();
    return Container(
        height: 220.h,
        width: 340.w,
        child: InkWell(
            child: Stack(
          alignment: Alignment.center,
          fit: StackFit.expand,
          children: [
            Container(
              height: 220.h,
              width: 340.w,
              child: AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  // Use the VideoPlayer widget to display the video.
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          height: 220.h,
                          width: 340.w,
                          margin: EdgeInsets.fromLTRB(10.w, 0.h, 10.w, 0),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(10),
                            child: VideoPlayer(_controller),
                          ),
                        ),
                      ])),
            ),
            Positioned(
                child: FlatButton(
              child: Icon(
                  _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                  size: 30,
                  color: ColorUtil.fromHex('#ffffff')),
              onPressed: () {
                setState(() {
                  // If the video is playing, pause it.
                  if (_controller.value.isPlaying) {
                    _controller.pause();
                  } else {
                    // If the video is paused, play it.
                    _controller.play();
                  }
                });
              },
            )),
            Positioned(
                left: 200.w,
                top: 160.h,
                child: FlatButton(
                  child: Icon(Icons.crop_free_outlined,
                      size: 20, color: ColorUtil.fromHex('#ffffff')),
                  onPressed: () {
                    _controller.pause();
                    Navigator.pushNamed(context, '/VideoFullScreen',
                        arguments: widget.file);
                  },
                )),
          ],
        )));
  }
}

QQ技术交流群:707 196 135  (大前端技术交流群)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值