我正在尝试制作应用程序,其中 gridview 显示本地存储文件夹中的视频,然后单击任何视频打开滑块并播放视频。为此目的,通过向左或向右滑动,播放下一个或上一个视频
我正在使用Carousel Slider包来实现动态视频数据的滑动效果,对于播放视频我正在使用Video player包
Slider 类的代码如下
class VideoSlider extends StatefulWidget {
const VideoSlider({
Key? key,
required this.listOfVideos, // Contains all videos in folder
required this.initialIndex, // initial index used in carousel options to show current page
}) : super(key: key);
final List<String> listOfVideos;
final int initialIndex;
@override
_VideoSliderState createState() => _VideoSliderState();
}
class _VideoSliderState extends State<VideoSlider> {
List<VideoPlayerController> controller = []; // list to contain controller for all files in floder
int i = 0; // used in carousel items to specify controller list index
@override
void initState() {
super.initState();
// used for loop to add controller for all files in controller list
for (int j = 0; j < widget.listOfVideos.length; j++) {
controller.add(
VideoPlayerController.file(File(widget.listOfVideos[j]))
..initialize().then((value) {
setState(() {});
}),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CarouselSlider(
items: widget.listOfVideos.map((e) {
return controller[i].value.isInitialized
? AspectRatio(
aspectRatio: controller[i].value.aspectRatio,
child: VideoPlayer(controller[i]))
: Container(),}).toList(),
options: CarouselOptions(
height: double.infinity,
viewportFraction: 1,
initialPage: widget.initialIndex,
enlargeCenterPage: true,
enableInfiniteScroll: false,
), ),
// fab to play and pause video
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
});
},
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
从我打电话给VideoSlider的地方
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return VideoSlider(
listOfVideos: listOfFiles,
initialIndex: index,
);
}),
);
代码显示滑块具有确切的数字,但每个幻灯片放映第一个视频(零索引的视频)不合适。任何解决方案都非常感谢。