【发布时间】:2019-12-05 18:44:00
【问题描述】:
我在 Flutter 中构建了一个应用程序,我的情况是,我得到了带有许多 TextSpan 小部件的 RichText 小部件,我需要有两个手势识别器,一个是双击,另一个是长按,所以我该怎么做如果可能,这样做?
【问题讨论】:
标签: flutter dart flutter-layout
我在 Flutter 中构建了一个应用程序,我的情况是,我得到了带有许多 TextSpan 小部件的 RichText 小部件,我需要有两个手势识别器,一个是双击,另一个是长按,所以我该怎么做如果可能,这样做?
【问题讨论】:
标签: flutter dart flutter-layout
每个textSpan 都带有自己的text 和children 属性,您可以使用recognizer 属性并根据需要实现不同的点击。
考虑下面的例子:
Container(
color: Colors.black,
padding: EdgeInsets.all(10),
child: Center(
child: RichText(
text: TextSpan( // <-- 1
text: 'This is a text from first textspan. ',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
fontWeight: FontWeight.bold),
children: <TextSpan>[ // <-- 2
TextSpan(
text: ' This is a text from second textspan ',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold),
recognizer: LongPressGestureRecognizer()
..onLongPress = () {
print('Long pressed');
},
children: <
TextSpan>[ // <-- 3 (children of 2 textspan
TextSpan(
text: ' This is a another text from second textspan',
recognizer: DoubleTapGestureRecognizer()
..onDoubleTap = () {
print('double tapped');
}
)
]
),
]
),
)
)
)
注释为2 的children: <TextSpan>[] 有一个text 属性和对应的recognizer,其中我使用了LongPressGestureRecognizer()。相同的textSpan (2) 具有children 属性,该属性再次可以具有带有文本的子文本跨度和对应的recognizer,其中我使用了DoubleTapGestureRecognizer()。
所以输出将是:你可以长按This is a text from second textspan,你可以双击This is another text from second textspan。
希望这能回答你的问题。
【讨论】:
您不能将整个 Text Span 包装在手势检测器小部件中吗? https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
【讨论】:
TextSpan 要两个recognizers 吗?