繁体   English   中英

Flutter 应用程序无法跳过老用户的介绍屏幕

[英]Flutter app not able to skip introduction screen for old users

我是扑的新手。 我正在制作一个应用程序,该应用程序在第一次使用该应用程序时将电话号码作为第一个屏幕中的输入。 从下一次开始,我希望应用程序跳过第一个屏幕并直接转到下一个屏幕。 但我无法实现此功能。请帮忙。 谢谢你。

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;
                  },
                ),

            ],
          ),
        ),
      ),
    );
  }
}

我认为你的问题来自这里: prefShare.spAppGetter();

由于Future<bool> spAppGetter() async {...}是一个返回未来的异步函数,因此您应该使用await调用它,以便在调用bool status = prefShare.isNew ?? false;之前确定 spAppGetter 已完成bool status = prefShare.isNew ?? false; bool status = prefShare.isNew ?? false; 为此,您还应该将主函数标记为异步。 您的主要功能将如下所示:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await prefShare.spAppGetter();
  bool status = prefShare.isNew ?? false;
  prefShare.spAppNew();
  runApp(
    MaterialApp(home: status == true ? ButtonScreen() : Home()),
  );
}

您没有使用 getBool("isLoggedIn") 的状态

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  prefShare.spAppGetter();// it will actually tell you that you if user logged in or not
  bool status = prefShare.isNew ?? false;
  prefShare.spAppNew(); // this should be where the user once logged in.
  runApp(
    MaterialApp(home: status == true ? ButtonScreen() : Home()),
  );
}

所以它应该是这样的

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
    bool status =await SharedPreferences.getInstance().getBool('isLoggedIn');
    runApp(
    MaterialApp(home: status == true ? ButtonScreen() : Home()),
  );
}

并在此处保存登录状态

  FlatButton(
                          child: Text("Continue"),
                          onPressed: () {
                            prefShare.spNumberSetter(phoneNo);
SharedPreferences.getInstance().setBool('isLoggedIn');
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => ButtonScreen()));
                          },
                        ),

并且您还应该致力于命名约定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM