6

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),
            ))
      ])),
    );
  }
}
2
  • Can you share your code? Feb 4, 2020 at 6:07
  • @SelimKundakçıoğlu i have added the code. Please check
    – arul
    Feb 4, 2020 at 6:21

3 Answers 3

15

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()

0
3

Try in this method of Navigator.pushReplacement

Navigator.pushReplacement<void>(
  context,
  MaterialPageRoute<void>( builder: (_) => ViewScreen()),
);

0

Don't 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)));
            ;
          },
        ),
      ]),
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.