如何:阻止ListView重建video_player小部件?

时间:2020-02-14 16:13:33

标签: flutter dart flutter-web

我设法使视频能够在Flutter-Web上播放,但是,当我在ListView中滚动视频时,将会重建/重新加载。

滚动时如何阻止在ListView中的video_player重建?

很抱歉,冗长的代码示例。.我不确定如何进一步精简

任何帮助都会很棒!谢谢

import 'package:flutter/material.dart';
import '../../widgets/video/chewie_video.dart';
import 'package:video_player/video_player.dart';

class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          Container(height: 1000),
          Center(
            child: ChewieVideo(
              videoPlayerController:
                  VideoPlayerController.asset('assets/video.mp4'),
            ),
          ),
          Container(height: 1000),
        ],
      ),
    );
  }
}
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class ChewieVideo extends StatefulWidget {
  final VideoPlayerController videoPlayerController;
  final bool looping;

  ChewieVideo({
    @required this.videoPlayerController,
    this.looping,
    Key key,
  }) : super(key: key);

  @override
  _ChewieVideoState createState() => _ChewieVideoState();
}

class _ChewieVideoState extends State<ChewieVideo> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      autoInitialize: true,
      aspectRatio: 16 / 9,
      looping: widget.looping,
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}

1 个答案:

答案 0 :(得分:0)

我不确定这是个好方法还是临时解决方法,但这就是我设法使其正常工作的方式...

如果要在ListView / CustomScrollView中使用,则需要为这两个小部件中的任何一个调整cacheExtent。我将我的值设置为1000。

我不太确定发生了什么,但是它可以正常工作...

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: primaryBlack,
      drawer: NavDrawer(),
      body: Scrollbar(
        child: CustomScrollView(
          cacheExtent: 1000,
          slivers: <Widget>[

...

执行完chewie和video_player插件后,不应在ListView内部重建/刷新