Skip to content

fix(navigation): histórico de navegação para o botão voltar do Android - #75

Merged
egionCode merged 3 commits into
mainfrom
fix/android-back-navigation-history
Aug 10, 2026
Merged

egionCode merged 3 commits into
mainfrom
fix/android-back-navigation-history

Conversation

@egionCode

Copy link
Copy Markdown
Owner

Problema

O botão físico de voltar do Android fechava o aplicativo em qualquer tela. Toda a navegação usava context.go(...), que substitui a pilha do go_router: sem pilha, o back não tinha o que desempilhar.

Solução

Novo lib/shared/routing/navigation.dart:

  • context.backOr(fallback): desempilha se houver histórico, senão navega para a rota pai (cobre deep link e reload no web, onde a pilha nasce vazia).
  • AppBackHandler: PopScope(canPop: false) que centraliza a decisão do back físico:
    1. tem rota anterior -> router.pop()
    2. sem histórico, com fallbackLocation -> router.go(fallback)
    3. tela raiz (fallbackLocation == null) -> diálogo de confirmação e SystemNavigator.pop()

Diálogos e bottom sheets continuam fechando normalmente: o framework só consulta a rota do topo, e o AppBackHandler fica na rota da tela, abaixo deles.

Mudanças de navegação

Antes Agora
go para avançar (home -> fichas/NPCs/campanhas, campanha -> sessão, lista -> formulário, login -> registro) push (empilha)
go nos botões voltar/cancelar e após salvar backOr('<rota pai>')
go após criar/entrar em campanha e troca de sessão pushReplacement (o formulário sai do histórico)
go('/') ao finalizar/sair da campanha mantido (reset intencional da pilha)

As 10 telas foram envolvidas com AppBackHandler, incluindo os estados de loading/erro. Home e login são raiz e pedem confirmação de saída.

Testes

test/shared/routing/navigation_test.dart dispara a mensagem de plataforma popRoute (o mesmo caminho do back físico) e cobre: pop para a tela anterior, fechamento de diálogo, fallback sem histórico e o fluxo de confirmação de saída (cancelar não chama SystemNavigator.pop, confirmar chama).

  • flutter analyze: sem issues
  • flutter test: 139 testes passando

Validação pendente

Sem teste em device físico: com canPop: false o gesto de back preditivo do Android 14+ perde a animação de preview, mantendo o comportamento funcional.

🤖 Generated with Claude Code

egionCode and others added 3 commits August 10, 2026 08:23
Adds AppBackHandler (PopScope) and the context.backOr extension so the
Android hardware back button pops the router stack, falls back to a parent
route when the stack is empty (deep link or web reload) and asks for exit
confirmation on root screens.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Forward navigation now uses context.push instead of context.go, which was
replacing the whole stack and left the hardware back button with nothing to
pop (closing the app). Back/cancel actions use context.backOr with the parent
route, screens created from a form use pushReplacement so the form leaves the
history, and every screen is wrapped in AppBackHandler.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sends the platform popRoute message to assert popping to the previous screen,
dialog dismissal, the parent fallback when there is no history, and the exit
confirmation flow on the root screen.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@egionCode egionCode added bug Something isn't working tests New test cases or test updates labels Aug 10, 2026
@egionCode
egionCode merged commit c4bc468 into main Aug 10, 2026
1 check passed
@egionCode
egionCode deleted the fix/android-back-navigation-history branch August 10, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working tests New test cases or test updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant