Home > Software design >  Changing method behavior depending source screen
Changing method behavior depending source screen

Time:12-14

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

CodePudding user response:

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'));
    
      }

CodePudding user response:

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); 
   }

CodePudding user response:

One solution to this problem is to use the Navigator.popUntil method to pop the screen that called the salvarCartao method, and then use a conditional statement to determine which screen called the method.

Here is an example of how to implement this solution:

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!);
  }

  // Pop the screen that called the salvarCartao method
  Navigator.popUntil(context, ModalRoute.withName(ModalRoute.of(context).settings.name));

  // Use a conditional statement to determine which screen called the method
  if (ModalRoute.of(context).settings.name == '/screen1') {
    // If screen 1 called the method, pop the screen normally
    Navigator.pop(context);
  } else if (ModalRoute.of(context).settings.name == '/screen2') {
    // If screen 2 called the method, pop all screens until the carrinho-pagamento screen
    Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento'));
  }
}

In the code above, the Navigator.popUntil method is used to pop the screen

  • Related