首页 > 解决方案 > 导航器中未定义的名称“上下文”

问题描述

我是flutter的初学者,几天前才开始。我想从一个小部件转到另一个文件中的另一个小部件。但是当我使用导航器时,它会显示错误。

我尝试使用堆栈溢出类似问题的一些答案来解决它,但我无法解决它。另外,我无法正确理解这些。

这些是其中一些:

未定义的名称“上下文”

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

“take_attendance.dart”

class TakeAttendance extends StatefulWidget {
  static String tag = 'take-attendance';
  @override
  _TakeAttendanceState createState() => _TakeAttendanceState();
}

bool colorCheck = false;

class _TakeAttendanceState extends State<TakeAttendance> {
  @override
  Widget build(BuildContext context) {
  }
}

“home_page.dart”

这是 home_page.dart 中的一个小部件。

Widget showPeriod(int a) {
  return Scaffold(
    appBar: new AppBar(
          title: new Text(
            "AppName",
            style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
          ),
          backgroundColor: Colors.blue[800],
          actions: <Widget>[
            new Padding(
                padding: EdgeInsets.only(right: 10.0),
                child: Icon(Icons.exit_to_app)),
          ],
        ),
        drawer: new AppDrawer(),
    body: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,

        children: <Widget>[
            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
              new Text("Period #$a",
            style: TextStyle(fontSize: 50.0,fontWeight: FontWeight.w400),
            )
            ],),

            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
              new Container(
                            child: Text(
                              "<time>",
                              style: TextStyle(color: Colors.white),
                            ),
                            decoration: new BoxDecoration(
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(30.0)),
                              color: Colors.grey,
                            ),
                            padding:
                                new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                          ),
                          SizedBox(width: 10.0),
                          new Container(
                            child: Text(
                              "<Class>",
                              style: TextStyle(color: Colors.white),
                            ),
                            decoration: new BoxDecoration(
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(30.0)),
                              color: Colors.grey,
                            ),
                            padding:
                                new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                          ),


            ],),
             new Padding(
                        padding: EdgeInsets.only(left: 10.0, top: 10.0),
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            new Container(
                              child: Text(
                                "An imaginary subject.",
                                style: TextStyle(color: Colors.white),
                              ),
                              decoration: new BoxDecoration(
                                borderRadius: new BorderRadius.all(
                                    new Radius.circular(30.0)),
                                color: Colors.grey,
                              ),
                              padding:
                                  new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                            ),
                          ],
                        )),

                        SizedBox(height: 40.0,),

                        new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            new RaisedButton(
                              child: const Text('GO!'),
                              color: Colors.redAccent,
                              elevation: 4.0,
                              splashColor: Colors.red,
                              onPressed: () {
                                // Perform some action
                                  Navigator.of(context).pushNamed(TakeAttendance.tag);
                                  //Navigator.push(context,takeAttendance());
                              },
                            ),

                            new RaisedButton(
                              child: const Text('Cancel'),
                              color: Colors.blueAccent,
                              elevation: 4.0,
                              splashColor: Colors.blue[200],
                              onPressed: () {
                                // Perform some action
                                //Navigator.pop(context);
                              },
                            ),
                          ],
                        )

          ]


        ),
  );
}

按 go 我希望它转到take_attendance.dart页面。

但是当使用导航器时,这是我得到的错误:

未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义 name.dart(undefined_identifier)

标签: flutter

解决方案


您的“上下文”在其任何父项上都没有 Navigator 实例,因为 Navgiator 是在 Scaffold 本身中初始化的。只需将 body: 包裹在一个新的小部件中,该小部件现在将有一个导航器。

Widget showPeriod(int a) {
  return Scaffold(
    appBar: new AppBar(
      title: new Text(
        "Hajar",
        style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
      ),
      backgroundColor: Colors.blue[800],
      actions: <Widget>[
        new Padding(
            padding: EdgeInsets.only(right: 10.0),
            child: Icon(Icons.exit_to_app)),
      ],
    ),
    drawer: new AppDrawer(),
    body: MyPageBody(),
  );
}


class MyPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              new Text(
                "Period #$a",
                style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.w400),
              )
            ],
          ),
          new Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              new Container(
                child: Text(
                  "<time>",
                  style: TextStyle(color: Colors.white),
                ),
                decoration: new BoxDecoration(
                  borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
                  color: Colors.grey,
                ),
                padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
              ),
              SizedBox(width: 10.0),
              new Container(
                child: Text(
                  "<Class>",
                  style: TextStyle(color: Colors.white),
                ),
                decoration: new BoxDecoration(
                  borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
                  color: Colors.grey,
                ),
                padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
              ),
            ],
          ),
          new Padding(
              padding: EdgeInsets.only(left: 10.0, top: 10.0),
              child: new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  new Container(
                    child: Text(
                      "An imaginary subject.",
                      style: TextStyle(color: Colors.white),
                    ),
                    decoration: new BoxDecoration(
                      borderRadius:
                          new BorderRadius.all(new Radius.circular(30.0)),
                      color: Colors.grey,
                    ),
                    padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
                  ),
                ],
              )),
          SizedBox(
            height: 40.0,
          ),
          new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              new RaisedButton(
                child: const Text('GO!'),
                color: Colors.redAccent,
                elevation: 4.0,
                splashColor: Colors.red,
                onPressed: () {
                  // Perform some action
                  Navigator.of(context).pushNamed(TakeAttendance.tag);
                  //Navigator.push(context,takeAttendance());
                },
              ),
              new RaisedButton(
                child: const Text('Cancel'),
                color: Colors.blueAccent,
                elevation: 4.0,
                splashColor: Colors.blue[200],
                onPressed: () {
                  // Perform some action
                  //Navigator.pop(context);
                },
              ),
            ],
          )
        ]);
  }
}

推荐阅读