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 the 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<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: <Widget>[
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<WebView2> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(children: <Widget>[
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),
))
])),
);
}
}