【问题标题】:How to access the videoController from parent widget如何从父小部件访问 videoController
【发布时间】:2021-07-23 15:41:21
【问题描述】:

所以我的 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,
                        ),
                      );
                  }),
            ),
          ),
        ),

【问题讨论】:

  • 你好,你需要显示整个小部件的代码......可能是整个页面......我看不到控制器的初始化位置
  • 确定我已经更新了问题,你现在可以检查代码吗。
  • 我跟着这个答案:stackoverflow.com/a/59664258/10013537

标签: flutter dart rxdart


【解决方案1】:

你仍然没有显示你在哪里声明了视频控制器变量,虽然我想我知道如何解决它,

只需在页面开头声明您的变量:

导入后立即

像这样:

//all imports
import 'flutter/material.dart';

var _videoController;

这样您就不会将控制器的使用限制为仅一个小部件

【讨论】:

  • 它是许多 videocardWidget 的 pageviewbuilder 所以我们不能使用它我需要 snapshot.data.lenght no of controllers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多