【问题标题】:stop closing showDialogue itself after 15 seconds15 秒后停止关闭 showDialogue 本身
【发布时间】:2021-06-29 20:17:15
【问题描述】:

我试图在启动应用程序时显示一个信息对话框。关闭后,会出现另一个窗口,请求许可。我在 initState 函数中调用它。它可以工作,但我注意到第一个信息对话框也会在 15 秒后自行关闭。我该如何解决?这样当用户没有关闭对话框时,应用程序就不会被进一步加载?

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(''),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

【问题讨论】:

  • showDialogIfFirstLoaded 用于显示第一个对话框?
  • @JohnJoe 是的,他显示了带有信息的弹出窗口
  • 在警报对话框中尝试Navigator.pop(context) 而不是Navigator.of(context).pop()
  • @NishuthanS 这行不通

标签: flutter dart asynchronous async-await


【解决方案1】:

如果用户是新用户,此代码会显示一个警告对话框,并且在单击按钮后,它将引导他进入另一个对话框。

我已经测试了代码,它在 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
        },
      ),],);
    },);
  }

【讨论】:

    【解决方案2】:

    您可以在第一个对话框中从Navigator 返回一个值

    Navigator.of(context).pop(true);
    prefs.setBool(keyIsFirstLoaded, false);
    

    一旦收到true,则只调用第二种方法。

    var value = await showDialogIfFirstLoaded(context);
    if(value == true) {
       await initPlatformState();
    }
    

    【讨论】:

    • 它不工作。第一个弹出窗口仍然消失,第二个弹出窗口甚至没有出现
    • showDialog 中添加barrierDismissible: false 怎么样?
    • 它也没有帮助
    • 据我所知,当第一个屏幕加载时,弹出窗口消失
    • 没有真正得到您的问题..第一个对话框何时显示?当第一个屏幕加载时,第一个对话框消失了?你能粘贴第一个屏幕的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多