1

I am a beginner with flutter, I had just started a few days ago. I want to go from one widget to another widget in another file. But when I use the navigator it shows an error.

I've tried to solve it using some answers to similar problems on stack overflow, but I can't solve it. Also, I am not able to understand those properly.

These are some of them:

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"

this is a widget in 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);
                              },
                            ),
                          ],
                        )

          ]


        ),
  );
}

On pressing go I want it to go to the take_attendance.dart page.

But when using navigator this is the error that I got:

Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.dart(undefined_identifier)

Abdulrahman Falyoun
  • 3,676
  • 3
  • 16
  • 43

1 Answers1

2

Your 'context' doesn't have a Navigator instance on any of its parents, because the Navgiator is initialized in the Scaffold itself. Just wrap the body: in a new widget which will now have a parent Navigator.

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);
                },
              ),
            ],
          )
        ]);
  }
}
xIsra
  • 867
  • 10
  • 21
  • I'd suggest you search for more on Navigator, there are plenty of good use cases/best practices for using the navigation system provided with flutter. I personally liked to build most of the navigation myself, but if you modify the regular Navigator to your need it is more powerful. – xIsra Jun 29 '19 at 08:27
  • @xIsra I am also facing similar issue. No solution found yet. https://stackoverflow.com/questions/61284614/flutter-navigate-to-other-page-not-working – Roxx Apr 19 '20 at 04:29