For my Flutter app I need to be able to open/download pdf files on Android without leaving the app. In short, now it looks like this:
...
InAppWebView(
...
shouldOverrideUrlLoading: (controller, nav) async {
final Uri? url = nav.request.url;
WebViewUtils.launchInBrowserOverlay(result?.value);
},
... );
...
static launchInBrowserOverlay(value) async {
Uri uri = Uri.parse(value);
ChromeSafariBrowser().open(
url: uri,
options: ChromeSafariBrowserClassOptions(
android: AndroidChromeCustomTabsOptions(
packageName: 'com.android.chrome',
),
),
);
}
All in all, it works (pdf file will be downloaded) with some restrictions.
The problem is when the file has already been downloaded, the browser only shows "Download is pending...". This is not the desired behaviour. The best solution would be just to show the pdf file without leaving the app. Another option would be to start the download even if the file has already been downloaded (I could do it with launchUrl()
from url_launcher
but then I would leave the app).