启动一个按钮(url),然后在 flutter 中打开浏览器

launch a button(url) and then open browser in flutter

我希望在单击 Sign Up 时打开浏览器并转到我的网站,但我不知道该怎么做。 这是我的代码,当我点击 Sign Up 时它不起作用:

    return Container(
      child: GestureDetector(
        onTap: () => launch("https://my.drclubs.ir/"),
        child: RichText(
          text: TextSpan(
            children: [
              TextSpan(
                text: "Don\`t have an Account?",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.w500),
              ),
              TextSpan(
                text: "Sign Up",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.bold),
              ),
            ],
          ),
        ),
      ),
    );
  }

您可以使用名为 url_launcher here.

的包

示例:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

const _url = 'https://flutter.dev'; /// Put your custom url here.

void main() => runApp(
      const MaterialApp(
        home: Material(
          child: Center(
            child: RaisedButton(
              onPressed: _launchURL,
              child: Text('Show Flutter homepage'),
            ),
          ),
        ),
      ),
    );

void _launchURL() async =>
    await canLaunch(_url) ? await launch(_url) : throw 'Could not launch $_url';

你可以试试这个:

return Container(
      child: GestureDetector(
        onTap: _launchURL,
        child: RichText(
          text: TextSpan(
            children: [
              TextSpan(
                text: "Don\`t have an Account?",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.w500),
              ),
              TextSpan(
                text: "Sign Up",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.bold),
              ),
            ],
          ),
        ),
      ),
    );