在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组件。