This seems like it should work, doesn’t work in dartpad either.
Usually I use combination of named routes and the static navigator push and pop and I can get the behavior I want for multiple pushes, so I am confused how this doesn’t work.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
// onGenerateRoute: TestRouter.generateRoute,
home:A()));
}
class A extends StatefulWidget {
const A({ super.key });
State createState() => AState();
}
class AState extends State {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("A"),
),
body:
ListView(children: [
GestureDetector(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context){
return B();
},
));
},
child:Container(
color: Colors.blue,
height: 100, width: 100, child: Center(child:Text("B")),)),
],)
);
}
}
class B extends StatefulWidget {
const B({ super.key });
State createState() => BState();
}
class BState extends State {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("B"),
),
body:
ListView(children: [
GestureDetector(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context){
return C();
}));
},
child:Container(
color: Colors.red,
height: 100, width: 100, child: Center(child:Text("C")),)),
],)
);
}
}
class C extends StatefulWidget {
const C({ super.key });
State createState() => BState();
}
class CState extends State {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("C"),
),
body:
ListView(children: [
GestureDetector(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context){
return A();
}));
},
child:Container(
color: Colors.green,
height: 100, width: 100, child: Center(child:Text("A")),)),
],)
);
}
}
I was expecting to see route C but only B is pushed then it seems to be broken
2
Answers
In your C state class you are using BState i think that might be the issue so its try to change it to C
In the C class, you’re using BState instead of CState
Change
To