Überlaufprobleme, die durch die Verwendung von AlignedGridView, GridView, ListView und anderen Komponenten in Flutter verursacht werden
Wiederkehr des Problems
Die Fehlermeldung lautet wie folgt
The following assertion was thrown during layout:
A RenderFlex overflowed by 189 pixels on the bottom.
The relevant error-causing widget was:
Column Column:file:///D:/DartProject/yqplaymusic/lib/components/CoverRow.dart:138:9
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.
The specific RenderFlex in question is: RenderFlex#9b9fb OVERFLOWING
... needs compositing
... parentData: offset=Offset(0.0, 0.0) (can use size)
... constraints: BoxConstraints(w=215.5, h=95.0)
... size: Size(215.5, 95.0)
... direction: vertical
... mainAxisAlignment: start
... mainAxisSize: max
... crossAxisAlignment: start
... textDirection: ltr
... verticalDirection: down
====================================================================================================
Ursache des Problems
Normalerweise liegt das daran, dass wir Komponenten mit unsicherer Breite oder Höhe verwenden, wie zum Beispiel Row
, Column
, und andere Komponenten und dann die Verwendung von Komponenten mit unsicherer Breite und Höhe in den Unterkomponenten, was manchmal dazu führen kann, dass die Höhe der Komponente nicht korrekt berechnet wird, was kurzzeitig auftreten kann . Abnormales Phänomen, dann wieder normalListView
, GridView
Lösung
Verwenden Sie in einer untergeordneten Komponente wieListView
Row
oder Ersetzen Sie und stellen Sie die gewünschte Richtung einColumn
ListView
ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: const NeverScrollableScrollPhysics(),
children: [],
)
Es ist zu beachten, dass das Attribut shrinkWrap
auf true
gesetzt werden muss, damit ListView
die Unterkomponente automatisch berechnen kann Größe