skip to Main Content

ListTile(
              leading: const Icon(
                Icons.link,
                color: Colors.black,
                size: 30,
              ),
              title: const Text(
                'Body Mass Index Calculator',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
              onTap: () {},
            ),

I am trying to make a link that opens website on browser directly.

2

Answers


  1. Using url_launcher you do easily.

    Here Example:

    import 'package:url_launcher/url_launcher.dart';
    
    //I assumed your link like below :
    final Uri _url = Uri.parse('https://flutter.dev');
    
          ListTile(
            leading: const Icon(
              Icons.link,
              color: Colors.black,
              size: 30,
            ),
            title: const Text(
              'Body Mass Index Calculator',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
              ),
            ),
            onTap: () async {
              if (!await launchUrl(_url)) {
                throw Exception('Could not launch $_url');
              }
            },
          )
    
    Login or Signup to reply.
  2. As of url_launcher: ^6.1.0 The plugin introduces support for webOnlyWindowName property and few Other new apis like launchUrl, For web support now you longer have to depend on dart:html You can simply declare the below function

    Future<void> launchLink(String url, {bool isNewTab = true}) async {
        await launchUrl(
            Uri.parse(url),
            webOnlyWindowName: isNewTab ? '_blank' : '_self',
        );
    }
    

    and use it like this

    onTap:(){
        launchLink('https://stackoverflow.com/questions/ask', isNewTab: true);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search