pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.01k stars 1.33k forks source link

Throw [ERROR:aw_browser_terminator.cc(154)] when close window, onCloseWindow does not work #2081

Open phuocbitmark opened 1 month ago

phuocbitmark commented 1 month ago

Environment

Technology Version
Flutter version 3.19
Plugin version 6.0.0
Android version
iOS version
macOS version 14.4
Xcode version 15.3
Google Chrome version

Device information:

Description

I have a screen that have InAppWebView on it when i close the page (Navigator.of(context).pop();) it throw errors: on android:

[ERROR:aw_browser_terminator.cc(154)] Renderer process (26451) crash detected (code -1).

on iOS

"FlutterWebViewController - dealloc"
"WebViewChannelDelegate - dealloc"
"FindInteractionControl - dealloc"
"PullToRefreshControl - dealloc"
"InAppWebView - dealloc"

I can still close the page, it look normal on UI, but i can not use

onCloseWindow: (controller) {
     print('onCloseWindow');
},

won't print out anything I'm not sure that i'm missing anything, what i need to do is to clear webstorage when window is closed

Expected behavior: onCloseWindow work

Current behavior:

Steps to reproduce

class InAppWebViewPage extends StatefulWidget {
  final InAppWebViewPayload payload;

  const InAppWebViewPage({required this.payload, super.key});

  @override
  State<InAppWebViewPage> createState() => _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  late InAppWebViewController webViewController;
  late String title;
  late bool isLoading;
  final _configurationService = injector<ConfigurationService>();

  @override
  void initState() {
    title = Uri.parse(widget.payload.url).host;
    isLoading = false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final version = _configurationService.getVersionInfo();
    return Scaffold(
      appBar: getDarkEmptyAppBar(Colors.black),
      backgroundColor: widget.payload.backgroundColor ?? theme.primaryColor,
      body: Column(
        children: [
          if (!widget.payload.isPlainUI) ...[
            _header(context),
            addOnlyDivider(color: AppColor.auGrey)
          ],
          Expanded(
            child: Stack(
              children: [
                InAppWebView(
                  initialUrlRequest:
                      URLRequest(url: WebUri(widget.payload.url)),
                  initialSettings: InAppWebViewSettings(
                    userAgent: 'user_agent'.tr(namedArgs: {'version': version}),
                  ),
                  onPermissionRequest: (InAppWebViewController controller,
                      permissionRequest) async {
                    if (permissionRequest.resources
                        .contains(PermissionResourceType.MICROPHONE)) {
                      await Permission.microphone.request();
                      final status = await Permission.microphone.status;
                      if (status.isPermanentlyDenied || status.isDenied) {
                        return PermissionResponse(
                            resources: permissionRequest.resources);
                      }
                      return PermissionResponse(
                          resources: permissionRequest.resources,
                          action: PermissionResponseAction.GRANT);
                    }
                    return PermissionResponse(
                        resources: permissionRequest.resources);
                  },
                  onWebViewCreated: (controller) {
                    if (widget.payload.onWebViewCreated != null) {
                      widget.payload.onWebViewCreated!(controller);
                    }
                    webViewController = controller;
                  },
                  onConsoleMessage: widget.payload.onConsoleMessage,
                  onLoadStart: (controller, uri) {
                    setState(() {
                      isLoading = true;
                      title = uri!.host;
                    });
                  },
                  onCloseWindow: (controller) {
                    print('onCloseWindow');
                  },
                  onLoadStop: (controller, uri) {
                    setState(() {
                      isLoading = false;
                    });
                  },
                ),
                if (isLoading)
                  Container(
                    color: AppColor.white,
                    child: Center(
                      child: loadingIndicator(),
                    ),
                  )
                else
                  const SizedBox(),
              ],
            ),
          ),
          if (!widget.payload.isPlainUI) ...[
            addOnlyDivider(color: AppColor.auGrey),
            _bottomBar(context)
          ],
        ],
      ),
    );
  }

  Widget _header(BuildContext context) => Stack(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: Container(
              decoration: const BoxDecoration(
                  color: AppColor.greyMedium,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10),
                    topRight: Radius.circular(10),
                  )),
              height: 75,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10),
            child: Container(
              alignment: Alignment.center,
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10),
                  topRight: Radius.circular(10),
                ),
              ),
              height: 75,
              child: SizedBox(
                  height: 50,
                  child: _appBar(
                    context,
                    title: title,
                    onClose: () {
                      Navigator.of(context).pop();
                    },
                  )),
            ),
          ),
        ],
      );

  AppBar _appBar(BuildContext context,
      {required Function()? onClose, String title = ''}) {
    final theme = Theme.of(context);
    return AppBar(
      centerTitle: true,
      automaticallyImplyLeading: false,
      title: Text(
        title,
        overflow: TextOverflow.ellipsis,
        style: theme.textTheme.ppMori400Black16,
        textAlign: TextAlign.center,
      ),
      actions: [
        IconButton(
          tooltip: 'CLOSE',
          onPressed: onClose,
          icon: closeIcon(),
        )
      ],
      backgroundColor: Colors.transparent,
      shadowColor: Colors.transparent,
      elevation: 0,
    );
  }

  Widget _bottomBar(BuildContext context) => Container(
        color: AppColor.white,
        padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 50),
        child: Row(
          children: [
            IconButton(
              icon: const Icon(AuIcon.chevron),
              onPressed: () async {
                if (await webViewController.canGoBack()) {
                  await webViewController.goBack();
                }
              },
            ),
            const Spacer(),
            IconButton(
              icon: const RotatedBox(
                  quarterTurns: 2, child: Icon(AuIcon.chevron)),
              onPressed: () async {
                if (await webViewController.canGoForward()) {
                  await webViewController.goForward();
                }
              },
            ),
            const Spacer(),
            IconButton(
              icon: SvgPicture.asset('assets/images/Reload.svg'),
              onPressed: () {
                unawaited(webViewController.reload());
              },
            ),
            const Spacer(),
            IconButton(
              icon: SvgPicture.asset('assets/images/Share.svg'),
              onPressed: () async {
                final currentUrl = await webViewController.getUrl();
                if (currentUrl != null) {
                  unawaited(launchUrl(
                    currentUrl,
                    mode: LaunchMode.externalApplication,
                  ));
                }
              },
            ),
          ],
        ),
      );
}

class InAppWebViewPayload {
  final String url;
  final bool isPlainUI;
  final Color? backgroundColor;
  Function(InAppWebViewController controler)? onWebViewCreated;
  Function(InAppWebViewController controler, ConsoleMessage consoleMessage)?
      onConsoleMessage;

  InAppWebViewPayload(this.url,
      {this.isPlainUI = false,
      this.onWebViewCreated,
      this.onConsoleMessage,
      this.backgroundColor});
}

Images

Stacktrace/Logcat

github-actions[bot] commented 1 month ago

👋 @phuocbitmark

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!