Search code examples
androidflutterdartflutter-navigation

How navigate to another screen


I have a next RaisedButton to go a next screen called DetailOracion.

Based on the example of Flutter to push new screen but doest work.

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Oraciones Cristianas'),
        ),
        body: SafeArea(
            child: Padding(
          padding: EdgeInsets.all(10.0),
          child: Column(
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => DetailOracion()));
                },
                child: Text('Hello Wolrd'),
              )
            ],
          ),
        )),
      ),
    );
  }

My DetailOracion

class DetailOracion extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Hola'),
    ),
    body: Text('Segunda Pantalla'),
  );
 }
}

And the error message its the next

I/flutter ( 3441): The following assertion was thrown while handling a gesture:
I/flutter ( 3441): Navigator operation requested with a context that does not include a Navigator.
I/flutter ( 3441): The context used to push or pop routes from the Navigator must be that of a widget that is a
I/flutter ( 3441): descendant of a Navigator widget.

Solution

  • When used the MaterialPageRoute you need to send your main class inside of MaterialApp from runApp()

    Explain with Code

    Correct

    void main() => runApp(MaterialApp(
      title: 'Oraciones Cristianas',
      home: MyApp(),
    ));
    

    You send yout first Screen insde of MaterialApp(), able to use MaterialPageRoute()

    Incorrect

    void main() => runApp(myApp());
    

    If you simply send your first screen without MaterialApp() wrapper doesnt works