颤振导航未定义的“上下文”

Posted

技术标签:

【中文标题】颤振导航未定义的“上下文”【英文标题】:Flutter Navigation undefined "context" 【发布时间】:2020-02-25 12:58:11 【问题描述】:

我是 flutter 和 dart 的新手,我正在尝试为应用程序构建一个注册。

我有一个开始屏幕 (StartPage),其中包含“登录”、“登录 FB”、“使用电子邮件注册”选项。 当我点击“登录”时,我可以导航到 signup_email_screen。从那里我想导航到 signup_email_screen_two 但它抛出“未定义的名称上下文”。

class StartPage extends StatefulWidget 
  @override
  _StartPageState createState() => _StartPageState();


class _StartPageState extends State<StartPage> 
  bool isAuth = false; //isAuth provides a bool if the user is already signed in

  Widget buildAuthScreen() 
    return Text('Angemeldet');
  

  Scaffold buildUnAuthScreen() 
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/images/tenor.gif'),
                fit: BoxFit.cover)),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: SizeConfig.blockSizeVertical * 10),
            Container(
              width: SizeConfig.blockSizeHorizontal * 65,
              height: SizeConfig.blockSizeVertical * 20,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/Insta_transparent.png'),
                  fit: BoxFit.scaleDown,
                ),
              ),
            ),
            SizedBox(height: SizeConfig.blockSizeVertical * 0),
            Text(
              'Nie mehr Langeweile',
              style: TextStyle(
                fontFamily: "Signatra",
                fontSize: SizeConfig.blockSizeHorizontal *8,
                color: Colors.white,
              ),
            ),
            SizedBox(height: SizeConfig.blockSizeVertical * 7),
            GestureDetector(
                onTap: () => _navigateToLogin(context),
                child: Container(
                  width: SizeConfig.blockSizeHorizontal * 70,
                  height: SizeConfig.blockSizeVertical * 7,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/Log_In_Button.png'),
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                )),
            SizedBox(height: SizeConfig.blockSizeVertical * 2),
            GestureDetector(
              onTap: () => print('facebook signin'),
              child: Container(
                width: SizeConfig.blockSizeHorizontal * 70,
                height: SizeConfig.blockSizeVertical * 7,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/Log_In_Button_FB.png'),
                    fit: BoxFit.scaleDown,
                  ),
                ),
              ),
            ),
            SizedBox(height: SizeConfig.blockSizeVertical * 2),
            GestureDetector(
              onTap: () => _navigateToEmailRegister(context),
              child: Container(
                width: SizeConfig.blockSizeHorizontal * 60,
                height: SizeConfig.blockSizeVertical *7,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                        'assets/images/button-email-registration.png'),
                    fit: BoxFit.scaleDown,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  

  void _navigateToLogin(BuildContext context) 
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => LoginScreen(),
        ));
  

  void _navigateToEmailRegister(BuildContext context) 
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => SignupEmailScreen(),
        ));
  

  @override
  Widget build(BuildContext context) 
    SizeConfig().init(context);
    return isAuth ? buildAuthScreen() : buildUnAuthScreen();
  

class SignupEmailScreen extends StatelessWidget 
  Scaffold signupEmailScreen() 
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/images/StartPage_Background.jpg'),
                    fit: BoxFit.cover)),
            alignment: Alignment.center,
          ),
          //AppBar transparent and background full screen with Stack and Positioned
          Positioned(
            top: 0.0,
            left: 0.0,
            right: 0.0,
            child: AppBar(
              title: Text('Registrierung'),
              backgroundColor: Colors.transparent, //transparent
              elevation: 0.0, //Shadow gone
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: SizeConfig.blockSizeVertical * 15),
              //Insta image
              Container(
                width: SizeConfig.blockSizeHorizontal * 50,
                height: SizeConfig.blockSizeVertical * 10,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/Insta_transparent.png'),
                    fit: BoxFit.scaleDown,
                  ),
                ),
              ),
              SizedBox(height: SizeConfig.blockSizeVertical * 7),
              //Email text field
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.text,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Accountname",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              //Password text field
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.emailAddress,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Email",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.visiblePassword,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Passwort",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.visiblePassword,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Passwort wiederholen",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              SizedBox(height: SizeConfig.blockSizeVertical * 10),
              //log in button
              GestureDetector(
                onTap: () => _navigateToSignupEmailScreenTwo(context),
                child: Container(
                  width: SizeConfig.blockSizeHorizontal * 70,
                  height: SizeConfig.blockSizeVertical * 7,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/Log_In_Button.png'),
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                )),
            ],
          ),
        ],
      ),
    );
  

//ACTIONS
  void _navigateToSignupEmailScreenTwo(BuildContext context) 
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => SignupEmailScreenTwo(),
        ));
  

//BUILD 
  @override
  Widget build(BuildContext context) 
    return signupEmailScreen();
  

class SignupEmailScreenTwo extends StatelessWidget 
  Scaffold signupEmailScreenTwo() 
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/images/StartPage_Background.jpg'),
                    fit: BoxFit.cover)),
            alignment: Alignment.center,
          ),
          //AppBar transparent and background full screen with Stack and Positioned
          Positioned(
            top: 0.0,
            left: 0.0,
            right: 0.0,
            child: AppBar(
              title: Text('Registrierung'),
              backgroundColor: Colors.transparent, //transparent
              elevation: 0.0, //Shadow gone
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: SizeConfig.blockSizeVertical * 15),
              Container(
                width: SizeConfig.blockSizeHorizontal * 50,
                height: SizeConfig.blockSizeVertical * 10,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/Insta_transparent.png'),
                    fit: BoxFit.scaleDown,
                  ),
                ),
              ),
              SizedBox(height: SizeConfig.blockSizeVertical * 7),
              //Email text field
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.text,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Dein Vorname",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              //Password text field
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: SizeConfig.safeBlockHorizontal * 5),
                child: TextField(
                  keyboardType: TextInputType.datetime,
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                  decoration: InputDecoration(
                    labelText: "Dein Geburtsdatum",
                    labelStyle: TextStyle(
                      color: Colors.white,
                    ),
                    hasFloatingPlaceholder: true,
                    enabledBorder: UnderlineInputBorder(
                        //Enables the border to change color
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide:
                            BorderSide(color: Colors.white, width: 2.0)),
                  ),
                ),
              ),
              SizedBox(height: SizeConfig.blockSizeVertical * 10),
              //log in button
              GestureDetector(
                onTap: () => print("Einlogggggeeeeeen"),
                child: Container(
                  width: SizeConfig.blockSizeHorizontal * 70,
                  height: SizeConfig.blockSizeVertical * 7,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/Log_In_Button.png'),
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                )),
            ],
          ),
        ],
      ),
    );
  

  @override
  Widget build(BuildContext context) 
    return signupEmailScreenTwo();
  

为什么上下文未定义? 感谢任何帮助!

【问题讨论】:

【参考方案1】:

因为你的函数内部没有定义上下文:)

**编辑:**你无法访问StatelessWidget中的上下文对象

这些是你可以做的:

1- 使用StatefulWidget 而不是StatelessWidget

2- 您可以将BuildContext context 作为parameter 传递到您的函数中。

3- 您可以在build 方法中编写所有小部件,而不是额外的函数。

【讨论】:

以上是关于颤振导航未定义的“上下文”的主要内容,如果未能解决你的问题,请参考以下文章

颤振未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称

未定义的名称“上下文”(在 Flutter 导航中)

导航未定义名称上下文

如何在颤振应用程序中无上下文导航?

未在颤振自定义应用程序栏中获取脚手架上下文

关闭导航器时出现颤振错误