Back to view in flutter

Issue

i have a little problem with the Navigator in flutter. I have 3 windows: (Login -> Home -> Orders). But when I go from Login to Home, everything works fine, but if I go from Home to Orders and use the android back button, it returns me to the Login window, that is, until the first view, not the second.

My code Navigation of Login:

Navigator.of(context).push(
  MaterialPageRoute(
    builder: (context) => HomeScreen(),
  ),
);

My Code Navigation of HomeScreen

Navigator.push(this.context,
  MaterialPageRoute(
    builder: (context) =\> Orders(
      numTable: numTable,
    ),
  )
);

Solution

Solution : use pushAndRemoveUntil or pushReplacement at the LoginPage

  class LoginPage extends StatelessWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
  return Scaffold(
    body: InkWell(
        onTap: ()=>Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(
            builder: (context) => HomePage(),
          )
        ,(Route<dynamic> route) => false), child: Center(child: Text("LoginPage"),)),

    );

}
}
  ------------
class HomePage extends StatelessWidget {
 const HomePage({Key? key}) : super(key: key);

 @override
 Widget build(BuildContext context) {
   return Scaffold(
   body: InkWell(
      onTap: ()=>Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => OrdersPage(),
          ))
          , child: Center(child: Text("HomePage"),)),

  );
 }
}
---------------
class OrdersPage extends StatelessWidget {
  const OrdersPage({Key? key}) : super(key: key);

  @override
   Widget build(BuildContext context) {
    return Scaffold(
  body: Center(child: Text("OrdersPage"),),
);
}
  }

Answered By – i.AGUIR

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *