AspectRatio组件中不能包含Positioned组件

在Flutter中遇到 AspectRatio中组件不能包含Positioned 组件,Debug时报异常但界面显示正常,但Release版本直接不能显示。

以下是Android  Studio 端口显示的异常:

======== Exception caught by widgets library =======================================================

The following assertion was thrown while applying parent data.:

Incorrect use of ParentDataWidget.

The ParentDataWidget Positioned(right: 0.0, width: 151.4, height: 135.1) wants to apply ParentData of type StackParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.

Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. Typically, Positioned widgets are placed directly inside Stack widgets.

The offending Positioned is currently placed inside a AspectRatio widget.

The ownership chain for the RenderObject that received the incompatible parent data was:

  AspectRatio ← Positioned ← AspectRatio ← Stack ← ConstrainedBox ← Container ← Row ← Padding ← Container ← Listener ← ⋯

When the exception was thrown, this was the stack:

#0      RenderObjectElement._updateParentData.<anonymous closure> (package:flutter/src/widgets/framework.dart:5723:11)

#1      RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:5739:6)

#2      ParentDataElement._applyParentData.applyParentDataToChild (package:flutter/src/widgets/framework.dart:4955:15)

#3      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4628:14)

#4      ParentDataElement._applyParentData (package:flutter/src/widgets/framework.dart:4961:5)

...

====================================================================================================

查看代码发现:

Positioned 组件被包含在AspectRatio里面

查看源码AspectRatio


查看源码Positioned

AspectRatio中的child只能是Wiget,而Positioned类包含了left、right 、width等属性不是简单的Wiget的子类,所以不能把AspectRatio中的child中设置Positioned组件。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容