为什么我得到这个Renderflex错误而无法解决它



我试图显示一个星形图标,当用户按下它时,它会显示5颗星,这样用户就可以投票了。但我得到一个错误,说

he following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a Listener widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
Column ← Expanded ← Container ← Listener ← RawGestureDetector ← GestureDetector ← Semantics ← _RawMouseRegion ← MouseRegion ← Semantics ← ⋯
════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 74 pixels on the right.
The relevant error-causing widget was
Row
lib/rating.dart/ratingbar.dart:42

我在代码中标记了错误的地方,希望任何人都能帮助你,如果你需要更多信息,请留下评论。当我按下图标上的时,错误就在线了,并且显示了带有文本"清除"的5颗星。我在另一个类中调用小部件,如果你需要,也请留下评论,

class Ratingpage extends StatefulWidget {
final int maximumRating;
final Function(int) onRatingSelected;
Ratingpage(this.onRatingSelected, [this.maximumRating = 5]);
@override
_RatingpageState createState() => _RatingpageState();
}
class _RatingpageState extends State<Ratingpage> {
int _currentRating = 0;
Widget _buildRatingStar(int index) {
if (index < _currentRating) {
return Icon(
Icons.star,
color: Colors.yellow,
);
} else {
return Icon(
Icons.star,
color: Colors.white,
);
}
}
Widget _buildBody() {
final stars = List<Widget>.generate(this.widget.maximumRating, (index) {
return GestureDetector(
child: _buildRatingStar(index),
onTap: () {
setState(() {
_currentRating = index + 1;
});
this.widget.onRatingSelected(_currentRating);
},
);
});
return 
Row(here the erroroororoororoorororoororoororororoorororoorororooror
mainAxisAlignment: MainAxisAlignment.end,
children: [
Row( 
children: stars,
),

TextButton(
onPressed: () {
setState(() {
_currentRating = 0;
});
this.widget.onRatingSelected(_currentRating);
},
child: Text(
"Clear",
style: TextStyle(color: Colors.white),
),
),
],


);
}
@override
Widget build(BuildContext context) {
return _buildBody();
}
}

我试着用膨胀包住,但无济于事。也许任何人都可以帮助

如果不看代码,我真的无法判断,但错误表明扩展的小部件有错误的祖先。

Expanded必须是Row、Column或Flex窗口小部件的子代,如果它不起作用,您可能会尝试在无法使用的地方使用Expanded((。

也许这就是你想要的

return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Row(
children: [
Expanded(
child: Container(
color: Colors.amber,
width: 100,
),
),
],
),
],
);`

最新更新