0

在 Flutter 中的 TextFormField 中,我想输入一个 url 或网站链接以将其保存到 firebase 数据库,并且应该使用浏览器打开链接,即在保存并将其保存到 firebase 数据库之前应该可以点击它。

Hece它应该打开网站或与borwser等链接。

如何在flutter中实现这个功能

我使用了 url_launcher: plugin 并导入了 import 'package:url_launcher/url_launcher.dart';

我的代码如下

 child: TextFormField(
                        validator: (value){
                          if(value.isEmpty){
                            return "Please write the Company Website of production";
                          }else {
                            website = value;
                          }
                        },

                        keyboardType: TextInputType.name,
                        autofocus: false,
                       // controller: _controller,
                        decoration: InputDecoration(
                          labelText: ' Company Website ',
                          hintText: 'Enter the Company Website',
                          prefixIcon: IconButton(
                            onPressed: () async {
                              if (await canLaunch("url")) {
                                await launch("url");
                              }
                            },
                            icon: Icon(Icons.open_in_browser),
                          ),
                        ),
                        maxLength: 15,
                      )

请指导我解决此问题。

4

1 回答 1

0

您可以尝试使用以下代码,这将对您有所帮助

             TextFormField(
              keyboardType: TextInputType.name,
              autofocus: false,
              controller: textEditingController,
              decoration: InputDecoration(
                labelText: ' Company Website ',
                hintText: 'Enter the Company Website',
                prefixIcon: IconButton(
                  onPressed: () async {
                    if(textEditingController.text.toString() == null || textEditingController.text.toString() == ""){
                      print("null data");
                    }else{
                      print(textEditingController.text.toString());
                      if (await canLaunch("https://" + textEditingController.text.toString())) {
                        await launch("https://" + textEditingController.text.toString());
                      } else {
                        throw 'Could not launch ${textEditingController.text.toString()}';
                      }
                    }
                  },
                  icon: Icon(Icons.open_in_browser),
                ),
              ),
              maxLength: 15,
            ),,
于 2020-12-24T14:18:45.567 回答