Flutter video_player点击重新播放

视频播放完成之后,暂停视频,点击重新播放

dart 复制代码
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class ListViewItemWidget extends StatefulWidget{

  @override
  State createState() {
    return _ListViewItemWidgetState();
  }
}

class _ListViewItemWidgetState extends State<ListViewItemWidget>{

  bool isPlay = false;
  bool isOver = false;
  late VideoPlayerController _controller;

  @override
  void initState() {
    _controller =  VideoPlayerController.network('https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4')
        ..initialize().then((_){
          _controller.play();
          setState(() {
            _controller.pause();
          });
        })
        ..addListener(videoListener);
  }

  @override
  void dispose() {
    _controller.dispose();
    _controller.removeListener(videoListener);
    super.dispose();
  }

  void videoListener(){
    var curPlayPosition = _controller.value.position;
    var totalPlayTime = _controller.value.duration;
    if (curPlayPosition == totalPlayTime && curPlayPosition.toString() != "0:00:00.000000"){
      isOver = true;
      isPlay = false;
      _controller.initialize()
        ..then((_) {
          setState(() {

          });
        });

      setState(() {

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //未关注的才会显示为你推荐
        Row(
          children: [
            Container(
              width: MediaQuery.of(context).size.width - 50,
              height: 50,
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: 12.0),
              child: Text("为你推荐"),
            ),
            SizedBox(
              width: 50,
              height: 50,
              child: InkWell(
                child: Icon(Icons.close),
                onTap: (){
                  /**
                   * 关闭
                   */
                },
              ),
            )
          ],
        ),

        Stack(
          children: [
            Container(
              height: 220,
              color: Colors.blueGrey,
              margin: EdgeInsets.only(top: 10),
              child: Stack(
                children: [
                  Positioned.fill(child: GestureDetector(
                      child: AspectRatio(
                        child: VideoPlayer(_controller),
                        aspectRatio: _controller.value.aspectRatio,
                      )),
                  ),
                  Container(
                    height: 40,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        /**
                         * 网络图片
                         */
                        // Image(image: Image.network(""))
                        Expanded(
                            flex: 1,
                            child:Icon(Icons.verified_user, color: Colors.white)
                        ),
                        Expanded(
                          flex: 6,
                          child:Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text("userName", style: TextStyle(fontSize: 18, color: Colors.white),),
                              Text("userName", style: TextStyle(fontSize: 12, color: Colors.white),)
                            ],
                          ),
                        ),
                        Expanded(
                            flex: 1,
                            child: Text("关注",  style: TextStyle(fontSize: 12, color: Colors.white))
                        ),
                        Expanded(
                            flex: 1,
                            child:   Icon(Icons.menu, color: Colors.white)
                        )
                      ],
                    ),
                  ),
                  buildControllerWidget()
                ],
              ),

            ),
          ],
        ),

        Container(
          margin: EdgeInsets.only(top: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              /**
               * 网络图片
               */
              // Image(image: Image.network(""))

              Expanded(
                  flex: 1,
                  child:Icon(Icons.favorite_border)
              ),
              Expanded(
                  flex: 1,
                  child:Icon(Icons.messenger_outline)
              ),
              Expanded(
                  flex: 1,
                  child:Icon(Icons.share_outlined)
              ),
              Expanded(
                flex: 4,
                child:Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                  ],
                ),
              ),
              Expanded(
                  flex: 1,
                  child:   Icon(Icons.collections_outlined)
              )
            ],
          ),
        ),
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 18,top: 20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("1732赞"),
              Text("hahhahah"),
              Text("总共23条评论"),
              Text("6天前")
            ],
          ),
        )
      ],
    );
  }

  //播放界面
  Widget buildControllerWidget(){
    if(isPlay){
      return Container(
        child: GestureDetector(
          onTap: (){
            _controller.pause();
            isPlay = false;
            setState(() {
            });
          },
        ),
      );
    }
    return  Positioned.fill(child: AspectRatio(
        aspectRatio: _controller.value.aspectRatio,
        child: Container(
          child: GestureDetector(
            onTap: (){
              if(isOver){
                isOver = false;
                _controller.play();
                isPlay = true;
                setState(() {
                });
              } else {
                _controller.play();
                isPlay = true;
                setState(() {
                });
              }
            },
            child: Icon(Icons.play_circle_fill, size: 44,),
          ),
        )),
    );
  }
}

pubspec.yaml依赖

dart 复制代码
  video_player: ^2.3.0

要求Android compileSdkVersion 为33

相关推荐
修炼者1 小时前
Android Studio的技巧
android·android studio
雨白2 小时前
ARouter 入门指南:从基本跳转到对象传递
android
用户69371750013842 小时前
17.Kotlin 类:类的形态(四):枚举类 (Enum Class)
android·后端·kotlin
h***34632 小时前
MS SQL Server 实战 排查多列之间的值是否重复
android·前端·后端
用户69371750013842 小时前
16.Kotlin 类:类的形态(三):密封类 (Sealed Class)
android·后端·kotlin
摆烂积极分子5 小时前
安卓开发学习-安卓版本
android·学习
谢斯5 小时前
编译AppFlowy
flutter
n***26566 小时前
MySQL JSON数据类型全解析(JSON datatype and functions)
android·mysql·json
t***82116 小时前
mysql的主从配置
android·mysql·adb
灰灰勇闯IT7 小时前
Flutter×鸿蒙深度融合指南:从跨端适配到分布式能力落地(2025最新实战)
分布式·flutter·harmonyos