본문 바로가기
Flutter/Flutter FAQ

Flutter 플러터 '뒤로' 버튼과 반환 데이터, Flutter Back button with return data

by 베타코드 2023. 12. 18.
반응형

질문


나는 두 개의 버튼이 있는 인터페이스를 가지고 있으며, 이 버튼들은 팝되고 true 또는 false를 반환합니다. 다음과 같이:

onPressed: () => Navigator.pop(context, false)

나는 앱바의 뒤로 가기 버튼을 조정해야 하는데, 이 버튼은 팝되고 false를 반환해야 합니다. 이를 수행할 수 있는 방법이 있을까요?


답변


더 쉬운 방법은 WillPopScope로 body를 감싸는 것입니다. 이렇게 하면 위쪽의 뒤로 가기 버튼아래쪽의 Android 뒤로 가기 버튼 모두 작동합니다.

두 개의 뒤로 가기 버튼이 모두 false를 반환하는 예제입니다:

final return = Navigator.of(context).push(MaterialPageRoute<bool>(
    builder: (BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("새 페이지"),
        ),
        body: WillPopScope(
          onWillPop: () async {
             Navigator.pop(context, false);
             return false;
          },
          child: newPageStuff(),
        ),
      );
    },
));

다른 답변에서는 다음을 사용하는 것을 제안했습니다:

leading: BackButton(...)

이 방법은 위쪽의 뒤로 가기 버튼에서만 작동하고 Android 버튼에서는 작동하지 않는 것을 발견했습니다.

아무튼 예제를 포함하겠습니다:

final return = Navigator.of(context).push(MaterialPageRoute<bool>(
    builder: (BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          leading: BackButton(
            onPressed: () => Navigator.pop(context, false),
          ),
          title: Text("새 페이지"),
        ),
        body: newPageStuff(),
      );
    },
));
반응형

댓글