亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過php和mysql檢查用戶是否在flutter中登錄?

如何通過php和mysql檢查用戶是否在flutter中登錄?

PHP
嗶嗶one 2023-07-01 17:06:05
我是顫振和編程的新手。我開發了一個 flutter 模板 (github.com/mitesh77/Best-Flutter-UI-Templates) 并為其添加了一個啟動屏幕。現在我想檢查用戶是否未登錄,啟動畫面將不會加載并且用戶會看到登錄頁面。我在新項目中嘗試了這個(flutter-examples.com/flutter-online-user-registration-using-php-mysql-server)并且對我來說效果很好。但如何將其添加到下面的代碼中。代碼:void main() async {      WidgetsFlutterBinding.ensureInitialized();      await SystemChrome.setPreferredOrientations(<DeviceOrientation>[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])          .then((_) => runApp(MyApp()));}/* This is First Screen */class FirstRoute extends StatelessWidget {  @override  Widget build(BuildContext context) {    return new SplashScreen(        seconds: 5,        navigateAfterSeconds: new AfterSplash(),        title: new Text('Hello',          style: new TextStyle(              fontWeight: FontWeight.w700,              fontFamily: 'IranYekan',              fontSize: 30.0          ),),        image: new Image.asset('assets/images/splashImage.png'),        backgroundColor: Colors.white,        styleTextUnderTheLoader: new TextStyle(),        photoSize: 110.0,        onClick: ()=>print("Flutter Egypt"),        loaderColor: Colors.blue    );  }}class AfterSplash extends StatelessWidget {  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('First Route'),      ),      body: Center(        child: RaisedButton(          child: Text('Open route'),          onPressed: () {            // Navigate to second route when tapped.            Navigator.push(context, MaterialPageRoute(builder: (context) => NavigationHomeScreen()),            );          },        ),      ),    );  }}
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

我將向您展示我在項目中做了什么,首先您需要安裝Sharedprefrence,然后在 lib 文件夾中創建文件,創建名為 Utils 的文件夾,您可以提供任何您想要的名稱,并在 Utils 文件夾中創建一個文件 sharedpreference.dart?lib\Utils\

在此文件中的sharedpreference.dart中添加此行,ps:您可以使用此文件添加更多數據,例如,如果api返回userid類型的內容,您可以在此處指定,并且可以使用sharedprefrence在所有屏幕上訪問數據

class SharedPrefrence {

? ? ?Future<bool> setLoggedIn(bool status) async {

? ? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();

? ? ? ? ? ? return prefs.setBool("logged_in", status);

? ? ? ? ? }

? ? ? ??

? ? ? ? ? Future<bool> getLogedIn() async {

? ? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();

? ? ? ? ? ? return prefs.getBool("logged_in") ?? false;

? ? ? ? ? }

? ? ? ? Future<bool> setUserId(String userId) async {

? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();

? ? ? ? ? return prefs.setString("user_id", userId);

? ? ? ?}

? ??

? ? ? Future<String> getUserId() async {

? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();

? ? ? ? return prefs.getString("user_id") ?? '';

? ? ? }

}

登錄頁面


這是示例登錄功能,我在其中使用了首選項


void AppLogin(String username, String password) async {


? ? var response = await http.post(Urls.LOGIN,

? ? ? ? headers: {"Content-Type": "application/json"},

? ? ? ? body: json.encode({

? ? ? ? ? "User_Name": username,

? ? ? ? ? "Password": password,

? ? ? ? }));


? ? Map<String, dynamic> value = json.decode(response.body);

? ? if (response.statusCode == 200) {

? ? ? dialog.dismissProgressDialog(context);

? ? ? try {

? ? ? ? Map<String, dynamic> value = json.decode(response.body);

? ? ? ? SharedPrefrence().setLoggedIn(true);

? ? ? ? SharedPrefrence().setUserId(value['_id'].toString());


? ? ? ? Navigator.pushAndRemoveUntil(

? ? ? ? ? ? context,

? ? ? ? ? ? MaterialPageRoute(builder: (context) => DashboardScreen()),

? ? ? ? ? ? ModalRoute.withName("/login"));

? ? ? } catch (e) {

? ? ? ? e.toString();

? ? ? }

? ? }? else {

? ? ? dialog.dismissProgressDialog(context);

? ? ? var message = value['message'];

? ? ? CustomDialogs().showErrorAlert(context, message);

? ? }

? }

在您的初始屏幕中添加此函數,并在 initState 函數中調用函數 startTime,此時您的初始屏幕將顯示 3 秒,然后它將調用 navigationPage ,在其中檢查用戶是否登錄的登錄狀態的共享首選項如果沒有,它將顯示登錄信息,如果已登錄,它將重定向到 dahsboard 屏幕


?startTime() async {

? ? var _duration = new Duration(seconds: 3);

? ? return new Timer(_duration, navigationPage);

? }


? void navigationPage() {



? ? Future loginstatus = SharedPrefrence().getLogedIn();

? ? loginstatus.then((data) {

? ? ? if (data == true) {

? ? ? ? Navigator.pop(context, true);

? ? ? ? Navigator.pushAndRemoveUntil(

? ? ? ? ? ? context,

? ? ? ? ? ? MaterialPageRoute(builder: (context) => DashboardScreen()),

? ? ? ? ? ? ModalRoute.withName("/login"));

? ? ? } else {

? ? ? ? Navigator.pop(context, true);

? ? ? ? Navigator.push(

? ? ? ? ? context,

? ? ? ? ? MaterialPageRoute(

? ? ? ? ? ? builder: (context) => LoginScreen(),

? ? ? ? ? ),

? ? ? ? );

? ? ? }

? ? });

? }


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號