1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Why Navigator push seems to be not working in small snippet

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 14:02.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página