IgnorePointer 和相關類別中忽略語義 (ignoringSemantics) 的遷移指南
摘要
#IgnoringPointer
widget 允許您指定 UI 的一個區域,您不希望該區域接受指標事件,例如,當您不希望允許使用者在文字欄位中輸入文字時。
先前,IgnorePointer
不僅會阻止指標事件,還會從語義樹中刪除其子樹。引入 ignoreSemantics
參數是作為在使用 IgnorePointer
時保留語義樹的替代方案。
IgnorePointer
的行為已變更,它不再刪除整個語義子樹,而僅阻止子樹中的語義動作。不再需要 ignoringSemantics
替代方案,並且已棄用。
此變更也適用於 AbsorbPointer
和 SliverIgnorePointer
widget。
變更說明
#ignoringSemantics
已移除。
遷移指南
#如果您在這些 widget 中將此參數設定為 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
,您可以將以下 widget 直接複製到您的程式碼中使用,以實現相同的行為。
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 中的使用者互動。
除非另有說明,否則本網站上的文件反映了 Flutter 的最新穩定版本。頁面最後更新時間:2024-04-04。 檢視原始碼 或 回報問題。