如何在Flutter中检查首次启动应用程序 [英] How to check first time app launch in Flutter

查看:98
本文介绍了如何在Flutter中检查首次启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,已经创建了我的应用程序,但是我想检查用户是否在安装后第一次打开该应用程序,我已经看到

I am a beginner in a flutter, I have created my application but I want to check if the user opens the application for the first time after installing, I have seen this article but did not know how that?

这是启动屏幕代码,该代码会在3秒钟后将用户直接移动到主屏幕,但是我想检查用户是否是第一次打开应用并将用户移动到欢迎"屏幕,还是不是用户第一次并将用户移至主屏幕.

This is the splash screen code, the code move the user directly to the Main screen after 3 sec, But I want to check if user first time opens the app and move the user to Welcome screen or if user not the first time and move the user to the Main screen.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:book_pen/main.dart';
import 'package:book_pen/Welcome.dart';

void main() {
  runApp(new MaterialApp(
    home: new SplashScreen(),
    routes: <String, WidgetBuilder>{
      '/HomePage': (BuildContext context) => new HomePage(),
      '/WelcomePage': (BuildContext context) => new WelcomePage()
    },
  ));
}

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  startTime() async {
    var _duration = new Duration(seconds: 3);

    return new Timer(_duration, navigationPageHome);
  }

  void navigationPageHome() {
    Navigator.of(context).pushReplacementNamed('/HomePage');
  }

  void navigationPageWel() {
    Navigator.of(context).pushReplacementNamed('/WelcomePage');
  }

  @override
  void initState() {
    super.initState();
    startTime();
  }

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Center(
            child: new Image.asset(
              'assets/images/SplashBack.jpg',
              width: size.width,
              height: size.height,
              fit: BoxFit.fill,
            ),
          ),
          Center(
              child: new Image.asset(
            'assets/images/BigPurppleSecSh.png',
            height: 150,
            width: 300,
          )),
        ],
      ),
    );
  }
}

推荐答案

下面是没有任何错误且说明清楚的代码

Here are code of without any error and clarifying well

class _SplashScreenState extends State<SplashScreen> {

  Future<bool> isFirstTime() async {
     var isFirstTime = SharedPref.pref.getBool('first_time');
     if (isFirstTime != null && !isFirstTime) {
       SharedPref.pref.setBool('first_time', false);
       return false;
     } else {
       SharedPref.pref.setBool('first_time', false);
       return true;
     }
  }

  @override
  void initState() {
    super.initState();

    Timer(Duration(seconds: 3), () {
    isFirstTime().then((isFirstTime) {
      isFirstTime ? print("First time") : print("Not first time");
     });
    }
   );
  }

}

这篇关于如何在Flutter中检查首次启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆