将 WidgetSpan 添加到 TextField 会导致 'dimensions != null': is not true'

Adding WidgetSpan to TextField results in 'dimensions != null': is not true'

我一直在尝试将 WidgetSpans 添加到 TextField(通过 TextEditingController 中的 buildTextSpan())并 运行 崩溃:“断言失败:[..]:'dimensions != null':是不对”。 在尝试找到解决方案时,我只 运行 遇到过类似问题的人,但未能找到解决方案。 我的代码(和问题)类似于在问题 https://github.com/flutter/flutter/issues/30688#issuecomment-870034058.

上发表的评论
  @override
  TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) {
    final atIndex = text.indexOf('@');
    var spans = <InlineSpan>[];

    if (atIndex != -1) {
      spans.add(TextSpan(text: text.substring(0, atIndex)));
      spans.add(
        WidgetSpan(
          alignment: PlaceholderAlignment.middle,
          child: Card(
            child: Padding(
              padding: const EdgeInsets.all(4.0),
              child: Text('@'),
            ),
          ),
        ),
      );
      spans.add(TextSpan(text: text.substring(1 + atIndex)));
    } else {
      spans.add(TextSpan(text: text));
    }

    return TextSpan(
      children: spans,
    );
  }

这个失败的断言是否有任何fix/workaround?

原来这个功能还没有在稳定版中。使用主频道“修复”了这个问题,无需修改即可向 运行 提供代码片段。

有关更改频道的信息,请参阅: