How to create a hyperlink in Flutter widget?

后端 未结 7 1117
一个人的身影
一个人的身影 2020-12-02 10:47

I would like to create a hyperlink to display in my Flutter app.

The hyper link should be embedded in a Text or similar text views like:

相关标签:
7条回答
  • 2020-12-02 11:26

    You can wrap your Text in GestureDetector and handle click in onTap().

    GestureDetector(
      child: Text("Click here", style: TextStyle(decoration: TextDecoration.underline, color: Colors.blue)),
      onTap: () {
        // do what you need to do when "Click here" gets clicked
      }
    )
    

    0 讨论(0)
  • 2020-12-02 11:31

    If you want to make it look even more like a link, you can add underline:

    new Text("Hello Flutter!", style: new TextStyle(color: Colors.blue, decoration: TextDecoration.underline),)
    

    and the result:

    0 讨论(0)
  • 2020-12-02 11:31

    An alternative (or not) way to put clickable links in your app (for me it just worked that way):

    1 - Add the url_launcher package in your pubspec.yaml file

    (the package version 5.0 didn't work well for me, so I'm using the 4.2.0+3).

    dependencies:
      flutter:
        sdk: flutter
      url_launcher: ^4.2.0+3
    

    2 - Import it and use as below.

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() {
      runApp(MaterialApp(
        title: 'Navigation Basics',
        home: MyUrl(),
      ));
    }
    
    class MyUrl extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Url Launcher'),
          ),
          body: Center(
            child: FlatButton(
              onPressed: _launchURL,
              child: Text('Launch Google!',
                  style: TextStyle(fontSize: 17.0)),
            ),
          ),
        );
      }
    
      _launchURL() async {
        const url = 'https://google.com.br';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-02 11:32

    Flutter doesn't have built-in hyperlink support but you can fake it yourself. There's an example in the Gallery's drawer.dart. They use a RichText widget containing a coloured TextSpan, which has a recognizer attribute to handle taps:

            RichText(
              text: TextSpan(
                children: [
                  TextSpan(
                    style: bodyTextStyle,
                    text: seeSourceFirst,
                  ),
                  TextSpan(
                    style: bodyTextStyle.copyWith(
                      color: colorScheme.primary,
                    ),
                    text: repoText,
                    recognizer: TapGestureRecognizer()
                      ..onTap = () async {
                        final url = 'https://github.com/flutter/gallery/';
                        if (await canLaunch(url)) {
                          await launch(
                            url,
                            forceSafariVC: false,
                          );
                        }
                      },
                  ),
                  TextSpan(
                    style: bodyTextStyle,
                    text: seeSourceSecond,
                  ),
                ],
              ),
    

    0 讨论(0)
  • 2020-12-02 11:32

    You can use package flutter_linkify
    https://pub.dev/packages/flutter_linkify
    Just want to provide another option.
    Package will divide your text and highlight http/https automatically
    Combine plugin url_launcher you can launch url
    You can check example below:

    full code below

    import 'package:flutter/material.dart';
    import 'package:flutter_linkify/flutter_linkify.dart';
    import 'dart:async';
    
    import 'package:url_launcher/url_launcher.dart';
    
    void main() => runApp(new LinkifyExample());
    
    class LinkifyExample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'flutter_linkify example',
          home: Scaffold(
            appBar: AppBar(
              title: Text('flutter_linkify example'),
            ),
            body: Center(
              child: Linkify(
                onOpen: _onOpen,
                text: "Made by https://cretezy.com \n\nMail: example@gmail.com \n\n  this is test http://pub.dev/ ",
              ),
            ),
          ),
        );
      }
    
      Future<void> _onOpen(LinkableElement link) async {
        if (await canLaunch(link.url)) {
          await launch(link.url);
        } else {
          throw 'Could not launch $link';
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-02 11:43

    You can use Link Text https://pub.dev/packages/link_text and use it like

     final String _text = 'Lorem ipsum https://flutter.dev\nhttps://pub.dev'; 
     @override
     Widget build(BuildContext context) {
     return Scaffold(
         body: Center(
          child: LinkText(
            text: _text,
            textAlign: TextAlign.center,
          ),
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题