Me gustaría crear un hipervínculo para mostrar en mi aplicación Flutter.
El hipervínculo debe estar incrustado en una Textvista de texto o similar como:
The last book bought is <a href='#'>this</a>
¿Alguna pista para hacer esto?
Respuestas:
Simplemente envuelva un InkWell alrededor de un widget de texto y proporcione un UrlLauncher (de la biblioteca de servicios) al atributo onTap. Instale UrlLauncher como un paquete de Flutter antes de usarlo a continuación.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('UrlLauchner'),
),
body: new Center(
child: new InkWell(
child: new Text('Open Browser'),
onTap: () => launch('https://docs.flutter.io/flutter/services/UrlLauncher-class.html')
),
),
),
);
}
}
Puede proporcionar un estilo al widget Texto para que parezca un enlace.
Después de investigar un poco el problema, encontré una solución diferente para implementar los hipervínculos 'en línea' que solicitó. Puede utilizar el widget RichText con TextSpans adjuntos .
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('UrlLauchner'),
),
body: new Center(
child: new RichText(
text: new TextSpan(
children: [
new TextSpan(
text: 'This is no Link, ',
style: new TextStyle(color: Colors.black),
),
new TextSpan(
text: 'but this is',
style: new TextStyle(color: Colors.blue),
recognizer: new TapGestureRecognizer()
..onTap = () { launch('https://docs.flutter.io/flutter/services/UrlLauncher-class.html');
},
),
],
),
),
),
),
);
}
}
De esta manera, puede resaltar una palabra y hacer un hipervínculo con ella;)
TapGestureRecognizercorrectamente el ciclo de vida de su . Tienes que llamar al dispose()método cuando RichTextya no se usa. Ver aquí: api.flutter.dev/flutter/painting/TextSpan/recognizer.html
StatelessWidgetno lo desechará mágicamente TapGestureRecognizerpor usted. De hecho, usar StatelessWidgeten este escenario es incorrecto, ya que no puede deshacerse de sus recursos de esta manera. Y sí, es absolutamente necesario llamar al dispose()método de TapGestureRecognizer, ya que ejecuta el temporizador interno que debe detenerse.
Flutter no tiene soporte de hipervínculo incorporado, pero puedes fingirlo tú mismo. Hay un ejemplo en drawer.dart de la Galería . Usan un RichTextwidget que contiene un color TextSpan, que tiene un recognizeratributo para manejar toques:
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,
),
],
),
InkWelllugar de GestureDetector.
Puede usar el paquete flutter_linkify
https://pub.dev/packages/flutter_linkify
Solo quiero proporcionar otra opción.
El paquete dividirá su texto y resaltará http / https automáticamente.Combine el
plugin url_launcher, puede iniciar la url
.
código completo a continuación
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';
}
}
}
Una forma alternativa (o no) de colocar enlaces en los que se puede hacer clic en su aplicación (para mí simplemente funcionó de esa manera):
1 - Agregue el paquete url_launcher en su archivo pubspec.yaml
(La versión del paquete 5.0 no funcionó bien para mí, así que estoy usando 4.2.0 + 3).
dependencies:
flutter:
sdk: flutter
url_launcher: ^4.2.0+3
2 - Importarlo y usarlo como se muestra a continuación.
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';
}
}
}
FlatButtoncon el mismo fondo y colores de texto que el resto de sus textos, así que formatee con TextDecoration.underline como el bartektartanus mostrado arriba ...
Puede usar Link Text https://pub.dev/packages/link_text y usarlo como
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,
),
),
);
}
flutter pub getporque falla conUnable to find a plugin.vcxproj for plugin "url_launcher_windows"