Flutter:如何在经过一定时间后自动更改页面?
Flutter : How to automatically change the page after passing a certain time?
感谢您阅读我的问题并抽出宝贵时间。我正在尝试添加一个功能,让您在 Flutter 中经过一段时间后返回主屏幕。我已经为这个问题苦苦挣扎了 3 个星期,所以我来到了这里。
共有 4 页。
- 主屏幕()
- 第二条路线()
- 第三条路线()
- ForthRoute()
当您转到下一页或 SecondRoute() 时,计时器启动。如果转到下一页或 ThirdRoute(),计时器将重新启动。我添加这个定时器功能成功了,但是到0后就没有跳转了。
1。我的代码
下面是我的代码。
mian.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:question/fourth_route.dart';
import 'package:question/second_route.dart';
import 'package:question/third_route.dart';
import 'package:question/timer_store.dart';
import 'home_screen.dart';
void main() {
runApp(ChangeNotifierProvider(
create: (context) => TimerStore(),
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Question',
theme: ThemeData.dark(),
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/second_route': (context) => SecondRoute(),
'/third_route': (context) => ThirdRoute(),
'/fourth_route': (context) => FourthRoute(),
},
);
}
}
time_store.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'home_screen.dart';
class TimerStore with ChangeNotifier {
static const COUNTER_VALUE = 10; // TODO: ユーザーが設定できるように
var count = COUNTER_VALUE;
static const sec = const Duration(seconds: 1);
static const ms = const Duration(milliseconds: 1);
Timer _timer;
startTimer() {
if (_timer != null) {
_timer.cancel();
}
count = COUNTER_VALUE;
_timer = Timer.periodic(sec, (timer) {
updateCounter();
});
}
void updateCounter() {
if (count > 0) {
count--;
print(count);
} else {
_timer.cancel();
updateCounter2();
updateCounter4;
}
notifyListeners();
}
//追加
void updateCounter2() {
print('page trangition');
}
void updateCounter3(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => HomeScreen(),
));
}
void updateCounter4(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomeScreen()));
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
//追加 ---
void stopTimer() {
_timer.cancel();
}
void restartTimer() {
startTimer();
}
}
2。问题
为了查看函数是否正常工作,我使用了“print”并检查了控制台中的输出。我可以看到 updateCounter2() 的倒计时和“页面转换”字样。
enter image description here
void updateCounter() {
if (count > 0) {
count--;
print(count);
} else {
_timer.cancel();
updateCounter2();
updateCounter4;
}
notifyListeners();
}
//追加
void updateCounter2() {
print('page trangition');
}
但是,updateCounter4 不起作用。我知道这看起来很奇怪,因为它应该有 () 就像 updateCounter4()。但如果我这样做,我会收到一条错误消息,
1 positional argument(s) expected, but 0 found. Try adding the missing arguments.
enter image description here
我知道 updateCounter4 需要作为上下文的参数。但是我不确定到底应该在 updateCounter4 的()中添加什么...
我输入了“updateCounter4(context)”但是没有用...
希望您理解我的问题并为我提供建议。如果不够清楚,很乐意解释细节。
非常感谢。
你可以为此使用定时器
@override
void initState() {
Timer(Duration(seconds: 3), (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>
HomeScreen()));
});
super.initState();
}
这将在 3 秒后将您转到主屏幕
并确保在文件顶部导入 dart:async
import 'dart:async';
感谢您阅读我的问题并抽出宝贵时间。我正在尝试添加一个功能,让您在 Flutter 中经过一段时间后返回主屏幕。我已经为这个问题苦苦挣扎了 3 个星期,所以我来到了这里。
共有 4 页。
- 主屏幕()
- 第二条路线()
- 第三条路线()
- ForthRoute()
当您转到下一页或 SecondRoute() 时,计时器启动。如果转到下一页或 ThirdRoute(),计时器将重新启动。我添加这个定时器功能成功了,但是到0后就没有跳转了。
1。我的代码
下面是我的代码。
mian.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:question/fourth_route.dart';
import 'package:question/second_route.dart';
import 'package:question/third_route.dart';
import 'package:question/timer_store.dart';
import 'home_screen.dart';
void main() {
runApp(ChangeNotifierProvider(
create: (context) => TimerStore(),
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Question',
theme: ThemeData.dark(),
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/second_route': (context) => SecondRoute(),
'/third_route': (context) => ThirdRoute(),
'/fourth_route': (context) => FourthRoute(),
},
);
}
}
time_store.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'home_screen.dart';
class TimerStore with ChangeNotifier {
static const COUNTER_VALUE = 10; // TODO: ユーザーが設定できるように
var count = COUNTER_VALUE;
static const sec = const Duration(seconds: 1);
static const ms = const Duration(milliseconds: 1);
Timer _timer;
startTimer() {
if (_timer != null) {
_timer.cancel();
}
count = COUNTER_VALUE;
_timer = Timer.periodic(sec, (timer) {
updateCounter();
});
}
void updateCounter() {
if (count > 0) {
count--;
print(count);
} else {
_timer.cancel();
updateCounter2();
updateCounter4;
}
notifyListeners();
}
//追加
void updateCounter2() {
print('page trangition');
}
void updateCounter3(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => HomeScreen(),
));
}
void updateCounter4(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomeScreen()));
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
//追加 ---
void stopTimer() {
_timer.cancel();
}
void restartTimer() {
startTimer();
}
}
2。问题
为了查看函数是否正常工作,我使用了“print”并检查了控制台中的输出。我可以看到 updateCounter2() 的倒计时和“页面转换”字样。
enter image description here
void updateCounter() {
if (count > 0) {
count--;
print(count);
} else {
_timer.cancel();
updateCounter2();
updateCounter4;
}
notifyListeners();
}
//追加
void updateCounter2() {
print('page trangition');
}
但是,updateCounter4 不起作用。我知道这看起来很奇怪,因为它应该有 () 就像 updateCounter4()。但如果我这样做,我会收到一条错误消息,
1 positional argument(s) expected, but 0 found. Try adding the missing arguments.
enter image description here
我知道 updateCounter4 需要作为上下文的参数。但是我不确定到底应该在 updateCounter4 的()中添加什么...
我输入了“updateCounter4(context)”但是没有用...
希望您理解我的问题并为我提供建议。如果不够清楚,很乐意解释细节。
非常感谢。
你可以为此使用定时器
@override
void initState() {
Timer(Duration(seconds: 3), (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>
HomeScreen()));
});
super.initState();
}
这将在 3 秒后将您转到主屏幕
并确保在文件顶部导入 dart:async
import 'dart:async';