我是flutter的初学者,我创建了我的应用程序,但是我想检查用户是否在安装后第一次打开该应用程序,我看过这篇文章但不知道该怎么做?
这是启动画面代码,该代码在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,
)),
],
),
);
}
}
这是没有任何错误并清楚说明的代码
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");
});
}
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句