Flutter中更多用于TextSpan的GestureRecognisers

时间:2019-07-27 22:58:10

标签: flutter dart flutter-layout

我在Flutter中构建了一个应用程序,并且我遇到了带有很多TextSpan小部件的RichText小部件,并且我需要有两个手势识别器,一个是双击的,另一个是长按的,所以我该怎么办?可能的话这样做吗?

2 个答案:

答案 0 :(得分:0)

您是否可以将整个文本跨度包装在手势检测器小部件中? https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

答案 1 :(得分:0)

每个textSpan都有自己的textchildren属性,您可以使用它们的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');
                                              }
                                        )
                                      ]
                                  ),
                                ]
                            ),
                          )
                      )
                  )

被注释为children: <TextSpan>[]的{​​{1}}具有2属性和相应的text,其中我使用了recognizer。相同的LongPressGestureRecognizer()textSpan)具有2属性,该属性又可以具有带有文本的子文本范围,并且在其中使用了children的相应recognizer

因此输出将是:您可以长按DoubleTapGestureRecognizer(),也可以双击This is a text from second textspan

希望这能回答您的问题。