我想让文本的一部分可点击,这样我就可以调用它的函数。我还想控制可点击文本的样式。在最好的情况下,我还可以将可点击区域的大小增加到例如 42px。
我已经研究过 flutter_linkify 和 linkify,但这不是我想要的。我很好奇是否已经有一个包或者甚至内置到 flutter 库中。
import 'package:flutter/gestures.dart';
将 RichText 与 TextSpan 和 GestureRecognizer 一起使用。使用
GestureRecognizer
,您可以检测点击,双击,长按等
Widget build(BuildContext context) {
TextStyle defaultStyle = TextStyle(color: Colors.grey, fontSize: 20.0);
TextStyle linkStyle = TextStyle(color: Colors.blue);
return RichText(
text: TextSpan(
style: defaultStyle,
children: <TextSpan>[
TextSpan(text: 'By clicking Sign Up, you agree to our '),
TextSpan(
text: 'Terms of Service',
style: linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () {
print('Terms of Service"');
}),
TextSpan(text: ' and that you have read our '),
TextSpan(
text: 'Privacy Policy',
style: linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () {
print('Privacy Policy"');
}),
],
),
);
}
RichText
将 TextSpan
列表合并到单个文本中。
return RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(
text: 'world!',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(
text: ' click here!',
recognizer: TapGestureRecognizer()
..onTap = () => print('click')),
],
),
);