Flutter报错Incorrect use of ParentDataWidget,列表中点赞按钮不显示
问题根因
- 错误触发原因完全符合报错提示:
Flexible组件只能直接放在Row、Column、Flex这类弹性布局容器内部,你的代码中renderBtnLike方法里,将Flexible作为了带padding的Container的直接子组件,Container属于普通盒子容器,不支持接收FlexParentData,因此触发了类型不兼容的错误。
修复方案
直接删除renderBtnLike方法中包裹Row的多余Flexible层即可,修改后的核心代码如下:
Widget renderBtnLike() { return Container( child: GestureDetector( onTapDown: onTapDownBtn, onTapUp: onTapUpBtn, onTap: onTapBtn, child: Container( // 已删除外层多余的Flexible组件 child: Row( children: <Widget>[ // Icon like Expanded( child: Transform.scale( child: Transform.rotate( child: Image.asset( getImageIconBtn(), width: 25.0, height: 25.0, fit: BoxFit.contain, color: getTintColorIconBtn(), ), angle: !isLongPress ? handleOutputRangeTiltIconLike(tiltIconLikeInBtn2.value) : tiltIconLikeInBtn.value, ), scale: !isLongPress ? handleOutputRangeZoomInIconLike(zoomIconLikeInBtn2.value) : zoomIconLikeInBtn.value, ), ), // Text like Expanded( child: Transform.scale( child: Text( getTextBtn(), style: TextStyle( color: getColorTextBtn(), fontSize: 14.0, fontWeight: FontWeight.bold, ), ), scale: !isLongPress ? handleOutputRangeZoomInIconLike(zoomIconLikeInBtn2.value) : zoomTextLikeInBtn.value, ), ), ], mainAxisAlignment: MainAxisAlignment.spaceEvenly, ), padding: EdgeInsets.all(10.0), color: Colors.transparent, ), ), width: 100.0, decoration: BoxDecoration( borderRadius: BorderRadius.circular(4.0), color: Colors.white, border: Border.all(color: getColorBorderBtn()), ), margin: EdgeInsets.only(top: 190.0), ); }
原理说明
- 你外层的
Container已经固定了宽度为100,删除Flexible后,Row会默认撑满父容器的宽度,内部的两个Expanded父容器是Row(属于弹性布局容器),完全符合使用规范,可以正常按比例分配宽度,不会影响原有布局效果。
内容的提问来源于stack exchange,提问作者Islem Talbi
相关产品推荐
相关产品推荐

