【发布时间】:2020-11-05 09:24:08
【问题描述】:
我是新来的颤振。我正在制作一个应用程序,它在第一次使用该应用程序时将电话号码作为第一个屏幕中的输入。从下一次开始,我希望应用程序跳过第一个屏幕并直接转到下一个屏幕。但我无法实现此功能。请帮助。谢谢。
import 'package:shared_preferences/shared_preferences.dart';
class PrefShare {
String mob;
bool isNew;
Future<String> spNumberSetter(String no) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('phoneNumber', no);
}
Future<String> spNumberGetter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
mob = prefs.getString('phoneNumber');
}
Future<bool> spAppNew() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isLoggedIn', true);
}
Future<bool> spAppGetter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
isNew = prefs.getBool('isLoggedIn');
}
}
//main
import 'package:emergency_messaging/button_screen.dart';
import 'package:flutter/material.dart';
import 'prefs.dart';
PrefShare prefShare = PrefShare();
void main() {
WidgetsFlutterBinding.ensureInitialized();
prefShare.spAppGetter();
bool status = prefShare.isNew ?? false;
prefShare.spAppNew();
runApp(
MaterialApp(home: status == true ? ButtonScreen() : Home()),
);
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
String phoneNo;
return SafeArea(
child: Scaffold(
backgroundColor: Color(0xff9ad3bc),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.black,
child: Icon(
Icons.arrow_forward,
color: Color(0xff9ad3bc),
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Confirmation"),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text("The phone number entered is: $phoneNo"),
],
),
),
actions: <Widget>[
FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.pop(context);
},
),
FlatButton(
child: Text("Continue"),
onPressed: () {
prefShare.spNumberSetter(phoneNo);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ButtonScreen()));
},
),
],
);
},
);
},
),
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: [
SizedBox(
height: 150.0,
),
TextField(
style: TextStyle(color: Colors.black,),
decoration: InputDecoration(
hintText: "Your emergency contact number",
hintStyle: TextStyle(color: Colors.black,),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black,),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black,),
),
),
keyboardType: TextInputType.phone,
autofocus: true,
cursorColor: Colors.black,
onChanged: (text) {
phoneNo = text;
},
),
],
),
),
),
);
}
}
【问题讨论】: