启动画面和一次 flutter 介绍

splash screen and one time intro in flutter

我希望我的启动画面始终出现在我的应用程序中,这很好,但是我在启动画面之后进行了一次浏览,我希望它是一次性浏览,所以我想添加共享首选项的整数,值为 0,每次我打开启动画面时,该值都会递增 1,因此当 "number" 在第二个 运行 等于 1 或更大时,启动画面会跳过演练然后回家,这是我现在要编辑的代码:

void initState() {
    // TODO: implement initState
    super.initState();
    Timer(Duration(seconds: 5), () => MyNavigator.goToIntro(context));
}

我希望它像:

void initState() {
    // TODO: implement initState
    super.initState();int number=0;//this is in the shared prefs
    Timer(Duration(seconds: 5), () => if(number==0){MyNavigator.goToIntro(context));
    }else{MyNavigator.goToHome(context));
    number++;}
}

我们需要在多个应用启动时保存 number 值。我们可以使用 shared_preference 插件来实现这一点。

下面的代码可以像我们预期的那样完美打印(仅在首次启动期间,"First launch")。您可以使用导航逻辑而不是打印。

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

void setValue() async {
    final prefs = await SharedPreferences.getInstance();
    int launchCount = prefs.getInt('counter') ?? 0;
    prefs.setInt('counter', launchCount + 1);
    if (launchCount == 0) {
        print("first launch"); //setState to refresh or move to some other page
    } else {
        print("Not first launch");
    }
}

首先,让我们将数据保存为布尔值

Future<void> saveData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    bool isIntroScreenOpened = true;
    sharedPreferences.setBool("isIntroScreenOpened", isIntroScreenOpened); // saved data to your device.
}

其次,getData 保存在我们的设备中。

Future<bool> getSaveData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    bool isIntroScreenOpenedBefore =
        sharedPreferences.getBool("isIntroScreenOpened") ?? false;
    print(sharedPreferences.containsKey("isIntroScreenOpened")); // check your key either it is save or not? 

    if (isIntroScreenOpenedBefore == true) {
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return LoginBoard();
      }));
    } else {
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return WalKThroughScreen();
      }));
    }
    return isIntroScreenOpenedBefore;
  }

@Dinesh Balasubramanian 的回答非常好。

但是我有 4 个初始屏幕需要显示一次。我已经在每个屏幕中使用相同的逻辑来做到这一点。然后我的应用程序第二次显示第 5 个屏幕,就像快进所有上一个屏幕并停止在第 5 个屏幕上。

为了解决这个问题,我在 main.dart 获取所有设置的首选项以直接打开第 5 个屏幕。但是当我这样做时我遇到了这个问题,

"E/flutter (32606): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.

E/flutter (32606): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget."

这是从 main.dart 切换的代码:

int firstLogin, firstMobile, firstOtp, firstInfo;
  void setValue() async {
    final prefs = await SharedPreferences.getInstance();
    firstLogin = prefs.getInt('counterLogin') ?? 0;
    firstMobile = prefs.getInt('counterMobile') ?? 0;
    firstOtp = prefs.getInt('counterOtp') ?? 0;
    firstInfo = prefs.getInt('counterInfo') ?? 0;

    prefs.setInt('counterLogin', firstLogin + 1);
    prefs.setInt('counterMobile', firstMobile + 1);
    prefs.setInt('counterOtp', firstOtp + 1);
    prefs.setInt('counterInfo', firstInfo + 1);

    if ((firstLogin == 0) && (firstMobile == 0) && (firstOtp == 0) && (firstInfo == 0)) {
      setState(() {
        print("first launch");
        Navigator.of(context).pushNamed(LoginScreen.routeName);
      });
    } else {
      setState(() {
        print("not first launch");
        Navigator.of(context).pushNamed(LandingSection.routeName);
      });
    }

  }

And calling the setValue() in initState()

我期待解决方案。