我想知道如何在从另一个类调用函数时停止视频onPageChanged函数,所以我很陌生,所以想在从另一个类调用函数时了解小部件通信。
下面是我的代码实际上未与该函数一起使用,实际上我直接调用了一个函数,这就是为什么我认为它不起作用,但实际上我不知道如何使用有状态或无状态来做到这一点。
class TutorialScreen extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<TutorialScreen>
with SingleTickerProviderStateMixin {
PageController _pageController;
@override
void initState() {
// TODO: implement initState
getDataByJson();
_pageController = PageController();
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getDataByJson(),
builder: (context, snapshot) {
if (complexTutorial == null) return Center(
child: CircularProgressIndicator()
);
return getBody();
},
);
}
Widget getBody() {
var size = MediaQuery.of(context).size;
return PageView(
children: [
PageView(
onPageChanged: (){
_VideoPlayerItemState _videoPlayerItemState = _VideoPlayerItemState();
_videoPlayerItemState.callToAnotherClassOnPageChanged();
},
controller: _pageController,
scrollDirection: Axis.horizontal,
children: List.generate(complexTutorial.data.length, (index) {
return VideoPlayerItem(
videoUrl: complexTutorial.data[index].tutorialLink,
size: size,
name: complexTutorial.data[index].titorialText,
);
}),
),
Container(child: Center(child: Text("USER OLD TUTORIALS"),),)
]
);
}
}
class VideoPlayerItem extends StatefulWidget {
final String videoUrl;
final String name;
VideoPlayerItem(
{Key key,
@required this.size,
this.name,
this.videoUrl})
: super(key: key);
final Size size;
@override
_VideoPlayerItemState createState() => _VideoPlayerItemState();
}
class _VideoPlayerItemState extends State<VideoPlayerItem> with AutomaticKeepAliveClientMixin,TickerProviderStateMixin{
// this is my videoPlayerController declared here
VideoPlayerController _videoController;
@override
void initState() {
// TODO: implement initState
super.initState();
_videoController = VideoPlayerController.network(widget.videoUrl)
..initialize().then((value) {
if(widget.pageControllerinsider.page.toInt() == 0) {
_videoController.play();
}
setState(() {
isShowPlaying = false;
});
});
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_videoController.dispose();
}
Widget isPlaying(){
return _videoController.value.isPlaying && !isShowPlaying ? Container() : Icon(Icons.play_arrow,size: 80,color: Colors.white.withOpacity(0.5),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
setState(() {
_videoController.value.isPlaying
? _videoController.pause()
: _videoController.play();
});
},
child: Container(
height: widget.size.height,
width: widget.size.width,
child: Stack(
children: <Widget>[
Container(
height: widget.size.height,
width: widget.size.width,
decoration: BoxDecoration(color: Colors.black),
child: Stack(
children: <Widget>[
FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _videoController.value.aspectRatio,
height: 1,
child: VideoPlayer(_videoController),
),
),
Center(
child: Container(
decoration: BoxDecoration(
),
child: isPlaying(),
),
)
],
),
),
],
)),
);
}
//这是我从上面的类中调用的函数 //我想从上面的onPageChanged函数中暂停调用此函数时的视频
callToAnotherClassOnPageChanged(){
print("VALUE GETTER");
print(_videoController.value);
if(_videoController.value.isPlaying){
_videoController.pause();
}else{
_videoController.play();
}
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
请帮助我,这要感谢StackOverFlow团队。