Reputation: 311
In my flutter application which is a webview there is a button to share details from app to whatsapp and whenever the share icon is clicked the whatsapp screen opens but when user clicks the back button from whatsapp to come to app screen the screen is turning white. This occurs only on device 14. Any help?
const hybridCompositionApiLevel = 29;
class WebViewWidget extends StatefulWidget {
final int apiVersion;
const WebViewWidget({Key key, this.apiVersion}) : super(key: key);
@override
_WebViewWidget createState() => _WebViewWidget();
}
class _WebViewWidget extends State<WebViewWidget> with WidgetsBindingObserver {
String URL;
bool isLoading = true;
final _key = UniqueKey();
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
@override
Future<void> initState() async {
await init();
WidgetsBinding.instance?.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
if (state == AppLifecycleState.resumed) {
//do your stuff
webViewController.loadUrl(Uri.encodeFull(URL));
}
}
// @override
// void initState() {
// init();
// super.initState();
// }
init() async {
if (widget.apiVersion != null &&
widget.apiVersion >= hybridCompositionApiLevel) {
print("hybrid composition");
WebView.platform = SurfaceAndroidWebView();
}
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: SafeArea(child: Scaffold(body: _showWebView())),
);
}
WebViewController webViewController;
Widget _showWebView() {
return Center(
child: Stack(
children: <Widget>[
WebView(
initialUrl: '',
javascriptMode: JavascriptMode.unrestricted,
javascriptChannels: <JavascriptChannel>[
_toasterJavascriptChannel(context),
].toSet(),
onPageFinished: (url) async {
setState(() {
isLoading = false;
});
print("came here");
try {
var javascript = '''
window.alert = function (e){
Alert.postMessage(e);
}
''';
await webViewController?.evaluateJavascript(javascript);
} catch (_) {}
},
navigationDelegate: (NavigationRequest request) async {
setState(() {
isLoading = false;
});
String requestUrlString = request.url;
URL = requestUrlString;
print('callback - $requestUrlString');
print("Url Print " + request.url);
if (request.url.startsWith("tel:") ||
request.url.startsWith("whatsapp:")) {
print('Enter in whatsApp ${request.url}');
var whatsAppUrl = request.url;
launchUrl(whatsAppUrl, context);
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
onWebViewCreated: (WebViewController _webViewController) async {
setState(() {
isLoading = false;
});
webViewController = _webViewController;
webViewController.loadUrl(Constants.mainUrl);
// await marketActProv.loadFromAssets();
},
),
isLoading
? Center(
child: CircularProgressIndicator(),
)
: Stack(),
],
),
);
}
void launchUrl(String whatsAppUrl, BuildContext context) async {
await canLaunch(whatsAppUrl)
? launch(whatsAppUrl)
: showAlert(context: context, message: "Please Install WhatsApp");
}
JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
return JavascriptChannel(
name: 'Alert',
onMessageReceived: (JavascriptMessage message) {
print("onMessageReceived");
print(message.message);
showAlert(context: context, message: message.message);
});
}
}
Upvotes: 2
Views: 703