flutter - 如何从父小部件访问 videoController
问题描述
所以我的childWidget(VideoCard)中有一个videoController。父小部件有 pageview.builder 和 childwidget,它显示一个视频,(视频提要列表)。我需要暂停小部件以显示每 4 页滑动一次的插页式广告。现在,如果我显示 adview,背景中的视频仍在播放。这是父小部件代码
StreamBuilder<ApiResponse<List<VideoData>>>(
stream: feedVideoBloc.videosResponseStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: SpinKitThreeBounce(
color: Colors.pinkAccent,
size: 30,
),
);
} else if (snapshot.data!.data!.isEmpty) {
return Center(
child: Text('No Videos'),
);
} else {
return Stack(
children: [
Container(
color: Colors.black,
child: PreloadPageView.builder(
itemCount: snapshot.data?.data?.length,
onPageChanged: (index) {
userSwiped++;
if (userSwiped == 4) {
userSwiped = 0;
_showInterstitialAdSwipe();
}
feedPageIndex = index;
print(feedPageIndex);
if (index == snapshot.data!.data!.length - 2) {
feedVideoBloc.getAllVideos();
}
},
pageSnapping: true,
controller: pageViewVideoController,
physics: BouncingScrollPhysics(),
scrollDirection: Axis.vertical,
preloadPagesCount: 1,
itemBuilder: (BuildContext context, int index) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return CircularProgressIndicator();
}
if (snapshot.data!.status == Status.LOADING) {}
if (snapshot.data!.status == Status.HAS_DATA) {
return VideoCard(
followUser: (bool current) {
feedVideoBloc.followUser(
snapshot.data!.data![index].user!.id!,
userModel.token!,
current,
index);
},
likeVideo: (bool current) {
feedVideoBloc.postVideoLike(
snapshot.data!.data![index].id!,
userModel.token!,
current,
index);
},
callback: (String url) {
_showInterstitialAdDownload(index, url);
},
key: Key("unique key $index"),
index: index,
videoData: snapshot.data!.data![index],
);
}
return CircularProgressIndicator();
},
),
),
Visibility(
visible: snapshot.data!.showPaginationLoader &&
feedPageIndex == snapshot.data!.data!.length - 1,
child: Center(
child: SpinKitThreeBounce(
color: Colors.pinkAccent,
size: 30,
)),
),
],
);
}
}),
这是内部 videoCard 小部件
@override
void initState() {
super.initState();
_videoController = VideoPlayerController.network(widget.videoData.url!);
_videoController.setLooping(true);
_initializeVideoPlayerFuture =
_videoController.initialize().then((value) => setState(() {}));
}
AspectRatio(
aspectRatio: _videoController.value.aspectRatio,
child: FittedBox(
fit: BoxFit.contain,
child: SizedBox(
width: _videoController.value.size.width,
height: _videoController.value.size.height,
child: FutureBuilder<void>(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return VisibilityDetector(
onVisibilityChanged: (VisibilityInfo info) {
debugPrint(
"${info.visibleFraction} of my widget is visible");
if (info.visibleFraction == 0) {
_videoController.pause();
} else {
playVideo();
}
},
key: widget.key,
child: VideoPlayer(_videoController),
);
} else
return Center(
child: SpinKitThreeBounce(
color: Colors.pinkAccent,
size: 30,
),
);
}),
),
),
),
解决方案
您仍然没有显示您在哪里声明了视频控制器变量,尽管我想我知道如何解决它,
只需在页面开头声明您的变量:
导入后立即
像这样:
//all imports
import 'flutter/material.dart';
var _videoController;
这样您就不会将控制器的使用限制为仅一个小部件
推荐阅读
- android - Android -> 在 Android 10 及更低版本上获取当前网络类型
- php - PHPMailer 250 验证
- reactjs - ReactJS图标在按钮上没有改变
- android - 在 Android 上禁用 USB 驱动程序
- multithreading - OpenMP 任务输出
- javascript - 在聚合期间从同一文档中的属性创建键值对
- html - 我如何通过 html 在 Internet Explorer 中使用瑞典语?(网页设计任务的 HTML)
- python - Python:返回值
- reactjs - 样式化组件 - 如何创建继承组件并能够将道具发送给父级的样式?
- apache-spark - SCD-2 在 Databricks 中使用 Delta