Android 颤振弹出菜单按钮打开URL

Android 颤振弹出菜单按钮打开URL,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,通过弹出菜单中的按钮打开URL,可以吗?我在网上搜索了很多,但没有找到任何相关信息。我做了那样的事,但没有成功。我不知道如何在“PopupMenuButton”中的“choiceAction”中使用“ulr_launcher”中的“Hyperlink”,我尝试使用“NewScaffold”来放置“body”和“child”,但出现编译错误。这是我的主飞镖 import 'package:flutter/material.dart'; import 'package:testprj/Constan

通过弹出菜单中的按钮打开URL,可以吗?我在网上搜索了很多,但没有找到任何相关信息。我做了那样的事,但没有成功。我不知道如何在“PopupMenuButton”中的“choiceAction”中使用“ulr_launcher”中的“Hyperlink”,我尝试使用“NewScaffold”来放置“body”和“child”,但出现编译错误。这是我的主飞镖

import 'package:flutter/material.dart';
import 'package:testprj/Constants.dart';
import 'package:testprj/hyperlink.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
        actions: <Widget>[
          PopupMenuButton<String>(
            onSelected: choiceAction,
            itemBuilder: (BuildContext context){
              return Constants.choices.map((String choice){
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          )
        ],
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'Body',
            ),
          ],
        ),
      ),
    );
  }

  void choiceAction(String choice){
    Hyperlink('www.test.com', 'sito web',);
  }
}
飞镖

class Constants{
  static const String WebSite = 'WebSite';

  static const List<String> choices = <String>[
    WebSite
  ];
}
类常量{
静态常量字符串WebSite='WebSite';
静态常量列表选项=[
网站
];
}

此链接将帮助您

创建两个字符串列表,1个网站名称/可见名称和2个网站url

List<String> websiteNames =[];
List<String> webUrls =[];

//in the action bar

actions: <Widget>[
          PopupMenuButton<String>(
            onSelected: launchUrl,
            itemBuilder: (BuildContext context){
              return websiteNames.map((String choice){
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          )
        ],

//launch method

    launchURL(website) async {
        String url = webUrls[webUrls.indexOf(website)];
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
列出网站名=[];
列出WebURL=[];
//在操作栏中
行动:[
弹出菜单按钮(
onSelected:launchUrl,
itemBuilder:(构建上下文){
返回websiteNames.map((字符串选择){
返回PopupMenuItem(
价值:选择,
儿童:文本(选择),
);
}).toList();
},
)
],
//发射方法
启动URL(网站)异步{
字符串url=WebURL[webURL.indexOf(网站)];
如果(等待canLaunch(url)){
等待发射(url);
}否则{
抛出“无法启动$url”;
}
}

要将启动方法和两个字符串添加到hyperlink.dart中吗?还有main.dart上的动作?因为我不明白,请确认一件事,N个网站名和N个URL???或1个网站名称和N个URL??
List<String> websiteNames =[];
List<String> webUrls =[];

//in the action bar

actions: <Widget>[
          PopupMenuButton<String>(
            onSelected: launchUrl,
            itemBuilder: (BuildContext context){
              return websiteNames.map((String choice){
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          )
        ],

//launch method

    launchURL(website) async {
        String url = webUrls[webUrls.indexOf(website)];
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }