【问题标题】:Flutter PopupMenu button open URLFlutter PopupMenu 按钮打开 URL
【发布时间】: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


    【解决方案1】:

    这个链接会帮助你!

    https://dev.to/guimg/hyperlink-widget-on-flutter-4fa5

    创建两个字符串列表,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';
            }
          }
    

    【讨论】:

    • 启动方法和两个字符串要加到hyperlink.dart中?以及对 main.dart 的操作?因为我不懂
    • 确认一件事,N 个网站名称和 N 个 URL ???还是 1 个网站名称和 N 个 URL??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2022-10-02
    • 2018-09-16
    • 1970-01-01
    • 2020-12-15
    • 2020-05-02
    相关资源
    最近更新 更多