启动屏幕
我想将基本用户数据(姓名和电子邮件(存储在我的flutter应用程序中(使用共享首选项(,我想使用这些数据来确定我应该显示启动屏幕还是主屏幕。我正在尝试使用三元运算符来查看共享首选项是否包含名称(如果是,则转到主屏幕,否则显示启动屏幕(。我正在分享我的主文件和表单代码,所以请帮我做这件事,或者建议其他方式,因为我对此非常陌生。
final val = _readName();
void main() {
WidgetsFlutterBinding.ensureInitialized();
_saveEmpty();
print(val);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My Title',
theme: ThemeData(
textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
primaryColor: kPrimaryColor,
accentColor: kPrimaryColor,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: val != "a" ? HomeScreen() : SplashScreen(),
);
}
}
_readName() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final value = prefs.getString("myName");
print(value);
return value;
}
_saveEmpty() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("myName", "a");
}
形式:
TextFormField buildEmailFormField() {
return TextFormField(
keyboardType: TextInputType.emailAddress,
onSaved: (newValue) {
email = newValue;
_saveEmail(newValue);
},
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: kEmailNullError);
} else if (emailValidatorRegExp.hasMatch(value)) {
removeError(error: kInvalidEmailError);
}
return null;
},
validator: (value) {
if (value.isEmpty) {
addError(error: kEmailNullError);
return "";
} else if (!emailValidatorRegExp.hasMatch(value)) {
addError(error: kInvalidEmailError);
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Email",
hintText: "Enter your email",
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/Mail.svg"),
),
);
}
TextFormField buildNameFormField() {
return TextFormField(
keyboardType: TextInputType.name,
onSaved: (newValue) {
name = newValue;
_saveName(newValue);
},
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: kNameNullError);
}
return null;
},
validator: (value) {
if (value.isEmpty) {
addError(error: kNameNullError);
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Name",
hintText: "Enter your name",
floatingLabelBehavior: FloatingLabelBehavior.always,
),
);
}
_saveName(val) async {
final prefs = await SharedPreferences.getInstance();
final key = 'my_name';
prefs.setString(key, val);
}
_saveEmail(val) async {
final prefs = await SharedPreferences.getInstance();
final key = 'my_email';
prefs.setString(key, val);
}
我想使用共享偏好,因为数据库对我来说听起来很复杂,所以请帮助我。谢谢
我建议的实现方法是在显示启动屏幕时检查共享偏好状态,然后根据结果将用户导航到主屏幕或登录屏幕。
以下是操作方法,
- 创建用于输入用户数据和主页的单独屏幕
- 在主应用程序中添加以下splashscreen类作为主页
- 当用户保存输入的数据时,将islogged_in值保存为true
- 在显示启动屏幕时检查is_loged_in值,并进行相应的导航
启动屏幕
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
startTimer();
}
@override
Widget build(BuildContext context) {
return Material(
child: Center(child: AppIconWidget(image: Assets.appLogo)),
);
}
startTimer() {
var _duration = Duration(milliseconds: 2000);
return Timer(_duration, navigate);
}
navigate() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if (preferences.getBool(Preferences.is_logged_in) ?? false) {
Navigator.of(context).pushNamed(Routes.home);
} else {
Navigator.of(context).pushNamed(Routes.login);
}
}
}
在你的主类中称之为家
home: SplashScreen()
更新:
我使用的preferences常量类应该是这样的,此值用作存储我们保存的首选值的密钥
class Preferences {
Preferences._();
static const String is_logged_in = "isLoggedIn";
}