flutter 根据源屏幕更改方法行为

evrscar2  于 2022-12-19  发布在  Flutter
关注(0)|答案(2)|浏览(115)

我有一个在flutter中使用此方法的小部件,它被两个不同的屏幕调用,我想让“Navigator.pop”根据哪个屏幕调用它来改变它的行为。在第一个屏幕上,它会应用一个普通的“pop”,而在第二个屏幕上,它会应用一个特定的路线。你能帮我吗?
'

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

  }

'
实际上我还在学习Flutter,以我目前的知识,我想不出一个解决方案

2izufjch

2izufjch1#

然后重新定义函数。例如:

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

mf98qq942#

您可以将标志传递给salvarCartao函数,具体取决于调用它的屏幕。

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

if (isFromScreen2) { 
      Navigator.popUntil(context, ModalRoute.withName('/carrinho-pagamento'))
   } else { 
      Navigator.pop(context); 
   }

相关问题