首页 > 解决方案 > 为什么 TapGestureRecognizer 在 ios 上不起作用?

问题描述

我正在尝试在我的应用程序中创建一个超链接,它在 android 上运行良好,但是当我在 IOS 上对其进行测试时,它会抛出附加的错误。

我的代码:

                                TextSpan(
                                style: TextStyle(
                                  color: Colors.blue,
                                  decoration: TextDecoration.underline,
                                ),
                                text: 'Hyperlink',
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () async {
                                    var url =
                                        "https://www.flutter.dev/";
                                    if (await canLaunch(url)) {
                                      await launch(url);
                                    } else {
                                      throw "Cannot load Url";
                                    }
                                  }),

错误:

======== Exception caught by scheduler library =====================================================
The following RangeError was thrown during a scheduler callback:
RangeError (index): Invalid value: Valid value range is empty: 0

When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:177:60)
#1      List.elementAt (dart:core-patch/growable_array.dart:386:16)
#2      RenderParagraph.assembleSemanticsNode (package:flutter/src/rendering/paragraph.dart:921:50)
#3      _SwitchableSemanticsFragment.compileChildren (package:flutter/src/rendering/object.dart:3717:13)
#4      _SwitchableSemanticsFragment.compileChildren (package:flutter/src/rendering/object.dart:3709:16)
...
====================================================================================================

根据要求,这是整个片段。我正在尝试实现一个 Listtile,它在按下时会打开一个可滚动的对话框。Únfortunately this error 当按下 Listtile 时发生

              ListTile(
            dense: true,
            leading: Icon(
              Icons.help,
              color: Colors.black,
            ),
            title: Text("contact"),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      elevation: 20,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(15.0),
                      ),
                      content: Container(
                        width: double.maxFinite,
                        height: 130,
                        child: ListView(children: <Widget>[
            
                          
                          RichText(
                              text: TextSpan(children: [
                            TextSpan(
                                text: 'Feedback:',
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: "\n",
                                style: TextStyle(color: Colors.black)),
                            WidgetSpan(
                                child: Icon(
                              Icons.email,
                              size: 15,
                            )),
                            TextSpan(
                                text:
                                    '  John.Doe@email.com' + "\n",
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: "\n",
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: 'Updates:',
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: "\n",
                                style: TextStyle(color: Colors.black)),
                            WidgetSpan(
                                child: Icon(
                              Icons.link,
                              size: 15,
                            )),
                            TextSpan(
                                text: '  ',
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                style: TextStyle(
                                  color: Colors.blue,
                                  decoration: TextDecoration.underline,
                                ),
                                text: 'Instagram' + "\n",
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () async {
                                    var url =
                                        "https://www.instagram.com/";
                                    if (await canLaunch(url)) {
                                      await launch(url);
                                    } else {
                                      throw "Cannot load Url";
                                    }
                                  }),
                            TextSpan(
                                text: "\n",
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: 'Support:',
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                text: "\n",
                                style: TextStyle(color: Colors.black)),
                            WidgetSpan(
                                child: Icon(
                              Icons.payment,
                              size: 15,
                            )),
                            TextSpan(
                                text: '  ',
                                style: TextStyle(color: Colors.black)),
                            TextSpan(
                                style: TextStyle(
                                  color: Colors.blue,
                                  decoration: TextDecoration.underline,
                                ),
                                text: 'Paypal',
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () async {
                                    var url =
                                        "https://www.paypal.me/";
                                    if (await canLaunch(url)) {
                                      await launch(url);
                                    } else {
                                      throw "Cannot load Url";
                                    }
                                  }),
                          ]))
                        ]),
                      ),
                    );
                  });
            },
          ),

这个片段有什么问题,为什么它可以在 android 上运行,但不能在 ios 上运行?

标签: flutter

解决方案


这是一个颤振错误:https ://github.com/flutter/flutter/issues/51936

你可以在这里看到问题:https ://github.com/flutter/flutter/issues/70159

我在我的一个应用程序中使用此代码:

RichText(
  textAlign: TextAlign.left,
  text: TextSpan(
    text: 'i accept',
    children: <TextSpan>[
      TextSpan(
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              Navigator.pushNamed(context, 'terms');
            },
          text: 'Terms & conditions'),
    ],
  ),
)

我正在使用 Flutter 1.22.2,通道稳定,Dart 2.10.2


推荐阅读