0

GridView.builder我通过以下代码在我的应用程序中显示视频视图:

GridView.builder(
              shrinkWrap: true,
              itemCount: video.length,
              // physics: NeverScrollableScrollPhysics(),
              scrollDirection: Axis.vertical,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  crossAxisSpacing: 4.0,
                  mainAxisSpacing: 4.0),
              itemBuilder: (BuildContext context, int index) {
                return Column(
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Stack(
                      children: <Widget>[
                        Container(
                          decoration: new BoxDecoration(color: Colors.white),
                          alignment: Alignment.center,
                          height: 150,
                          child: InkWell(
                            //  child: VimeoPlayer(id: video[index].description, autoPlay: false),
                            child: VideoWidget(
                                url: video[index].imageUrl!, play: false),
                            onLongPress: () {
                              setState(() {
                                pressd = true;
                                _selectedIndex = index;
                              });
                            },
                          ),
                        ),
                        pressd != false &&
                                _selectedIndex != null &&
                                _selectedIndex == index
                            ? Positioned(
                                left: 0,
                                right: 0,
                                top: 0,
                                bottom: 0,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    InkWell(
                                      child: Icon(Icons.image_search_sharp),
                                      onTap: () async {
                                        result = await Navigator.of(context)
                                            .push(new MaterialPageRoute(
                                                builder: (BuildContext
                                                        context) =>
                                                    ChangeVideo(
                                                      imageUrl: video[index]
                                                          .imageUrl!,
                                                      keyWord:
                                                          video[index].keyWord!,
                                                      description: images[index]
                                                          .description!,
                                                      type: video[index].type!,
                                                    )));
                                        print(result!.length);
                                        if (result != null) {
                                       setState(() {
                                         video.removeAt(index);
                                         video.insertAll(index, result!);
                                       });
                                        }
                                      },
                                    ),
                                    Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        InkWell(
                                          child:
                                              Icon(Icons.delete_forever_sharp),
                                          onTap: () async {
                                          setState(() {
                                            video.removeAt(index);
                                          });
                                          },
                                        )
                                      ],
                                    )
                                  ],
                                ),
                              )
                            : SizedBox()
                      ],
                    ),
                    Text(video[index].keyWord!),
                  ],
                );
              },
            ),

我的VideoWidget班级有带有 Chewie 插件的视频播放器

class VideoWidget extends StatefulWidget {

  final bool play;
  final String url;

  const VideoWidget({Key? key, required this.url, required this.play})
      : super(key: key);

  @override
  _VideoWidgetState createState() => _VideoWidgetState();

}


class _VideoWidgetState extends State<VideoWidget> {
  late VideoPlayerController videoPlayerController ;
  late Future<void> _initializeVideoPlayerFuture;

  @override
  void initState() {
    super.initState();
    videoPlayerController = new VideoPlayerController.network(widget.url);

    _initializeVideoPlayerFuture = videoPlayerController.initialize().then((_) {
      //       Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(() {});
    });
  } // This closing tag was missing

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


  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: _initializeVideoPlayerFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return new Container(

            child: Card(
              key: new PageStorageKey(widget.url),
              elevation: 5.0,
              child: Column(
                children: <Widget>[
                Expanded(child:  Chewie(
                  key: new PageStorageKey(widget.url),
                  controller: ChewieController(
                    videoPlayerController: videoPlayerController,
                    aspectRatio: 3 / 2,
                    allowFullScreen: true,
                    // Prepare the video to be played and display the first frame
                    autoInitialize: true,
                    looping: false,
                    autoPlay: false,
                    // Errors can occur for example when trying to play a video
                    // from a non-existent URL
                    errorBuilder: (context, errorMessage) {
                      return Center(
                        child: Text(
                          errorMessage,
                          style: TextStyle(color: Colors.white),
                        ),
                      );
                    },
                  ),
                ),)
                ],
              ),
            ),
          );
        }
        else {
          return Center(
            child: CircularProgressIndicator(),);
        }
      },
    );
  }
}

现在,我想从调用ChangeVideo类更新gridview数据,即返回数据result,我试图通过以下方式更新数据:

                           setState(() {
                                         video.removeAt(index);
                                         video.insertAll(index, result!);
                                       });

但是我的 gridview 没有更新数据,那么我该如何实现呢?

4

0 回答 0