简体   繁体   English

Flutter 网页视图

[英]Flutter webView

I've created a list of web addresses of some websites which I want to go to on a click of their corresponding buttons in the app(1st button for 1st website, 2nd for the second one and so on...).我已经创建了一些网站的网址列表,我想通过单击应用程序中相应的按钮来访问这些网站(第一个按钮用于第一个网站,第二个按钮用于第二个网站,依此类推......)。 I'm using the 'flutter-webview-plugin' which has all the properties of "Scaffold" and also a property "url".我正在使用“flutter-webview-plugin”,它具有“Scaffold”的所有属性以及一个属性“url”。 I'm not able to access those websites 'index-wise' in the "url" property(like we access the images in a list, index wise).我无法在“url”属性中“按索引”访问这些网站(就像我们按索引访问列表中的图像一样)。

My code:我的代码:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Just Another App',
    home: Home(),
    routes: {
      "/webview": (_) => WebviewScaffold(
            withJavascript: true,
            withLocalStorage: true,
            url: ,
            appBar: AppBar(
              title: Text('Browser'),
            ),
          ),
    },
    theme: ThemeData(primaryColor: Colors.black),
  );
} 
}

I haven't shared the code for the buttons as its already been done and this is the only problem left to be solved.我没有分享按钮的代码,因为它已经完成了,这是唯一需要解决的问题。 Any help is really appreciated.非常感谢任何帮助。

Declare a WebViewController like this,像这样声明一个WebViewController

WebViewController controller;

then on each button press change url,然后在每个按钮上按下更改网址,

          RaisedButton(onPressed: () {
              url = 'https://flutter.dev/docs/get-started/editor';
              setState(() {
                print(url);
                controller.loadUrl(url);
              });
            }),
            RaisedButton(onPressed: () {
              url = 'https://flutter.dev/docs/get-started/test-drive';
              setState(() {
                print(url);
                controller.loadUrl(url);
              });
            }),

Now inside webview declare your controller,现在在 webview 中声明你的控制器,

            Expanded(
              child: WebView(
                initialUrl: url,
                onWebViewCreated: (WebViewController webViewController) {
                  controller = webViewController;
                },
              ),
            ),

I have created a working example here我在这里创建了一个工作示例


Additionally , To enable click events on WebView you have to enable javascript in your webview by adding javascriptMode: JavascriptMode.unrestricted in your code.此外,要在 WebView 上启用点击事件,您必须通过在代码中添加javascriptMode: JavascriptMode.unrestricted来在 Webview 中启用 javascript。 Because,因为,

By default, JavaScript in your WebView is disabled默认情况下,WebView 中的 JavaScript 是禁用的

For ex:例如:

WebView(
         initialUrl: 'https://flutter.dev/',
        javascriptMode: JavascriptMode.unrestricted,
      ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM