我需要在 RichText 中启用文本选择。当用户单击特定的 TextSpan 时,我还需要调用一些代码。
我厌倦了在代码下面运行。但我注意到onTap
它只适用于_nonSelectableRichText()
,而不适用于_selectableRichText
。
我不知道这是否是一个错误......但我想问是否有人有结合这两个功能的解决方案?
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Builder(
builder: (context) => Container(
child: _nonSelectionRichText(context),
//child: _selectionRichText(context),
),
),
),
);
}
Widget _nonSelectableRichText(BuildContext context) {
return RichText(
text: TextSpan(
children: [
TextSpan(
text: "Welcome to ",
style: TextStyle(color: Colors.black, fontSize: 70)),
TextSpan(
text: "Flutter",
style: TextStyle(color: Colors.black, fontSize: 70),
recognizer: TapGestureRecognizer()
..onTap = () {
print("Flutter"); // will work here
}),
],
),
);
}
Widget _selectableRichText(BuildContext context) {
return SelectableText.rich(
TextSpan(
children: [
TextSpan(
text: "Welcome to ",
style: TextStyle(color: Colors.black, fontSize: 70)),
TextSpan(
text: "Flutter",
style: TextStyle(color: Colors.black, fontSize: 70),
recognizer: TapGestureRecognizer()
..onTap = () {
print("Flutter"); // will not work here
}),
],
),
);
}
}