EEYatHo 앱 깎는 이야기

Flutter ) 탭바 돌아와도 initState 안하기 - EEYatHo Flutter 본문

Flutter/Tip

Flutter ) 탭바 돌아와도 initState 안하기 - EEYatHo Flutter

EEYatHo 2022. 10. 28. 17:20
반응형

 

DefaultTabController 를 기반으로 탭바를 구현했을 때,

다른 탭으로 가면 기존에 보이던 화면이 dispose (deinit) 된다.

 

그래서 다시 돌아왔을 때는 initState 부터 시작한다.

 

이를 방지하기 위해, 다른 탭에가도 dispose 되지 않게 할 수 있다.

 

 

Solution


dispose 되지 않길 바라는 화면의 State 에 AutomaticKeepAliveClientMixin 을 Mixin 하고,

wantKeepAlive 를 true 로 override 해준다.

// dispose 되지 않길 바라는 화면의 State
class _SomeTabState extends State<SomeTab> with AutomaticKeepAliveClientMixin {
  ...	
  // 다른 탭 갔다와도 dispose 안하기 ( with AutomaticKeepAliveClientMixin )
  @override
  bool get wantKeepAlive => true;
  ...
}

'Flutter > Tip' 카테고리의 다른 글

Flutter ) OS 구분 - EEYatHo Flutter  (0) 2022.09.21
Comments