Flutter中,带有单选框的ListView在AlertDialog中不显示。

6

这是代码。 代码:

  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不支持返回内在尺寸。

计算固有尺寸将需要实例化视口的每个子项,这违反了视口惰性加载的原则。 如果您只是想在主轴方向上收缩包装视口,则可以通过仅提供视口宽松的约束条件来实现该效果,而无需测量其固有尺寸。

enter image description here

2个回答

17

AlertDialog使用了一个不允许ListView.builder的IntrinsicWidth小部件。您需要为您的ListView指定一个特定的宽度。例如:

    return AlertDialog(
      title: Text('Dialog'),
      content: SizedBox(
        width: double.maxFinite,
        child: ListView(
          children: <Widget>[
           //Your content here
          ],
        ),
      ),
    );

1
当你遇到这个错误 LayoutBuilder does not support returning intrinsic dimensions. 时,这个解决方案同样适用。 - Amir
SizedBox(width: double.maxFinite, child: ...)也可以使用,而且可能更好,因为Container有很多你不需要的额外功能。我很高兴找到了一个解决方案,但为什么它有效呢?是否有更优雅的解决方案? - 1housand
1
感谢 @1housand 的提醒。使用 SizedBox 是更好的方法。当时我经验不足,所以使用了 Container。我已经更新了答案。这个方法可行是因为 ListView 尽可能地占用宽度,而 IntristicWidth 的大小与其子元素相同。因此,您必须限制 IntristicWidth 子元素的宽度。 - CoderUni

-1

在对话框中从API获取数据的正确方法不是使用StatefulBuilder并将其包装在SizedBox中,而是没有找到正确的路径,即使数据仅使用int函数的索引打印,也只能使用void函数的索引。


1
目前您的回答不清晰。请进行[编辑]以添加更多细节,以帮助其他人了解其如何解决问题。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接