'Web view page is empty if clicks the back arrow in flutter?
I have a WebView
page with multiple links. By clicking the links it will open another WebView
page with a close button. If I click the close button, the current window should close and WebView
page should not reload. I tried using onPressed: () => Navigator.of(context).pop()
but it shows WebView
page as empty. Please help to resolve this.
class Leader extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( body: Stack( children: [ WebView( initialUrl: 'web view url', javascriptMode: JavascriptMode.unrestricted, navigationDelegate: (NavigationRequest request) { print(request.url); var url = request.url; Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => WebView2(urlVal: url))); return NavigationDecision.navigate; }, ), ] ), ); } } class WebView2 extends StatefulWidget { final String urlVal; WebView2({Key key, @required this.urlVal}) : super(key: key); @override _WebView2State createState() => _WebView2State(); } class _WebView2State extends State { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Stack( children: [ SimplePdfViewerWidget( completeCallback: (bool result){ print("completeCallback,result:${result}"); }, initialUrl: widget.urlVal, ), Align( alignment: Alignment.bottomCenter, child: SizedBox( width: 330, child: RaisedButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Close', style:TextStyle(fontSize:20)), textColor: Colors.white, color: Colors.blue, elevation: 5 ), ) ) ] ) ), ); } }
Solution 1:[1]
You are using Navigator.pushReplacement()
in your first WebView
widget. So when you try to pop the second WebView
there are no widgets to show. Instead of using Navigator.pushReplacement()
try to use Navigator.push()
Solution 2:[2]
Try in this method of Navigator.pushReplacement
Navigator.pushReplacement<void>(
context,
MaterialPageRoute<void>( builder: (_) => ViewScreen()),
);
Solution 3:[3]
Dont use return after navigator push
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
WebView(
initialUrl: 'web view url',
javascriptMode: JavascriptMode.unrestricted,
navigationDelegate: (NavigationRequest request) {
print(request.url);
var url = request.url;
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => WebView2(urlVal: url)));
;
},
),
]
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Selim Kundakç?o?lu |
Solution 2 | Røhäñ Dås |
Solution 3 | Paresh Mangukiya |