file1 : pageViewWidg.dart
class PageViewWidg extends StatefulWidget {
final String videoUrl;
final String videoUrl2;
final String avatarImg;
PageViewWidg({
super.key,
required this.videoUrl,
required this.avatarImg,
required this.videoUrl2,
});
@override
State<PageViewWidg> createState() => _PageViewWidgState();
}
class _PageViewWidgState extends State<PageViewWidg> {
late VideoPlayerController controller;
@override
void initState() {
controller = VideoPlayerController.asset(widget.videoUrl)..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
// build Profile
buildProfuile(String profilePhoto){
return SizedBox(
width: 60,
height:60,
child: Stack(
children: [
Positioned(child: Container(
width: 50,
height: 50,
padding: const EdgeInsets.all(1),
decoration: BoxDecoration(
color:Colors.white,
borderRadius: BorderRadius.circular(25),
),
child:ClipRRect(
borderRadius: BorderRadius.circular(25),
child: Image(image: NetworkImage(profilePhoto),fit: BoxFit.cover),
),
),
),
],
),
);
}
// build Music Album
buildMusicAlbum(String profilePhoto){
return SizedBox(
width: 60,
height:60,
child:Column(
children: [
Container(
padding: const EdgeInsets.all(8),
width: 50,
height:50,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [ Colors.grey,Colors.white]
),
borderRadius: BorderRadius.circular(25),
),
child:ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Image(image: NetworkImage(profilePhoto),fit: BoxFit.cover,),
)
)
],
)
);
}
@override
Widget build(BuildContext context) {
return Expanded(
child: InkWell(
onTap:(){
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => ExpandPageView(avatarImg: widget.avatarImg,imgUrl: widget.videoUrl,controller: controller,),
));
},
child: Stack(
children: [
// AspectRatio(
// aspectRatio: controller.value.aspectRatio,
// child: VideoPlayer(controller),
// ),
controller.value.isInitialized
?
VideoPlayer(controller)
:
Container(),
Center(
child: InkWell(
onTap:(){
controller.value.isPlaying
?
setState((){
controller.pause();
})
:
setState((){
controller.play();
});
},
child:
Image.asset(
controller.value.isPlaying
?
'assets/images/pause_icon.png'
:
'assets/images/play_icon.png'
,
height:50,
)
),
),
file2 : home_screen.dart
class HomeSceen extends StatefulWidget {
const HomeSceen({super.key});
@override
State<HomeSceen> createState() => _HomeSceenState();
}
class _HomeSceenState extends State<HomeSceen> {
List<PageViewWidg> videos=[
PageViewWidg(
videoUrl: 'assets/videos/video.mp4',
videoUrl2: 'assets/videos/video.mp4',
avatarImg: 'assets/images/pic2.jpg',
),
PageViewWidg(
videoUrl: 'assets/videos/video2.mp4',
videoUrl2: 'assets/videos/video2.mp4',
avatarImg: 'assets/images/pic1.jfif',
),
PageViewWidg(
videoUrl: 'assets/videos/video3.mp4',
videoUrl2: 'assets/videos/video3.mp4',
avatarImg: 'assets/images/pic2.jpg',
),
PageViewWidg(
videoUrl: 'assets/videos/video4.mp4',
videoUrl2: 'assets/videos/video2.mp4',
avatarImg: 'assets/images/pic1.jfif',
),
PageViewWidg(
videoUrl: 'assets/videos/video2.mp4',
videoUrl2: 'assets/videos/video2.mp4',
avatarImg: 'assets/images/pic2.jpg',
),
PageViewWidg(
videoUrl: 'assets/videos/video2.mp4',
videoUrl2: 'assets/videos/video2.mp4',
avatarImg: 'assets/images/pic2.jpg',
),
];
final PageController pagecontroller=PageController(
initialPage: 0,
);
//
@override
Widget build(BuildContext context) {
// final size=MediaQuery.of(context).size;
bool like=true;
return Scaffold(
body:SafeArea(
child: PageView.builder(
itemCount: videos.length,
controller:pagecontroller,
scrollDirection: Axis.vertical,
itemBuilder: (context,index){
return Stack(
alignment: AlignmentDirectional.centerEnd,
children:[
Column(
children: [
videos[index],
// Divider(height:2,color:Color.fromARGB(255, 76, 72, 72)),
videos[index+1],
],
),
I tried to add two controller and every controller of video,and it's not work for me.
VideoPlayerController _controller1;
VideoPlayerController _controller2;
@override
void initState() {
super.initState();
_controller1 = VideoPlayerController.assets(videoUrl1)
..initialize().then((_) {
setState(() {});
});
_controller2 = VideoPlayerController.assets(videoUrl2)
..initialize().then((_) {
setState(() {});
});
}
what I need now is how to control two videos when video is start the second will stop and change their icon or imageAssets like if the icon is paused it changes to the play icon and the second video also if the first video has a pause icon another video will be change to start video.