0

I have got a gridview which contains thumbnails for videos. Now this list of thumbnails is generated from a stream. What I need is, when user taps on any of the image the particular video should get opened in a pageview. I have already achieved this but then the video opened in the pageview does not listen to any changes.

I am not sure if I can use the same stream in the gridview as well as in the pageview.

Here's some of my code :

    StreamBuilder(
                            stream: Firestore.instance
                                .collection("videos")
                                .where("userKey", isEqualTo: userKey)
                                .where("isActive", isEqualTo: true)
                                .snapshots(),
                            builder:
                                (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                              if (snapshot.hasData) {
                                videos = snapshot.data.documents
                                    .map((doc) =>
                                        Video.fromMap(doc.data, doc.documentID))
                                    .toList();
                                return videos != null && videos.length > 0
                                    ? Stack(
                                        children: <Widget>[
                                          GridView.builder(
                                            gridDelegate:
                                                SliverGridDelegateWithFixedCrossAxisCount(
                                                    childAspectRatio:
                                                        MediaQuery.of(context)
                                                                .size
                                                                .width /
                                                            (MediaQuery.of(context)
                                                                    .size
                                                                    .height /
                                                                1.4),
                                                    crossAxisCount: 3),
                                            itemCount: videos.length,
    //                                    padding: EdgeInsets.all(8.0),
                                            itemBuilder:
                                                (BuildContext context, int index) {
                                              return Padding(
                                                  padding:
                                                      const EdgeInsets.all(1.0),
                                                  child: GestureDetector(
                                                    onLongPress: () {
                                                      Get.defaultDialog(
                                                          title: "Delete Video ?",
                                                          onConfirm: () async {
                                                            await DeleteVideo(
                                                                videos[index].id);
                                                            Get.back();
                                                          },
                                                          textConfirm: "Yes",
                                                          textCancel: "No",
                                                          confirmTextColor:
                                                              Colors.white,
                                                          cancelTextColor:
                                                              Colors.black,
                                                          buttonColor: Colors.red
                                                              .withOpacity(0.7),
                                                          backgroundColor:
                                                              Colors.white,
                                                          middleText:
                                                              "Do you want to delete this video ?");
                                                    },
                                                    onTap: () {
_pageController =
                                                      PageController(
                                                          initialPage: index);
                                                  currentlyPlayingVideoKey =
                                                      videos[index].id;
                                                  Get.dialog(
                                                      setupAlertDialoadContainer());

// opens a complete new window for each video
                                                      //Get.to(ProfileSingleVideo(
                                                          //videoKey:
                                                              //videos[index].id,
                                                          //goBackForMusic: true));
                                                    },



  Widget setupAlertDialoadContainer(String page) {
    showModalBottomSheet(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(25.0)),
        ),
        context: context,
        isScrollControlled: true,
        builder: (context) => Container(
              child: new PageView.builder(
                  controller: _pageController,
                  itemCount: videos.length,
                  scrollDirection: Axis.vertical,
                  pageSnapping: true,
                  reverse: false,
                  onPageChanged: (page) {
                    setState(() {
                      currentlyPlayingVideoKey = videos[page].id;
                    });
                  },
                  itemBuilder: (context, position) {
                    return pageViewContent(video: videos[position]);
                  }),
            ));
  }
}

Again, I am able to open the videos in the pageview and jump to the video that I want to on tap. But, the video does not listen to the changes that i make when video gets opened in the pageview. E.g. liking that video does not increement its counter although liking does work in other screens where pageview is placed directly inside of the Streambuilder. I dont want to place pageview inside the streambuider in this case though.

Screenshot below to imagine it better.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
rohan koshti
  • 313
  • 4
  • 17

1 Answers1

0

I think what you need to use is a StreamProvider. And place it above your MaterialApp in main so that you can access its Stream even when you use the Navigator.

You can then set the PageView as the Consumer of the StreamProvider. by Wrapping it in Consumer Widget.

I hope this solves your problem of your page view not being able to listen to changes.

Tonny Bawembye
  • 496
  • 5
  • 16