【问题标题】:Web view page is empty if clicks the back arrow in flutter?如果单击颤动中的后退箭头,网页视图页面为空?
【发布时间】:2020-05-19 20:32:11
【问题描述】:

我有一个带有多个链接的WebView 页面。通过单击链接,它将打开另一个带有关闭按钮的WebView 页面。如果我单击关闭按钮,当前窗口应该关闭并且WebView 页面不应该重新加载。我尝试使用onPressed: () => Navigator.of(context).pop(),但它显示WebView 页面为空。请帮助解决这个问题。

班长扩展 StatelessWidget { @覆盖 小部件构建(BuildContext 上下文){ 返回材料应用程序( 标题:“颤振演示”, 主题:主题数据( primarySwatch:颜色.蓝色, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } 类 MyHomePage 扩展 StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); 最终字符串标题; @覆盖 _MyHomePageState createState() => _MyHomePageState(); } 类 _MyHomePageState 扩展状态 { @覆盖 小部件构建(BuildContext 上下文){ 返回脚手架( 正文:堆栈( 孩子们: [ 网页视图( initialUrl: '网页浏览网址', javascriptMode: JavascriptMode.unrestricted, navigationDelegate: (NavigationRequest 请求) { 打印(request.url); var url = request.url; Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => WebView2(urlVal: url))); 返回 NavigationDecision.navigate; }, ), ] ), ); } } WebView2 类扩展 StatefulWidget { 最终字符串 urlVal; WebView2({Key key, @required this.urlVal}) : super(key: key); @覆盖 _WebView2State createState() => _WebView2State(); } 类 _WebView2State 扩展状态 { @覆盖 小部件构建(BuildContext 上下文){ 返回材料应用程序( 家:脚手架( 正文:堆栈( 孩子们: [ SimplePdfViewerWidget( 完成回调:(布尔结果){ print("completeCallback,result:${result}"); }, 初始网址:小部件.urlVal, ), 对齐( 对齐:Alignment.bottomCenter, 孩子:大小框( 宽度:330, 孩子:RaisedButton( onPressed: () => Navigator.of(context).pop(), 孩子:常量文本('关闭',样式:TextStyle(字体大小:20)), 文本颜色:颜色.白色, 颜色:颜色.蓝色, 海拔:5 ), ) ) ] ) ), ); } }

【问题讨论】:

  • 你能分享你的代码吗?
  • @SelimKundakçıoğlu 我已经添加了代码。请检查

标签: flutter dart flutter-layout


【解决方案1】:

您在第一个 WebView 小部件中使用了 Navigator.pushReplacement()。因此,当您尝试弹出第二个 WebView 时,没有要显示的小部件。而不是使用Navigator.pushReplacement() 尝试使用Navigator.push()

【讨论】:

    【解决方案2】:

    导航器推送后不要使用return

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

    【讨论】:

      【解决方案3】:

      试试Navigator.pushReplacement这个方法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        相关资源
        最近更新 更多