Flutter AlertDialog出现溢出截断报错,使用Flexible/Expanded无效如何解决?
你的代码存在3个错误写法,直接导致溢出报错:
Flexible只能作为Row/Column这类Flex布局的直接子组件生效,你把它套在UnconstrainedBox内部,完全没有起到约束宽度的作用UnconstrainedBox会解除父组件传递的宽度限制,允许子组件无限拉伸宽度,自然会超出AlertDialog的可用宽度- 超长Text组件没有开启自动换行属性,长文本会单行撑宽容器
修复后的完整代码如下:
builder: (BuildContext context) => AlertDialog( title: const Center(child: Text('Completing task')), content: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ // Flexible放在Row的直接子节点,约束内容宽度不超过AlertDialog可用宽度 Flexible( child: Column( // 让Column高度自适应内容,不会强行占满全屏高度 mainAxisSize: MainAxisSize.min, children: [ Text('Tag: ' + task.tag), // 开启自动换行,长文本超出宽度后自动折行 Text( "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", softWrap: true, ), const SizedBox(height: 10), const Text('Waiting for NFC'), const SizedBox(height: 20), const CircularProgressIndicator( color: Colors.grey, ), ], ), ), ], ), )
内容的提问来源于stack exchange,提问作者JBoudry
相关产品推荐
相关产品推荐

