Flutter 15秒后停止关闭showDialogue本身
我试图在启动应用程序时显示一个信息对话框。关闭后,会出现另一个窗口,请求许可。我在initState函数中调用它。它可以工作,但我注意到,当15秒过去后,第一个信息对话框也会自动关闭。我该如何解决这个问题?这样,当用户未关闭对话框时,应用程序将不会进一步加载Flutter 15秒后停止关闭showDialogue本身,flutter,dart,asynchronous,async-await,Flutter,Dart,Asynchronous,Async Await,我试图在启动应用程序时显示一个信息对话框。关闭后,会出现另一个窗口,请求许可。我在initState函数中调用它。它可以工作,但我注意到,当15秒过去后,第一个信息对话框也会自动关闭。我该如何解决这个问题?这样,当用户未关闭对话框时,应用程序将不会进一步加载 class _MyAppState extends State<MyApp> { final keyIsFirstLoaded = 'is_first_loaded'; @override void initState()
class _MyAppState extends State<MyApp> {
final keyIsFirstLoaded = 'is_first_loaded';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
final context = MyApp.navKey.currentState.overlay.context;
await showDialogIfFirstLoaded(context);
await initPlatformState();
});
}
showDialogIfFirstLoaded(BuildContext context, prefs) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isFirstLoaded = prefs.getBool(keyIsFirstLoaded);
if (isFirstLoaded == null) {
return showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return new AlertDialog(
// title: new Text("title"),
content: new Text("//"),
actions: <Widget>[
new FlatButton(
child: new Text(".."),
onPressed: () {
Navigator.of(context).pop();
prefs.setBool(keyIsFirstLoaded, false);
},
),
],
);
},
);
}
}
initPlatformState() async {
print('Initializing...');
await BackgroundLocator.initialize();
print('Initialization done');
final _isRunning = await BackgroundLocator.isRegisterLocationUpdate();
setState(() {
isRunning = _isRunning;
});
onStart();
print('Running ${isRunning.toString()}');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
navigatorKey:MyApp.navKey,
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: analytics),
],
debugShowCheckedModeBanner: false,
title: '',
theme: ThemeData(),
home: new SplashScreen(),}
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
Timer _timer;
bool _visible = true;
startTime() async {
_timer = Timer(new Duration(seconds: 5), navigationPage);
}
void navigationPage() {
Navigator.of(context).pushReplacementNamed('/home');
}
@override
void initState() {
_timer = Timer(Duration(seconds: 4),
() => setState(
() {
_visible = !_visible;
},
),
);
startTime();
super.initState();
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
Container(
width: double.infinity,
child: Image.asset('images/bg.jpg',
fit: BoxFit.cover,
height: 1200,
),
),
Container(
width: double.infinity,
height: 1200,
color: Color.fromRGBO(0, 0, 0, 0.8),
),
Container(
alignment: Alignment.center,
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
child: Text(''),
),
),
],
),
),
],
);
}
}
class\u MyAppState扩展状态{
final keyIsFirstLoaded='is_first_loaded';
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback(())异步{
最终上下文=MyApp.navKey.currentState.overlay.context;
等待显示对话框iffirstload(上下文);
等待initPlatformState();
});
}
showDialogIfFirstLoaded(BuildContext上下文,prefs)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
bool isfirstload=prefs.getBool(keyisfirstload);
如果(isFirstLoaded==null){
返回显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回新警报对话框(
//标题:新文本(“标题”),
内容:新文本(“/”),
行动:[
新扁平按钮(
子项:新文本(“…”),
已按下:(){
Navigator.of(context.pop();
prefs.setBool(keyIsFirstLoaded,false);
},
),
],
);
},
);
}
}
initPlatformState()异步{
打印('初始化…');
等待BackgroundLocator.initialize();
打印(“初始化完成”);
final _isRunning=等待BackgroundLocator.isRegisterLocationUpdate();
设置状态(){
isRunning=_isRunning;
});
onStart();
打印('Running${isRunning.toString()}');
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
本地化授权:[
//…此处为特定于应用程序的本地化委托
GlobalMaterialAllocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalSuperTinoLocalizations.delegate,
],
导航工作:MyApp.navKey,
导航观察者:[
FirebaseAnalyticsSobServer(分析:分析),
],
debugShowCheckedModeBanner:false,
标题:“”,
主题:主题数据(),
主页:新建SplashScreen(),}
类SplashScreen扩展StatefulWidget{
@凌驾
_SplashScreenState createState()=>new_SplashScreenState();
}
类_SplashScreenState使用SingleTickerProviderStateMixin扩展状态{
定时器(u定时器),;
bool_visible=真;
startTime()异步{
_计时器=计时器(新的持续时间(秒:5),导航页面);
}
无效导航页(){
Navigator.of(context.pushReplacementNamed('/home');
}
@凌驾
void initState(){
_计时器=计时器(持续时间(秒:4),
()=>设置状态(
() {
_可见=!\u可见;
},
),
);
开始时间();
super.initState();
}
@凌驾
无效处置(){
_timer.cancel();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回新堆栈(
儿童:[
容器(
宽度:double.infinity,
子项:Image.asset('images/bg.jpg',
适合:BoxFit.cover,
身高:1200,
),
),
容器(
宽度:double.infinity,
身高:1200,
颜色:color.fromRGBO(0,0,0,0.8),
),
容器(
对齐:对齐.center,
孩子:排(
儿童:[
扩大(
弹性:2,
子:容器(
子项:文本(“”),
),
),
],
),
),
],
);
}
}
您可以在第一个对话框中从导航器返回值
Navigator.of(context).pop(true);
prefs.setBool(keyIsFirstLoaded, false);
一旦接收到true,则只调用第二个方法
var value = await showDialogIfFirstLoaded(context);
if(value == true) {
await initPlatformState();
}
如果用户是新用户,此代码将显示一个警报对话框,单击按钮后,它将引导他进入另一个对话框
我已经测试了代码,15秒后它还没有关闭。我仍然不确定你想要完成什么,但我希望这能有所帮助
初始状态
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await dialog1(context);
//await initPlatformState();
});
super.initState();
}
警报对话框1
dialog1(BuildContext context)async{
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isFirstLoaded = prefs.getBool("keyIsFirstLoaded")??true;
if (isFirstLoaded) {
showDialog(
barrierDismissible: false, //disables user from dismissing the dialog by clicking out of the dialog
context: context, builder: (ctx) {
return AlertDialog(
title: Text("dialog 1"), content: Text("Content"), actions: [
TextButton(
child: new Text(".."),
onPressed: () async{
Navigator.pop(ctx);
await dialog2(context);
prefs.setBool("keyIsFirstLoaded", false);
},
),],);
},);
}else{
//not first time
}
}
警报对话框2
void dialog2(BuildContext context)async{
print("dialog 2");
showDialog(context: context, builder: (context) {
return AlertDialog(title: Text("Dialog 2"),content: Text("permissions"),actions: [
TextButton(
child: new Text("close"),
onPressed: () async{
Navigator.pop(context);
//await dialog1(context); //uncomment if you want to go back to dialoge 1
},
),],);
},);
}
showDialogIfFirstLoaded
用于显示第一个对话框?@JohnJoe是的,他用infotryNavigator.pop(上下文)
显示弹出窗口,而不是Navigator.of(上下文).pop()
在警报中dialog@NishuthanS它不工作,不工作。第一个弹出窗口仍然消失,第二个弹出窗口甚至没有出现。在showDialog
中添加障碍ismissible:false
怎么样?据我所知,当第一个屏幕加载时,弹出窗口消失并没有真正理解您的问题。当将显示第一个对话框?当第一个屏幕加载时,第一个对话框消失?您可以粘贴第一个屏幕的代码吗?