(flutter) Navigation(1) – 이전 화면으로 돌아가지 않는 기능 (pushAndRemoveUntil)

이 기사 “노마드 코더 – 틱톡 클론 코더” 수업시간에 작성한 글입니다.

유료 강의라 정리가 필요한 부분만 발췌했습니다.

이해해 주셔서 감사합니다.


푸시 네비게이터

이전 화면 이상 새 사진기능적 범위
또한 이전 뒤로 버튼과 뒤로 버튼을 추가합니다.

LoginFormScreen 존재하다 interestsScreen 통과할 때 발생하므로 이 오류를 수정해야 합니다.

그러므로 push그리고 동시에 전에 모든 화면 삭제길을 바꾸다


  • login_form_screen.dart

하나. false사례

void _onSubmitTap() {
    if (_formKey.currentState != null) {
      if (_formKey.currentState!.validate()) {
        _formKey.currentState!.save();
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
              builder: (context) => const InterestsScreen(),
            ), (route) {
          return false; // 이전의 위젯들을 저장하지 않음.(뒤로 가기 기능 X)
        });
      }
    }
  }

2. true사례

void _onSubmitTap() {
    if (_formKey.currentState != null) {
      if (_formKey.currentState!.validate()) {
        _formKey.currentState!.save();
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
              builder: (context) => const InterestsScreen(),
            ), (route) {
          return true; // 이전의 위젯들을 저장(뒤로 가기 기능 ○)
        });
      }
    }
  }


  • birthday_screen.dart 편집
void _onNextTap() {
    Navigator.of(context).pushAndRemoveUntil(
        // false : 위젯 삭제 , true : 해당 route 유지
        MaterialPageRoute(
          builder: (context) => const InterestsScreen(),
        ),
        (route) => false);
  }

  • tutorial_screen.dart 수정
  1. 버튼을 클릭해도 돌아오지 않는 기능 추가
  2. 특징 / main_navigation/main_navigation_screen.dart 생산
    (버튼을 클릭했을 때 표시되는 화면)
  void _onEnterAppTap() {
    Navigator.of(context).pushAndRemoveUntil(
        // false : 위젯 삭제 , true : 해당 route 유지
        MaterialPageRoute(
          builder: (context) => const MainNavigationScreen(),
        ),
        (route) => false);
  }
child: CupertinoButton(
                onPressed: _onEnterAppTap,
                color: Theme.of(context).primaryColor,
                child: const Text('Enter the app!'),
),