개발/Flutter

[Navigator] popUntil

leebera_ 2022. 6. 14. 20:53

Navigator클래스 내의 static인 popUntil과 NavigatorState클래스의 멤버 함수 두 개가 있다.

그 중 static인 함수는 첫번째 매개변수의 BuildContext를 통해 NavigatorState를 찾은 뒤에 NavigatorState클래스의 popUntil을 호출하도록 되어있다.

콜백 함수의 반환 값이 true일 때 pop을 그만하도록 구현되어있다.

 

예시

 

내가 유용하게 쓴 곳은 BottomNavigationBar를 두고 각 탭별로 NavigatorState를 가지고 있게 한 상황이였다.

게시판이라는 탭이 있으면 게시판에서 어떤 글을 보고 있을 때 같은 탭인 게시판을 누르면 게시판 탭의 메인 화면으로 이동하도록 했고 이미 메인 탭일 땐 아무런일도 일어나지 않도록 구현했었다.

 


 

참고

 

How to get current route path in Flutter?

While implementing persistent bottom bar, previous route need to be restored when a button in the bottom bar was clicked. When a button in the bottom bar is clicked, its current route path (/a/b/...

stackoverflow.com

 

How to check which the current Route is?

I want to navigate to different Routes using a Drawer, though I do not want to open a new instance of a Route each time I tap on it if I am already on that Route, rather I would prefer that in this...

stackoverflow.com