错误:请求的导航器操作的上下文不包含导航器

时间:2020-11-11 17:23:43

标签: android android-studio flutter

我试图连接2张幻灯片(登录和注册),但出现以下错误。当我单击“没有帐户吗?”时出现错误。文本,该文本应将我发送到注册幻灯片。 这是错误

═══════手势捕获异常═══════以下 处理手势时引发断言:导航器操作 请求的上下文不包含导航器。

用于从导航器推送或弹出路由的上下文必须是 小部件的一部分,它是导航器小部件的后代。

这是我的登录文件

import 'package:flutter/material.dart';
import 'package:scroll_app_2/register.dart';

void main() => runApp(new MyApp());
class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    Color hexToColor(String code) {
      return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
    }
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "Welcome to Flutter",
        home: new Material(
            child: new Container (
                padding: const EdgeInsets.all(30.0),
                color: Colors.white,
                child: new Container(
                  child: new Center(
                      child: new Column(
                          children : [
                            new Padding(padding: EdgeInsets.only(top: 140.0)),
                            new Text('Welcome!', //heading
                              style: new TextStyle(color: hexToColor("#0000"), fontSize: 30.0),),
                            new Padding(padding: EdgeInsets.only(top: 50.0)),
                            // email text field is below
                            new TextFormField(
                              decoration: new InputDecoration(
                                labelText: "Enter Email",
                                fillColor: Colors.blue,
                                border: new OutlineInputBorder(
                                  borderRadius: new BorderRadius.circular(17.0),
                                  borderSide: new BorderSide(
                                  ),
                                ),

                              ),
                              validator: (val) {
                                if(val.length==0) {
                                  return "Email cannot be empty";
                                }else{
                                  return null;
                                }
                              },
                              keyboardType: TextInputType.emailAddress,
                              style: new TextStyle(
                                fontFamily: "Poppins",
                              ),
                            ),

                            new Padding(padding: EdgeInsets.only(top: 20.0)),


                            //password text field is below
                            new TextFormField(
                              obscureText: true,
                              decoration: new InputDecoration(
                                labelText: "Enter Password",
                                fillColor: Colors.blue,
                                border: new OutlineInputBorder(
                                  borderRadius: new BorderRadius.circular(17.0),
                                  borderSide: new BorderSide(
                                  ),
                                ),
                                //fillColor: Colors.green
                              ),
                              validator: (val) {
                                if(val.length==0) {
                                  return "Password cannot be empty";
                                }else{
                                  return null;
                                }
                              },
                              keyboardType: TextInputType.emailAddress,
                              style: new TextStyle(
                                fontFamily: "Poppins",
                              ),
                            ),
                            new Padding(padding: EdgeInsets.only(top: 10.0)),

                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                new Text("Forgot Password?", style: TextStyle(
                                  fontSize: 15.0,
                                  color: Colors.grey[800],
                                  fontFamily: "poppins",
                                  letterSpacing: 1.2,
                                ),)
                              ],
                            ),
                            new Padding(padding: EdgeInsets.only(top: 15.0)),

                            //Login button below
                            RaisedButton (
                              onPressed: () {},
                              color: Colors.amber,
                              padding: EdgeInsets.fromLTRB(75.0, 10.0, 75.0, 10.0),
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(17)),
                              child: Text("Login", style: TextStyle(
                                fontSize: 20.0,
                                letterSpacing: 1.2,
                              ),),
                            ),
                            new Padding(padding: EdgeInsets.only(top: 150.0)),

                            InkWell(
                              onTap: () {
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(builder: (context) => RegisterPage())  
                                );
                              },
                              child: new Text(
                                "Don't Have an Account?",
                                style: TextStyle(
                                  fontSize: 15.0,
                                  letterSpacing: 1.2,
                                ),
                              ),
                            )


                          ]
                      )
                  ),
                )
            )
        )
    );
  }
}

这是我的注册文件,单击文本后我要转到该文件

import 'package:flutter/material.dart';

class RegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: new Material(
          child: new Container (
            padding: const EdgeInsets.all(30.0),
            color: Colors.yellowAccent,
          ),
        ),


    );
  }
}

1 个答案:

答案 0 :(得分:0)

Container小部件的当前子项MaterialApp提取为新小部件:

return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "Welcome to Flutter",
        home: LoginPage(),
     );

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Material( 
      child: Container (
            padding: const EdgeInsets.all(30.0),
            color: Colors.white,
            child: new Container(
              child: new Center(
               ....
         );
      }
    }