我当时正在做我的项目,在使用一个灵活的小部件时遇到了这个错误。看看下面的代码。我试着把这个绿色的小盒子做得灵活些,并根据它的需要来调整尺寸。但是它抛出了一个错误“不正确地使用ParentDataWidget”。我认为问题在于我正在使用的灵活小部件。我不确定能不能帮帮我。这是我的密码。
产品卡Widget.dart
Padding(
padding: const EdgeInsets.only(
top: 10,
left: 5,
),
child: Flexible(
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: product.type == 'Veg'
? Colors.green.withOpacity(0.8)
: Colors.red.withOpacity(0.8),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
product.type,
style: GoogleFonts.acme(
color: Colors.white,
),
),
),
),
),
),误差
应用父数据时引发以下断言。:不正确地使用ParentDataWidget。
ParentDataWidget灵活(flex: 1)希望将类型为FlexParentData的ParentData应用于RenderObject,该RenderObject已被设置为接受不兼容类型的BoxParentData的ParentData。
通常,这意味着灵活的小部件有错误的祖先RenderObjectWidget。通常,灵活的小部件直接放置在Flex小部件中。违规的灵活性目前放置在一个填充小部件中。
发布于 2022-07-20 17:28:55
错误在于您在错误的父级中使用了一个灵活的小部件。提供灵活的小部件将为它提供它所能拥有的最大维度。如果您想要使您的盒适合和灵活,我建议您选择FittedBox小部件。这是一个更新的代码。如果这对你有用的话请告诉我。
Padding(
padding: const EdgeInsets.only(
top: 10,
left: 5,
),
child: FittedBox(
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: product.type == 'Veg'
? Colors.green.withOpacity(0.8)
: Colors.red.withOpacity(0.8),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
product.type,
style: GoogleFonts.acme(
color: Colors.white,
),
),
),
),
),
),发布于 2022-07-21 10:41:32
尝试使用Wrap小部件。
Padding(
padding: const EdgeInsets.only(
top: 10,
left: 5,
),
child: Wrap(
child: Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: product.type == 'Veg'
? Colors.green.withOpacity(0.8)
: Colors.red.withOpacity(0.8),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
product.type,
style: GoogleFonts.acme(
color: Colors.white,
),
),
),
),
),
),https://stackoverflow.com/questions/73055835
复制相似问题