How do i automatically convert addresses into clickable links in flutter?

193 Views Asked by At

In Android we have https://developer.android.com/reference/android/text/util/Linkify

and https://developer.android.com/reference/android/widget/TextView#attr_android:autoLink

so for example if you SMS someone an address properly formatted with city state it lets you pull up google maps when you tap on it.

How do you achieve this in flutter? The packages I find either support only url, or emamil or phone number maybe but not address.

https://pub.dev/packages/selectable_autolink_text etc

2

There are 2 best solutions below

1
Hoa Le On

Assume you have an address:

var address = '1 Maritime Square, Singapore 099253';

You build a Google Map URL to launch:

var googleMapslocationUrl =
        "https://www.google.com/maps/search/?api=1&query=$address"; 

Build an Inkwell widget for user:

InkWell(
              onTap: () => launchUrl(Uri.parse(googleMapslocationUrl)),
              child: Text(address,
                  style: const TextStyle(
                    color: Colors.blue,
                    decoration: TextDecoration.underline,
                  )),
            ),

The launchUrl function is from the url_launcher package: https://pub.dev/packages/url_launcher
Hope this help.

0
MohitJadav On
import 'package:flutter/gestures.dart';
...

new RichText(
      text: new TextSpan(text: 'Non touchable. ', children: [
        new TextSpan(
          text: 'Tap here.',
          recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
        )
      ]),
    );

You can use RichText for Linkredirects for like Terms&condition etc.