第一步:在pubspec.yaml 文件中 导入
url_launcher: ^3.0.3
在lib main.drat中导入包
import 'package:url_launcher/url_launcher.dart'
import 'dart:io';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '富文本组件',
home: Scaffold(
body: RichTextDemo(),
),
);
}
}
class RichTextDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Center(
child: RichText(
text: TextSpan(
text: 'I',
style: TextStyle(
fontSize: 38.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
children: <TextSpan>[
TextSpan(
text: 'want',
style: TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
color: Colors.red)),
TextSpan(
text: 'study',
style: TextStyle(
fontSize: 32.0,
fontStyle: FontStyle.italic,
color: Colors.blue)),
TextSpan(
text: 'Flutter',
style: TextStyle(
fontSize: 32.0,
fontStyle: FontStyle.italic,
color: Colors.yellow),
recognizer: TapGestureRecognizer()
..onTap = () {
_lancurl();
}),
]),
),
);
}
// 点击文字跳转到百度
_lancurl() async {
const url = 'http://www.baidu.com';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}