【发布时间】:2019-12-06 03:18:38
【问题描述】:
通过 PopupMenu 中的按钮打开 URL,这可能吗?我在网上搜索了很多,但我一无所获。我做了类似的事情,但没有成功。我不明白如何在“PopupMenuButton”的“choiceAction”中使用“ulr_launcher”中的“超链接”,我尝试使用“NewScaffold”来放置“body”和“child”,但出现编译错误。这是我的 main.dart
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',);
}
}
hyperlink.dart
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class Hyperlink extends StatelessWidget {
final String _url;
final String _text;
Hyperlink(this._url, this._text);
_launchURL() async {
if (await canLaunch(_url)) {
await launch(_url);
} else {
throw 'Could not launch $_url';
}
}
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
alignment: Alignment.topRight,
child: Text(
_text,
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
)),
onTap: _launchURL,
);
}
}
常量.dart
class Constants{
static const String WebSite = 'WebSite';
static const List<String> choices = <String>[
WebSite
];
}
【问题讨论】:
标签: android flutter dart flutter-layout