首页 > 解决方案 > 在后台打开网站并在 Flutter 应用程序中访问其字段

问题描述

在我的颤振应用程序中,我想从特定网站上的文本字段中读取和写入。我正在谈论的网站没有后端,当您在其计算字段中输入值时,它会在浏览器中执行 javascript 代码。我想将我的应用程序中的值读/写到这些文本字段中。是否可以使用 webview 来实现这一点?这可能吗?如果可能的话,我不想将用户拉出我的应用程序 ui。为自己实施计算工具会花费大量时间。

标签: flutter

解决方案


这是开始的一种方法。这使用本地托管的文件作为 web 视图,但没有理由不能将其调整为公共网站。

RaisedButton(
onPressed: () {
                  showAboutDialog(
                      context: context,
                      children: [
                        Column(mainAxisSize: MainAxisSize.min, children: [
                          Text("Please don't sue me"),
                          Container(
                            height: 160,
                            child: SingleChildScrollView(
                              child: Container(
                                height: 180,
                                child: WebView(
                                  initialUrl: "",
                                  javascriptMode: JavascriptMode.unrestricted,
                                  // this is defined elsewhere and fixes a (at one time) bug with gesture detection
                                  gestureRecognizers: [
                                    Factory(() =>
                                        PlatformViewVerticalGestureRecognizer()),
                                  ].toSet(),                                  
                                  onWebViewCreated: (WebViewController
                                      webViewController) async {
                                    _controller = webViewController;
                                    await loadHtmlFromAssets(
                                        'public/index.html', _controller);
                                  },
                                ),
                              ),
                            ),
                          ),
                        ]),
                      ],
                      applicationVersion: "Version 0.1",
                      applicationName: "Your Application",
                      applicationLegalese: "",
                      applicationIcon:
                          ImageIcon(AssetImage("assets/images/pos_icon.png")));
                },
                child: Text("About, and Legalese for masochists"),
),

推荐阅读