Flutter播放器 chewie-0.9.8 自定義UI,滑動(dòng)手勢

修改后大致效果如下圖


AAAAAAAAAAAA.png
  • 頂部添加返回按鈕
  • 支持手勢滑動(dòng)快進(jìn)快退
import 'dart:async';

import 'package:chewie/src/chewie_player.dart';
import 'package:chewie/src/chewie_progress_colors.dart';
import 'package:chewie/src/material_progress_bar.dart';
import 'package:chewie/src/utils.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class MaterialControls extends StatefulWidget {
  const MaterialControls({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MaterialControlsState();
  }
}

class _MaterialControlsState extends State<MaterialControls> {
  VideoPlayerValue _latestValue;
  double _latestVolume;
  bool _hideStuff = true;
  Timer _hideTimer;
  Timer _initTimer;
  Timer _showAfterExpandCollapseTimer;
  bool _dragging = false;
  bool _displayTapped = false;
  double dragValue = 0;

  final barHeight = 48.0;
  final marginSize = 5.0;

  VideoPlayerController controller;
  ChewieController chewieController;

  @override
  Widget build(BuildContext context) {
    if (_latestValue.hasError) {
      return chewieController.errorBuilder != null
          ? chewieController.errorBuilder(
        context,
        chewieController.videoPlayerController.value.errorDescription,
      )
          : Center(
        child: Icon(
          Icons.error,
          color: Colors.white,
          size: 42,
        ),
      );
    }
    ///添加滑動(dòng)手勢處理  以及頂部退出按鈕啥的
    return MouseRegion(
      onHover: (_) {
        _cancelAndRestartTimer();
      },
      child: GestureDetector(
        onHorizontalDragUpdate: (detail) {
          print('${detail.delta.dx}');
          dragValue += detail.delta.dx / 2;
        },
        onHorizontalDragEnd: (detail) {
          print(dragValue);
          controller.seekTo(Duration(seconds: controller.value.position.inSeconds + dragValue.floor()));
          isShowProgressRate(false);
        },
        onHorizontalDragStart: (details) {
          dragValue = 0;
          isShowProgressRate(true);
        },
        onTap: () => _cancelAndRestartTimer(),
        child: AbsorbPointer(
          absorbing: _hideStuff,
          child: Column(
            children: <Widget>[
              _buildTopBar(context),
              buildProgressRate(),
              _latestValue != null &&
                  !_latestValue.isPlaying &&
                  _latestValue.duration == null ||
                  _latestValue.isBuffering
                  ? const Expanded(
                child: const Center(
                  child: const CircularProgressIndicator(),
                ),
              )
                  : _buildHitArea(),
              _buildBottomBar(context),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _dispose();
    super.dispose();
  }

  void _dispose() {
    controller.removeListener(_updateState);
    _hideTimer?.cancel();
    _initTimer?.cancel();
    _showAfterExpandCollapseTimer?.cancel();
  }

  @override
  void didChangeDependencies() {
    final _oldController = chewieController;
    chewieController = ChewieController.of(context);
    controller = chewieController.videoPlayerController;

    if (_oldController != chewieController) {
      _dispose();
      _initialize();
    }

    super.didChangeDependencies();
  }

  ///創(chuàng)建頂部返回按鈕
  AnimatedOpacity _buildTopBar(BuildContext context,) {
    final iconColor = Theme
        .of(context)
        .textTheme
        .button
        .color;


    return AnimatedOpacity(
      opacity: _hideStuff ? 0.0 : 1.0,
      duration: Duration(milliseconds: 300),
      child: Container(color: Colors.black38, width: double.infinity, height: 48, child: Row(children: <Widget>[
        InkWell(
          onTap: () {
            Navigator.of(context).maybePop();
//        chewieController.toggleFullScreen();
          }, child: Padding(child: Icon(Icons.arrow_back, color: Colors.white,),
          padding: EdgeInsets.symmetric(horizontal: 10),),),
        Text('${chewieController.videoName}', style: TextStyle(color: Colors.white,),)
      ],),),
    );
  }

  ///底部狀態(tài)欄高度,修改了下透明度
  AnimatedOpacity _buildBottomBar(BuildContext context,) {
    final iconColor = Theme
        .of(context)
        .textTheme
        .button
        .color;
    return AnimatedOpacity(
      opacity: _hideStuff ? 0.0 : 1.0,
      duration: Duration(milliseconds: 500),
      child: Container(
        height: barHeight,
        color: Colors.black38,
        child: Row(
          children: <Widget>[
            _buildPlayPause(controller),
            chewieController.isLive ? Expanded(child: const Text('LIVE')) : _buildPosition(iconColor),
            chewieController.isLive ? const SizedBox() : _buildProgressBar(),
            chewieController.allowMuting ? _buildMuteButton(controller) : Container(),
            chewieController.allowFullScreen ? _buildExpandButton() : Container(),
          ],
        ),
      ),
    );
  }

  GestureDetector _buildExpandButton() {
    return GestureDetector(
      onTap: _onExpandCollapse,
      child: AnimatedOpacity(
        opacity: _hideStuff ? 0.0 : 1.0,
        duration: Duration(milliseconds: 300),
        child: Container(
          height: barHeight,
          margin: EdgeInsets.only(right: 12.0),
          padding: EdgeInsets.only(
            left: 8.0,
            right: 8.0,
          ),
          child: Center(
            child: Icon(
              chewieController.isFullScreen ? Icons.fullscreen_exit : Icons.fullscreen
              , color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  Expanded _buildHitArea() {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          if (_latestValue != null && _latestValue.isPlaying) {
            if (_displayTapped) {
              setState(() {
                _hideStuff = true;
              });
            } else
              _cancelAndRestartTimer();
          } else {
            _playPause();

            setState(() {
              _hideStuff = true;
            });
          }
        },
        child: Container(
          color: Colors.transparent,
          child: Center(
            child: AnimatedOpacity(
              opacity:
              _latestValue != null && !_latestValue.isPlaying && !_dragging
                  ? 1.0
                  : 0.0,
              duration: Duration(milliseconds: 300),
              child: GestureDetector(
                child: Container(
                  decoration: BoxDecoration(
                    color: Theme
                        .of(context)
                        .dialogBackgroundColor,
                    borderRadius: BorderRadius.circular(48.0),
                  ),
                  child: Padding(
                    padding: EdgeInsets.all(12.0),
                    child: Icon(Icons.play_arrow, size: 32.0),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }


  GestureDetector _buildMuteButton(VideoPlayerController controller,) {
    return GestureDetector(
      onTap: () {
        _cancelAndRestartTimer();

        if (_latestValue.volume == 0) {
          controller.setVolume(_latestVolume ?? 0.5);
        } else {
          _latestVolume = controller.value.volume;
          controller.setVolume(0.0);
        }
      },
      child: AnimatedOpacity(
        opacity: _hideStuff ? 0.0 : 1.0,
        duration: Duration(milliseconds: 300),
        child: ClipRect(
          child: Container(
            child: Container(
              height: barHeight,
              padding: EdgeInsets.only(
                left: 8.0,
                right: 8.0,
              ),
              child: Icon(
                (_latestValue != null && _latestValue.volume > 0) ? Icons.volume_up : Icons.volume_off,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }

  GestureDetector _buildPlayPause(VideoPlayerController controller) {
    return GestureDetector(
      onTap: _playPause,
      child: Container(
        height: barHeight,
        color: Colors.transparent,
        margin: EdgeInsets.only(left: 8.0, right: 4.0),
        padding: EdgeInsets.only(
          left: 12.0,
          right: 12.0,
        ),
        child: Icon(
          controller.value.isPlaying ? Icons.pause : Icons.play_arrow, color: Colors.white,
        ),
      ),
    );
  }

  Widget _buildPosition(Color iconColor) {
    final position = _latestValue != null && _latestValue.position != null
        ? _latestValue.position
        : Duration.zero;
    final duration = _latestValue != null && _latestValue.duration != null
        ? _latestValue.duration
        : Duration.zero;

    return Padding(
      padding: EdgeInsets.only(right: 24.0),
      child: Text(
        '${formatDuration(position)} / ${formatDuration(duration)}',
        style: TextStyle(
            fontSize: 14.0,
            color: Colors.white
        ),
      ),
    );
  }

  void _cancelAndRestartTimer() {
    _hideTimer?.cancel();
    _startHideTimer();

    setState(() {
      _hideStuff = false;
      _displayTapped = true;
    });
  }

  Future<Null> _initialize() async {
    controller.addListener(_updateState);

    _updateState();

    if ((controller.value != null && controller.value.isPlaying) ||
        chewieController.autoPlay) {
      _startHideTimer();
    }

    if (chewieController.showControlsOnInitialize) {
      _initTimer = Timer(Duration(milliseconds: 200), () {
        setState(() {
          _hideStuff = false;
        });
      });
    }
  }

  void _onExpandCollapse() {
    setState(() {
      _hideStuff = true;

      chewieController.toggleFullScreen();
      _showAfterExpandCollapseTimer = Timer(Duration(milliseconds: 300), () {
        setState(() {
          _cancelAndRestartTimer();
        });
      });
    });
  }

  void _playPause() {
    bool isFinished = _latestValue.position >= _latestValue.duration;

    setState(() {
      if (controller.value.isPlaying) {
        _hideStuff = false;
        _hideTimer?.cancel();
        controller.pause();
      } else {
        _cancelAndRestartTimer();

        if (!controller.value.initialized) {
          controller.initialize().then((_) {
            controller.play();
          });
        } else {
          if (isFinished) {
            controller.seekTo(Duration(seconds: 0));
          }
          controller.play();
        }
      }
    });
  }

  void _startHideTimer() {
    _hideTimer = Timer(const Duration(seconds: 3), () {
      setState(() {
        _hideStuff = true;
      });
    });
  }

  void _updateState() {
    setState(() {
      _latestValue = controller.value;
    });
  }

  Widget _buildProgressBar() {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.only(right: 20.0),
        child: MaterialVideoProgressBar(
          controller,
          onDragStart: () {
            setState(() {
              _dragging = true;
            });

            _hideTimer?.cancel();
          },
          onDragEnd: () {
            setState(() {
              _dragging = false;
            });

            _startHideTimer();
          },
          colors: chewieController.materialProgressColors ??
              ChewieProgressColors(
                  playedColor: Theme
                      .of(context)
                      .accentColor,
                  handleColor: Theme
                      .of(context)
                      .accentColor,
                  bufferedColor: Theme
                      .of(context)
                      .backgroundColor,
                  backgroundColor: Theme
                      .of(context)
                      .disabledColor),
        ),
      ),
    );
    print(controller.value.buffered.length);
    return Expanded(
      child: VideoProgressIndicator(controller, allowScrubbing: true,
        colors: VideoProgressColors(
            backgroundColor: Colors.blue, playedColor: Colors.green, bufferedColor: Colors.red),
      ),
    );
  }

  ///顯示快進(jìn)快退的時(shí)間
  Widget buildProgressRate() {
    return Visibility(visible: isShowProgressRote, child: Container(
      height: 50,
      width: 250,
      child: Row(children: <Widget>[
        Text('${formatDuration(Duration(seconds: controller.value.position.inSeconds.floor() + dragValue.floor()))} /',
          style: TextStyle(fontSize: 26, color: Colors.white,),),
        Text('${controller.value.duration == null ? 0 : formatDuration(controller.value.duration)}',
            style: TextStyle(fontSize: 26, color: Colors.white))
      ], mainAxisAlignment: MainAxisAlignment.center,),
      decoration: BoxDecoration(color: Colors.black38),
      alignment: Alignment.center,),);
  }

  bool isShowProgressRote = false;

  ///是否顯示哪個(gè)進(jìn)度
  void isShowProgressRate(bool isShow) {
    isShowProgressRote = isShow;
    setState(() {});
  }
}

覆蓋chewie-0.9.8+1\lib\src\material_progress_bar.dart文件 對應(yīng)版本為0.9.8(Android 有效Ios需自行修改cupertino_progress_bar.dart)

改動(dòng)處添加了注釋 搜索///即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末须揣,一起剝皮案震驚了整個(gè)濱河市兑障,隨后出現(xiàn)的幾起案子姑躲,更是在濱河造成了極大的恐慌棠涮,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件比吭,死亡現(xiàn)場離奇詭異绽族,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)梗逮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門项秉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人慷彤,你說我怎么就攤上這事娄蔼〔烙鳎” “怎么了?”我有些...
    開封第一講書人閱讀 165,933評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵岁诉,是天一觀的道長锚沸。 經(jīng)常有香客問我,道長涕癣,這世上最難降的妖魔是什么哗蜈? 我笑而不...
    開封第一講書人閱讀 58,976評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮坠韩,結(jié)果婚禮上距潘,老公的妹妹穿的比我還像新娘。我一直安慰自己只搁,他們只是感情好音比,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評(píng)論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著氢惋,像睡著了一般洞翩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上焰望,一...
    開封第一講書人閱讀 51,775評(píng)論 1 307
  • 那天骚亿,我揣著相機(jī)與錄音,去河邊找鬼熊赖。 笑死来屠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的秫舌。 我是一名探鬼主播的妖,決...
    沈念sama閱讀 40,474評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼绣檬,長吁一口氣:“原來是場噩夢啊……” “哼足陨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起娇未,我...
    開封第一講書人閱讀 39,359評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤墨缘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后零抬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體镊讼,經(jīng)...
    沈念sama閱讀 45,854評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評(píng)論 3 338
  • 正文 我和宋清朗相戀三年平夜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蝶棋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,146評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忽妒,死狀恐怖玩裙,靈堂內(nèi)的尸體忽然破棺而出兼贸,到底是詐尸還是另有隱情,我是刑警寧澤吃溅,帶...
    沈念sama閱讀 35,826評(píng)論 5 346
  • 正文 年R本政府宣布溶诞,位于F島的核電站,受9級(jí)特大地震影響决侈,放射性物質(zhì)發(fā)生泄漏螺垢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評(píng)論 3 331
  • 文/蒙蒙 一赖歌、第九天 我趴在偏房一處隱蔽的房頂上張望枉圃。 院中可真熱鬧,春花似錦庐冯、人聲如沸讯蒲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽墨林。三九已至,卻和暖如春犯祠,著一層夾襖步出監(jiān)牢的瞬間旭等,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評(píng)論 1 272
  • 我被黑心中介騙來泰國打工衡载, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留搔耕,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,420評(píng)論 3 373
  • 正文 我出身青樓痰娱,卻偏偏與公主長得像弃榨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子梨睁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評(píng)論 2 356