======== Exception caught by widgets library =======================================================
The 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 BoxParentData.
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 Padding widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
Column ← Expanded ← Padding ← DecoratedBox ← ConstrainedBox ← Container ← Expanded ← Column ← Semantics ← DefaultTextStyle ← ⋯
solution:
你遇到的錯誤訊息 "Incorrect use of ParentDataWidget" 表示你正在試圖以不允許的方式使用 Expanded widget。Expanded widget 應該被用在支援它的父級 widget 內,例如 Column、Row 或 Flex,以指定它的子 widget 應如何填充可用空間。將 Expanded widget 放在 Padding widget 內可能會導致此錯誤。
要解決這個問題,請確保你在適當的父級 widget 內使用 Expanded widget。以下是正確使用的方法:
將
Expandedwidget 包裹在支援的父級 widget 內:如果你打算使用Expanded來控制一個 widget 如何填充可用空間,應將它直接放在Column、Row或Flexwidget 內。dartColumn( children: [ Expanded( flex: 1, child: YourWidget(), ), ], )重新評估你的佈局:如果你嘗試在不同的上下文中使用
Expanded,請重新評估你的佈局結構。你可能需要調整 widget 階層結構以達到你期望的設計。
以下是在 Column 內正確使用 Expanded 的示例:
dartColumn( children: [ Expanded( flex: 1, child: Container( color: Colors.red, ), ), Container( color: Colors.blue, ), ], )
在這個示例中,使用 Expanded 包裹的第一個 Container 將佔據所有可用的垂直空間,將第二個 Container 推到 Column 的底部。
確保 Expanded widget 被用在能夠支援它的父級 widget 內,例如 Column、Row 或 Flex。
來看看code:
原來錯誤的地方是expanded ,紅字的部分,移除就好了.
所以要檢查一下所有程式碼,是否有多餘的expanded.
child: Expanded(
child: Column(
children: [
Icon(
isLightOnList[index]
? Icons.light_mode_outlined
: Icons.lightbulb_outline,
size: 48,
color:
isLightOnList[index] ? Colors.white : Colors.grey,
),
const SizedBox(height: 16),
Text(
'Light ${index + 1}',
style: TextStyle(
fontSize: 14,
color: isLightOnList[index]
? Colors.white
: Colors.grey,
fontWeight: FontWeight.bold,
),
),
留言