【问题标题】:More GestureRecognisers For The TextSpan In FlutterFlutter 中用于 TextSpan 的更多 GestureRecognizers
【发布时间】:2019-12-05 18:44:00
【问题描述】:

我在 Flutter 中构建了一个应用程序,我的情况是,我得到了带有许多 TextSpan 小部件的 RichText 小部件,我需要有两个手势识别器,一个是双击,另一个是长按,所以我该怎么做如果可能,这样做?

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    每个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');
                                                  }
                                            )
                                          ]
                                      ),
                                    ]
                                ),
                              )
                          )
                      )
    

    注释为2children: &lt;TextSpan&gt;[] 有一个text 属性和对应的recognizer,其中我使用了LongPressGestureRecognizer()。相同的textSpan (2) 具有children 属性,该属性再次可以具有带有文本的子文本跨度和对应的recognizer,其中我使用了DoubleTapGestureRecognizer()

    所以输出将是:你可以长按This is a text from second textspan,你可以双击This is another text from second textspan

    希望这能回答你的问题。

    【讨论】:

      【解决方案2】:

      您不能将整个 Text Span 包装在手势检测器小部件中吗? https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

      【讨论】:

      • 不,因为 RichText 的子列表需要 TextSpan 类型的小部件,所以我不能将 GestureDetector 放在 TextSpan 上方,因为它不会接受它
      • 每个TextSpan 要两个recognizers 吗?
      • @DK15 是的,这就是我想要的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多