我是一个初学者,已经创建了我的应用程序,但是我想检查用户是否在安装后第一次打开该应用程序,我看过this article,但不知道怎么办?>
这是启动屏幕代码,该代码在3秒钟后将用户直接移动到主屏幕,但是我想检查用户是否是第一次打开应用并将用户移动到“欢迎”屏幕,或者是否不是第一次将用户移动到主屏幕。屏幕。
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,
)),
],
),
);
}
}
答案 0 :(得分:1)
您可以在用户首次输入时使用https://pub.dev/packages/shared_preferences添加一个值
答案 1 :(得分:1)
@Abdullrahman,请按照他人的建议使用shared_preferences
。这是您的方法,
pubspec.yaml
中的shared_preferences程序包并运行Packages get
:dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.5.4+6
import 'package:shared_preferences/shared_preferences.dart';
class _SplashScreenState extends State<SplashScreen> {
startTime() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool firstTime = prefs.getBool('first_time');
var _duration = new Duration(seconds: 3);
if (firstTime != null && !firstTime) {// Not first time
return new Timer(_duration, navigationPageHome);
} else {// First time
prefs.setBool('first_time', false);
return new Timer(_duration, navigationPageWel);
}
}
void navigationPageHome() {
Navigator.of(context).pushReplacementNamed('/HomePage');
}
void navigationPageWel() {
Navigator.of(context).pushReplacementNamed('/WelcomePage');
}
........
注意:如果用户清除了缓存,则SharedPreferences数据将被删除。 SharePreferences是本地选项。如果您想避免这种情况,可以使用firestore来保存bool值,但是对于这样的简单任务,firestore可能会显得过大。
希望这会有所帮助。
答案 2 :(得分:0)
这里的代码没有任何错误并且说明得很好
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");
});
}
);
}
}