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

Time:10-07

I want when the Sign Up is clicked, the browser be open and go to my website but i dont know how to do that. here is my code, when i tap on Sign Up it doesn't work:

    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),
              ),
            ],
          ),
        ),
      ),
    );
  }

CodePudding user response:

You can use a package called url_launcher here.

Example:

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';

You can try this:

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),
              ),
            ],
          ),
        ),
      ),
    );
  • Related