1
votes

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

3
Can you share your code?Selim Kundakçıoğlu
@SelimKundakçıoğlu i have added the code. Please checkarul

3 Answers

8
votes

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

1
votes

Try in this method of Navigator.pushReplacement

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

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