arul
arul

Reputation: 143

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

Upvotes: 6

Views: 1963

Answers (3)

Mylaram Venkatesh
Mylaram Venkatesh

Reputation: 137

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

Upvotes: 0

Rohan Das
Rohan Das

Reputation: 556

Try in this method of Navigator.pushReplacement

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

Upvotes: 3

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

Upvotes: 15

Related Questions