Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

dart - Flutter : How to recover a screen with data in the Navigator stack

I am making a sales app, where I use a drawer to move between screens using the Navigator commands, and found the following situation

1 - When opening the app it enters on the main screen where the sale is made

2 - I select two products to sell

3 - right after I leave the sales screen that has the two products and go to customers screen

4 - now I need to go back to the sales screen, and the two products that had already been selected should still be there

Right, in this part 4, using the key to go back on the cell phone it returns a screen and goes to this sales screen (with the two products there), but if I go in the drawer and select the option to go to the sales screen, how do I search this screen at the beginning of the stack instead of creating a new sales screen (which comes empty without the two products). I want this because if it is only one screen on top of the stack, no problem, the user presses to go back and that's it, but if there are for instance 5 or 6 screens on top, it is a great wear and tear for the user to have to go back everything, pressing several times on the back, to not lose what was already made on the sales screen. Is there any navigator command that does this? I've already looked for some things, but I didn't find something that helps me.

Drawer code

 @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return MultiLevelDrawer(
            backgroundColor: Color(0xfff0f3f4),
            rippleColor: Colors.white,
            subMenuBackgroundColor: Color(0xfff0f3f4),
            divisionColor: Colors.grey,
            header: Container(
              height: size.height * 0.25,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                  Image.asset(
                  "Imagens/ACMIL_CIRCULAR.png",
                  width: 100,
                  height: 100,
                ),
                SizedBox(
                  height: 10,
                ),
                Text("Usuario : Iury")
              ],
            )),
            ),
            children: [
            MLMenuItem(
                leading: Icon(Icons.person),
                trailing: Icon(Icons.arrow_right),
                content: Text(
                  "Cadastro",
                ),
                subMenuItems: [
                  MLSubmenu(
                      onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => ClientePesquisa("",0,"",0)));
                      },
                      submenuContent: Text("Cliente")),
                  MLSubmenu(onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => ProdutoPesquisa(0,"",0,0,"")));
                  }, 
                  submenuContent: Text("Produto")),
                  MLSubmenu(onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => VendedorPesquisa(0,"",0)));
                  }, 
                  submenuContent: Text("Vendedor")),
                  MLSubmenu(onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => CondPGTOPesquisa("","","","")));
                  }, 
                  submenuContent: Text("Cond. de Pagamento")),
                  MLSubmenu(onClick: () {
                        Navigator.of(context).pop();
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => FormaPGTOPesquisa(0,"")));
                  }, 
                  submenuContent: Text("Forma de Pagamento")),
                ],
                onClick: () {}),
            MLMenuItem(
              leading: Icon(Icons.local_grocery_store),
              content: Text("Pré-Venda"),
              onClick: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => PedidoVenda()));
              },
            ),
            MLMenuItem(
              leading: Icon(Icons.settings),
              content: Text("Configura??o"),
              onClick: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => Configuracao()));
              }
            )],
          );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are looking for is something called state management. As a user already explained why a framework like Flutter makes managing state difficult, you can read their comment under your post.

There are many state management solutions, but the two most adopted and generally most used are:

  1. Provider
  2. BLoC

Now, almost 99% of the time it comes down to personal preference and which variant you are most comfortable with.

If you want to read more on each of the variations, you can read some great guides on BLoc and provider on Medium.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...