这是代码。 代码:
class ThemeChangerWidget extends StatelessWidget {
final List<String> string = ['Light', 'Dark', 'Amoled'];
@override
Widget build(BuildContext context) {
final stateData = Provider.of<ThemeNotifier>(context);
final ThemeData state = stateData.getTheme();
return Theme(
data: state.copyWith(unselectedWidgetColor: state.accentColor),
child: AlertDialog(
backgroundColor: state.primaryColor,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
title: Text('Select Theme', style: state.textTheme.body1),
content: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return RadioListTile(
value: index,
groupValue: themes.indexOf(state),
onChanged: (ind) {
onThemeChanged(ind, stateData);
},
title: Text(
string[index],
style: state.textTheme.body2,
),
);
},
itemCount: string.length,
)),
);
}
}'
错误-在执行布局期间抛出了以下断言:
RenderShrinkWrappingViewport不支持返回固有尺寸。
有时会抛出此错误,而不是上述LayoutBuilder不支持返回内在尺寸。
计算固有尺寸将需要实例化视口的每个子项,这违反了视口惰性加载的原则。 如果您只是想在主轴方向上收缩包装视口,则可以通过仅提供视口宽松的约束条件来实现该效果,而无需测量其固有尺寸。
LayoutBuilder does not support returning intrinsic dimensions.
时,这个解决方案同样适用。 - AmirSizedBox
是更好的方法。当时我经验不足,所以使用了Container
。我已经更新了答案。这个方法可行是因为ListView
尽可能地占用宽度,而IntristicWidth
的大小与其子元素相同。因此,您必须限制IntristicWidth
子元素的宽度。 - CoderUni