skip to Main Content

I have a widget with this method in flutter that is called by two different screens, I would like ‘Navigator.pop’ to change its behavior depending on which screen calls it.
On the first screen it would apply a common ‘pop’, and on the second screen, for a specific route. Can you help me with this?

`

void salvarCartao(InputCartaoDto cartao, BuildContext context) async {
    var cartaoDto = await AdicionarCartaoCommand().execute(cartao, context);
    if (cartaoDto != null) {
      var usuarioCorrente = await ObterUsuarioCorrenteCommand().execute();
      var listaCartoes = usuarioCorrente?.cartoes;
      listaCartoes?.add(cartaoDto);
      AtualizarUsuarioCommand().execute(usuarioCorrente!);
    }
    //if screen 1 called the method:
    Navigator.pop(context);

    //if screen 2:
    Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento'));

  }

`

I’m actually still learning flutter, I couldn’t think of a solution with my current knowledge

2

Answers


  1. then redefine your function. Ex:

        void salvarCartao(InputCartaoDto cartao, BuildContext context, int opt) async {
            var cartaoDto = await AdicionarCartaoCommand().execute(cartao, context);
            if (cartaoDto != null) {
              var usuarioCorrente = await ObterUsuarioCorrenteCommand().execute();
              var listaCartoes = usuarioCorrente?.cartoes;
              listaCartoes?.add(cartaoDto);
              AtualizarUsuarioCommand().execute(usuarioCorrente!);
            }
            //if screen 1 called the method:
           
           if(opt ==1)
            Navigator.pop(context);
           else
            //if screen 2:
            Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento'));
        
          }
    
    Login or Signup to reply.
  2. You can pass a flag to the salvarCartao function, depending on which screen calls it.

    isFromScreen2 ? Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento')) : Navigator.pop(context);
    

    or

    if (isFromScreen2) { 
          Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento'))
       } else { 
          Navigator.pop(context); 
       }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search