【问题标题】:Flutter "RangeError (index): Invalid value: Valid value range is empty: 0 with WidgetSpanFlutter“RangeError(索引):无效值:有效值范围为空:0与WidgetSpan
【发布时间】:2020-10-30 16:01:55
【问题描述】:

我正在尝试在 Flutter 中创建下面的图片。如您所见,我需要一个链接和一个图像图标。但是,我在控制台中收到“范围”错误。

这是我的代码的 sn-p

Widget build(BuildContext context) {
return Row(
  mainAxisAlignment: MainAxisAlignment.start,
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
      Image.asset(
        "assets/images/checkbox-check.png",
      ),
    Expanded(
      child: Container(
        child: Wrap(
          children: [
            RichText(
              text: new TextSpan(
                children: [
                  TextSpan(
                    text: 'Email opportunities.  ',
                    style: TextStyle(
                        color: Palette.DARK_BLUE, fontSize: 16.0),
                  ),
                  TextSpan(
                    text: 'privacy policy',
                    style: TextStyle(
                        color: Palette.DARK_BLUE,
                        decoration: TextDecoration.underline),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        launch();
                      },
                  ),
                  TextSpan(
                    text: ' and ',
                    
                  ),
                  TextSpan(
                    text: 'terms of use',
                    style: TextStyle(
                        decoration: TextDecoration.underline),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        launch();
                      },
                  ),
                  WidgetSpan(
                    child: Icon(
                      Icons.info_outline,
                      color: Colors.red,
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    )
  ],
);

} }

还有其他方法可以创建我的用户界面吗?我没有收到错误屏幕,但我在控制台中看到了错误。我在小部件树中有一个包装,但认为我不需要它。但如果我删除它,同样的问题也不会少。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    当您将 TextSpan 与识别器与 WidgetSpan 一起使用时,这是一个 Flutter 问题:https://github.com/flutter/flutter/issues/51936

    一种解决方法是将RichText 包装在ExcludeSemantics 中:

    ExcludeSemantics(
      excluding: true,
      child: RichText( ... ),
    ),
    

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2021-11-13
      • 1970-01-01
      • 2021-06-16
      • 2021-07-31
      • 2019-07-25
      • 2021-02-23
      • 2020-11-01
      • 2020-10-07
      相关资源
      最近更新 更多