如何:停止 ListView 重建 video_player 小部件?
How to: Stop ListView from rebuilding video_player widget?
我已经设法让视频在 flutter-web 上播放,但是,当我在 ListView 中滚动视频时 rebuild/reload。
滚动时如何阻止 video_player(在 ListView 中)被重建?
抱歉,代码示例太长了。我不确定如何进一步压缩它
任何帮助都会很棒!谢谢
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();
}
}
我不确定这是一个很好的修复还是只是临时修复,但这就是我设法让它工作的方式...
如果您在 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 插件不应该 rebuild/refresh 在 ListView
中
我已经设法让视频在 flutter-web 上播放,但是,当我在 ListView 中滚动视频时 rebuild/reload。
滚动时如何阻止 video_player(在 ListView 中)被重建?
抱歉,代码示例太长了。我不确定如何进一步压缩它
任何帮助都会很棒!谢谢
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();
}
}
我不确定这是一个很好的修复还是只是临时修复,但这就是我设法让它工作的方式...
如果您在 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 插件不应该 rebuild/refresh 在 ListView
中