【问题标题】:flutter listview with radio not showing in alertDialog带有收音机的颤动列表视图未显示在警报对话框中
【发布时间】:2020-07-08 12:53:02
【问题描述】:

这是代码。 代码:

  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,
      )),
      );
     }
     }'

errors - 在 performLayout() 期间抛出了以下断言:

RenderShrinkWrappingViewport 不支持返回固有尺寸。

有时会抛出这个错误而不是上面LayoutBuilder does not support returning intrinsic dimensions.

计算内在尺寸需要实例化视口的每个子项,这会破坏视口惰性的观点。 如果您只是试图在主轴方向上收缩包裹视口,您应该能够通过给视口松散约束来实现该效果,而无需测量其固有尺寸。

【问题讨论】:

    标签: android-studio flutter dart


    【解决方案1】:

    AlertDialog 使用不允许 ListView.builderIntrinsicWidth 小部件。您必须为您的ListView 示例指定特定宽度:

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

    【讨论】:

    • 当您遇到此错误LayoutBuilder does not support returning intrinsic dimensions. 时,此解决方案也有效
    • SizedBox(width: double.maxFinite, child: ...) 也可以工作,而且可能会更好,因为容器有很多你不需要的额外花里胡哨。我很高兴我找到了解决方案,但为什么会这样?还有更优雅的解决方案吗?
    • 感谢@1housand 的提醒。 SizedBox 是一种更好的方法。那时我没有经验,所以我使用了Container。我已经更新了答案。这是可行的,因为ListView 尽可能多地占用宽度,并且IntristicWidth 调整自身大小以使其具有与其子级相同的宽度。因此,您必须限制 IntristicWidth 子项的宽度。
    猜你喜欢
    • 2011-09-29
    • 2021-11-08
    • 1970-01-01
    • 2019-11-15
    • 2013-03-23
    • 1970-01-01
    • 2018-11-30
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多