我有一個視頻播放到想要的持續時間(計時器)。計時器完成后,它將導航到另一個螢屏。另外,如果用戶想跳過視頻轉到下一個螢屏,我有一個跳過按鈕。問題來了,如果用戶點擊跳過按鈕,計時器仍在運行,即使用戶跳過視頻,螢屏也會更新。當用戶選擇跳過時,我希望計時器停止。
代碼:
//imports
class _VideoAppState extends State<additionVideo> {
late VideoPlayerController _controller; //the controller of the video player
@override
void initState() {
super.initState();
_controller = VideoPlayerController.asset('assets.mp4')
..initialize().then((_) {
_controller.setVolume(1.0); //video sound (volume)
_controller.setLooping(false); //do not repeat the video when finish
_controller.play(); //play the video
//navigate to the question page when the video is finished
Timer(
Duration(seconds: 38), //the wanted duration for the timer
() => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen(),
)));
setState(() {});
});
}
@override
Widget build(BuildContext context) {
//fit to the screen
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return MaterialApp(
home: Stack(
children: <Widget>[
SizedBox.expand(
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: width,
height: height,
child: VideoPlayer(_controller),
),
),
),
Container(
alignment: Alignment.topRight,
padding: EdgeInsets.all(30.0),
child: NiceButtons(
//A skip button to go to the questions page
stretch: false,
startColor: Colors.lightBlueAccent,
endColor: Colors.lightBlueAccent,
borderColor: Color(0xFF3489e9),
width: 100.0,
height: 60,
borderRadius: 60,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
//Navigate to the questions page
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
_controller
.pause(); //stop the video when naviagte to the questions page
},
child: Text('skip',
textAlign: TextAlign.center,
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 20.0,
color: Colors.white,
fontFamily: 'ReadexPro-Regular',
fontWeight: FontWeight.bold))),
),
],
),
debugShowCheckedModeBanner: false,
);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
}
uj5u.com熱心網友回復:
宣布timer
late Timer timer;
初始化timer
@override
void initState() {
super.initState();
timer = Timer(
Duration(seconds: 38),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen(),
)));
setState(() {});
}
@override
void dispose() {
super.dispose();
timer.cancel();
}
取消或功能或timer
_onTap
onPressed
dispose
timer
timer.cancel();
uj5u.com熱心網友回復:
解決方案是定義對計時器的參考并在處置時取消它。
enstade:
Timer(
Duration(seconds: 38), //the wanted duration for the timer
() => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen(),
)));
寫這個:
var timer = Timer(
Duration(seconds: 38), //the wanted duration for the timer
() => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen(),
)));
和 onDispose 將是這樣的:
@override
void dispose() {
super.dispose();
_controller.dispose();
timer.cancel();
}
uj5u.com熱心網友回復:
要實作這一點,您需要在單擊跳過按鈕時取消計時器。所以在這里我在 initstate 方法中初始化計時器,當用戶單擊跳過按鈕時,計時器被取消并導航到其他頁面。
class _VideoAppState extends State<additionVideo> {
late VideoPlayerController _controller; //the controller of the video player
late Timer timer;
@override
void initState() {
super.initState();
_controller = VideoPlayerController.asset('assets.mp4')
..initialize().then((_) {
_controller.setVolume(1.0); //video sound (volume)
_controller.setLooping(false); //do not repeat the video when finish
_controller.play(); //play the video
//navigate to the question page when the video is finished
timer = Timer(
Duration(seconds: 38), //the wanted duration for the timer
() => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen(),
)));
setState(() {});
});
}
@override
Widget build(BuildContext context) {
//fit to the screen
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return MaterialApp(
home: Stack(
children: <Widget>[
SizedBox.expand(
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: width,
height: height,
child: VideoPlayer(_controller),
),
),
),
Container(
alignment: Alignment.topRight,
padding: EdgeInsets.all(30.0),
child: NiceButtons(
//A skip button to go to the questions page
stretch: false,
startColor: Colors.lightBlueAccent,
endColor: Colors.lightBlueAccent,
borderColor: Color(0xFF3489e9),
width: 100.0,
height: 60,
borderRadius: 60,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
timer.cancel();
//Navigate to the questions page
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
_controller.pause(); //stop the video when naviagte to the questions page
},
child: Text('skip',
textAlign: TextAlign.center,
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 20.0,
color: Colors.white,
fontFamily: 'ReadexPro-Regular',
fontWeight: FontWeight.bold))),
),
],
),
debugShowCheckedModeBanner: false,
);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
timer.cancel();
}
}
uj5u.com熱心網友回復:
試試看:
onTap: (finish) {
//Navigate to the questions page
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
setState(() {
_controller
.pause();
});
//stop the video when naviagte to the questions page
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453788.html