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

时间:2019-06-29 07:56:59

标签: flutter

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

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

其中一些是

Undefined name 'context'

Undefined name 'context' in flutter navigation

“ 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页。

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

  

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

1 个答案:

答案 0 :(得分:1)

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

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