Ray Turner
Ray Turner

Reputation: 21

I would like to open a URL in a WebView from within a TextSpan

Here is My Code.

Center(
  child: SingleChildScrollView(
    child: Container(
      child: RichText(
        text: TextSpan(
          //style: DefaultTextStyle.of(context).style,
          children: [
            TextSpan(
              text: 'URL IS IN HERE.COM!\n',  
              style: TextStyle(letterSpacing: 1, fontSize: 26.0, fontWeight: FontWeight.bold, color: Colors.black)
            ),
            ...

I would like to tap the text string ('URL IS IN HERE.COM!) and for it to open in my webview widget?

Upvotes: 0

Views: 932

Answers (2)

Ray Turner
Ray Turner

Reputation: 21

I have worked it out after a lot of reading and research, This was the best solution Using a URL Scheme.

               onTap: () async {
                    const url = "https:www.example.com";
                  if (await canLaunch(url)) {
                      await launch(url, forceWebView: true);
                } else {
                      throw "Could not launch $url";
                  }
                },

Upvotes: 0

Faiq Mustaqeem
Faiq Mustaqeem

Reputation: 96

you can use recognizer property of textspan and for open url in browser you can use this https://pub.dev/packages/url_launcher

Widget

TextSpan(text: 'Non touchable. ', children: [
  new TextSpan(
    text: 'Tap here.',
    recognizer: new TapGestureRecognizer()..onTap = () => {
      _launchURL();
    },
  )
])

Method

_launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Upvotes: 0

Related Questions