跳至主要内容

忽略 IgnorePointer 及相关类中 ignoringSemantics 的迁移指南

摘要

#

IgnoringPointer 小部件允许您指定 UI 中某个区域,您不希望在该区域接受指针事件,例如,当您不希望用户在文本字段中输入文本时。

以前,IgnorePointer 不仅阻止指针事件,还会将其子树从语义树中删除。ignoreSemantics 参数作为一种变通方法引入,以便在使用 IgnorePointer 时保留语义树。

IgnorePointer 的行为已更改,它不再删除整个语义子树,而只是阻止子树中的语义操作。ignoringSemantics 变通方法不再需要,并且已弃用。

此更改也适用于 AbsorbPointerSliverIgnorePointer 小部件。

更改说明

#

ignoringSemantics 已移除。

迁移指南

#

如果您在这些小部件中将此参数设置为 true,请考虑改用 ExcludeSemantics

迁移前代码

dart
IgnorePointer(
  ignoringSemantics: true,
  child: const PlaceHolder(),
);

AbsorbPointer(
  ignoringSemantics: true,
  child: const PlaceHolder(),
);

SliverIgnorePointer(
  ignoringSemantics: true,
  child: const PlaceHolder(),
);

迁移后代码

dart
ExcludeSemantics(
  child: IgnorePointer(
    child: const PlaceHolder(),
  ),
);

ExcludeSemantics(
  child: AbsorbPointer(
    child: const PlaceHolder(),
  ),
);

SliverIgnorePointer(
  child: ExcludeSemantics(
    child: const PlaceHolder(),
  ),
);

如果您之前使用 IgnorePointer 并将 ignoringSemantics 设置为 false,则可以通过将以下小部件直接复制到您的代码中并使用来实现相同的效果。

dart
/// A widget ignores pointer events without modifying the semantics tree.
class _IgnorePointerWithSemantics extends SingleChildRenderObjectWidget {
  const _IgnorePointerWithSemantics({
    super.child,
  });

  @override
  _RenderIgnorePointerWithSemantics createRenderObject(BuildContext context) {
    return _RenderIgnorePointerWithSemantics();
  }
}

class _RenderIgnorePointerWithSemantics extends RenderProxyBox {
  _RenderIgnorePointerWithSemantics();

  @override
  bool hitTest(BoxHitTestResult result, { required Offset position }) => false;
}

/// A widget absorbs pointer events without modifying the semantics tree.
class _AbsorbPointerWithSemantics extends SingleChildRenderObjectWidget {
  const _AbsorbPointerWithSemantics({
    super.child,
  });

  @override
  _RenderAbsorbPointerWithSemantics createRenderObject(BuildContext context) {
    return _RenderAbsorbPointerWithSemantics();
  }
}

class _RenderAbsorbPointerWithSemantics extends RenderProxyBox {
  _RenderAbsorbPointerWithSemantics();

  @override
  bool hitTest(BoxHitTestResult result, { required Offset position }) {
    return size.contains(position);
  }
}

/// A sliver ignores pointer events without modifying the semantics tree.
class _SliverIgnorePointerWithSemantics extends SingleChildRenderObjectWidget {
  const _SliverIgnorePointerWithSemantics({
    super.child,
  });

  @override
  _RenderSliverIgnorePointerWithSemantics createRenderObject(BuildContext context) {
    return _RenderSliverIgnorePointerWithSemantics();
  }
}

class _RenderSliverIgnorePointerWithSemantics extends RenderProxySliver {
  _RenderSliverIgnorePointerWithSemantics();

  @override
  bool hitTest(BoxHitTestResult result, { required Offset position }) => false;
}

时间轴

#

包含于版本:3.10.0-2.0.pre
稳定版发布:3.13.0

参考

#

相关 PR

  • PR 120619:修复 IgnorePointer 和 AbsorbPointer 以仅阻止 a11y 中的用户交互。