【问题标题】:Hyperlinking an image to an URL in Flutter在 Flutter 中将图像超链接到 URL
【发布时间】:2026-01-28 09:10:02
【问题描述】:

我有一张图片,点击后应重定向到网页并在浏览器中打开。

这是我用于图像的代码,我有两张图像,一张是背景,第二张是我需要超链接 URL 的地方。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("About the app"),
      ),
      body: new Container(
        decoration: new BoxDecoration(
          image: new DecorationImage(
            image: new AssetImage('images/upgradedbg.png'), // Background Image
            fit: BoxFit.cover,
          ),
        ),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(8.0, 110.0, 8.0, 20.0),
          child: ListView(
            children: <Widget>[
              Image.asset(
                'images/madeby.png', // On click should redirect to an URL
                width: 400.0,
                height: 180.0,
                fit: BoxFit.cover,
              )
            ],
          ),
        ),
      ),
    );
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    使用this plugin:

      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("About the app"),
          ),
          body: new Container(
            decoration: new BoxDecoration(
              image: new DecorationImage(
                image: new AssetImage('images/upgradedbg.png'), // Background Image
                fit: BoxFit.cover,
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.fromLTRB(8.0, 110.0, 8.0, 20.0),
              child: ListView(
                children: <Widget>[
                  GestureDetector(
                    onTap: _launchURL,
                    child: Image.asset(
                      'images/madeby.png', // On click should redirect to an URL
                      width: 400.0,
                      height: 180.0,
                      fit: BoxFit.cover,
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      }
      _launchURL() async {
        const url = 'https://flutter.io';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    

    【讨论】:

    • 完美地完成了这项工作。谢谢!
    最近更新 更多