dev/flutter

[Flutter] Backpressed WillPopScope #pop

donguran 2024. 4. 3. 13:53
728x90
반응형

웹에서 뒤로가기시 이전 화면을 보여주기

: 우선, WillPopScope로 보여준다

: return false는 뒤로가지 못하게 막는 것

: return true는 뒤로가도록 허용하는 것

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: () async {
      if (await webviewController.canGoBack()) {
        webviewController.goBack();
        return false;
      } else {
        return true;
      }
    },
    child: Scaffold(
      body: WebViewWidget(
          controller: webviewController,
        ),
    ),
  );
}

 

android에서 backpressDispatcher를 만져줘야 하나 했지만, dart엔진 위에 그려지는 것이기 때문에

걱정할 필요가 없는 듯하다.

 

WillPopScope는 Deprecated된 위젯이기 때문에 PopScope로 변환해보자,

@override
Widget build(BuildContext context) {
  return PopScope(
    canPop: false,
    onPopInvoked: (bool didPop) async {
      if (await webviewController.canGoBack()) {
        webviewController.goBack();
      } else {
        SystemNavigator.pop(animated: true);
      }
    },
    child: Scaffold(
      body: WebViewWidget(
          controller: webviewController,
        ),
    ),
  );
}
728x90
반응형