有没有一种方法可以自动弹出屏幕?

时间:2020-04-11 23:51:06

标签: flutter

在我的应用"see the structure image"中,我使用包装器来浏览“启动”页面或“首页/ NewsFeed”页面。在启动页面上,我有2个选项1.用户登录2.志愿者登录。当我使用这些页面中的任何一个成功登录时,页面却在后台更改。我的登录页面没有弹出。我使用Navigator.of(context).pop(),但无济于事

主页代码

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  // This widget is the root of This application.

  @override
  Widget build(BuildContext context) {
    return StreamProvider<User>.value(
      value: Authentication().user,
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData().copyWith(
          scaffoldBackgroundColor: Color(0XFF00B8A9),
          canvasColor: Color(0XFF00B8A9),
          cursorColor: Color(0XFF00B8A9),
        ),
        initialRoute: Wrapper.id,

        routes: {
          Wrapper.id: (context) => Wrapper(),
          StartUpScreen.id: (context) => StartUpScreen(),
          SignIn.id: (context) => SignIn(),
          VolunteerLogin.id: (context) => VolunteerLogin(),
          Registration.id: (context) => Registration(),
          VolunteerHome.id: (context) => VolunteerHome(),
        },
      ),
    );
  }
}

包装页代码

  class Wrapper extends StatelessWidget {

  static String id = "Wrapper";


  @override
  Widget build(BuildContext context) {

    // Check if the user login or not and based on condition send him to different screen
    final user = Provider.of<User>(context);

    if(user == null){
      return StartUpScreen();
    }else{
      return NewsFeed();
    }
  }
}

启动页面代码

class StartUpScreen extends StatefulWidget {

  static String id = "Start_Up_Screen";

  @override
  _StartUpScreenState createState() => _StartUpScreenState();
}

class _StartUpScreenState extends State<StartUpScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, SignIn.id);
                  },
                  child: ReusableButton(
                    containerColor: kPrimaryButtonColor,
                    buttonChild: Text(
                      "User Login",
                      style: TextStyle(
                        fontFamily: 'Roboto',
                        fontSize: 15.0,
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: 40.0,
                  width: 30.0,
                ),
                GestureDetector(
                  onTap: () =>
                      Navigator.pushNamed(context, VolunteerHome.id),
                  child: ReusableButton(
                    containerColor: kSOSButtonColor,
                    buttonChild: Text(
                      "Volunteer Login",
                      style: TextStyle(
                        fontFamily: 'Roboto',
                        fontSize: 15.0,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

登录页面代码

class SignIn extends StatefulWidget {

  static String id = "Sign_In";

  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {

  Authentication _authentication = Authentication();
  final _formKey = GlobalKey<FormState>();

  // text field state
  String email = '';
  String password = '';


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 30.0),
        child: Form(
          key: _formKey,
          autovalidate: true,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              TextFormField(
                decoration: kInputFormFieldDecoration.copyWith(hintText: 'email'),
                validator: (val) => val.isEmpty ? 'Enter an email' : null,
                onChanged: (val) {
                  setState(() => email = val);
                },
              ),
              SizedBox(height: 20.0),
              TextFormField(
                decoration: kInputFormFieldDecoration.copyWith(hintText: 'password'),
                obscureText: true,
                validator: (val) => val.length < 6 ? 'Enter a password 6+ chars long' : null,
                onChanged: (val) {
                  setState(() => password = val);
                },
              ),
              SizedBox(height: 20.0),

              RaisedButton(
                color: Colors.pink[400],
                child: Text(
                  'Sign In',
                  style: TextStyle(color: kPrimaryButtonColor),
                ),
                onPressed: () async {
                  await _authentication.signInWithEmailAndPassword(email, password);
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您在哪里使用Navigator.of(context).pop(),我看到您在使用Navigator.pushNamed(context, routeName);,这就是为什么您的屏幕没有弹出的原因,请尝试也进行更改。

  Navigator.of(context).popAndPushNamed(routeName)

如果您已经尝试过此操作,请告诉我,我会尽力帮助您。