【发布时间】:2021-11-11 05:35:54
【问题描述】:
我的布局是一个带有一些控件的表单小部件:
Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Form(
key: widget.addEventFormKey,
onChanged: () {},
child: Expanded(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(CustomResources.strings["add_event_category_label"], style: TextStyle(fontWeight: FontWeight.bold)),
/*some other widgets*/
Visibility(
child: Container(
height: 200.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _attachments?.length,
itemBuilder: (context, index) {
return Stack(
children: [
Container(
margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
child: Stack(
children: [
ClipRRect(
child: Image.file(File(_attachments[index]), cacheWidth: 200),
borderRadius: BorderRadius.circular(8.0),
),
],
),
),
IconButton(
onPressed: () {
File(_attachments[index]).delete();
setState(() => _attachments.remove(_attachments[index]));
},
icon: Icon(FontAwesomeIcons.trash, color: Colors.white, size: 20),
padding: EdgeInsets.only(top: 4, left: 4),
constraints: BoxConstraints(),
),
],
);
},
),
),
visible: _attachments.length > 0,
),
Visibility(child: Padding(padding: EdgeInsets.fromLTRB(0, 18, 0, 0)),visible: _attachments.length > 0),
SizedBox(
child: RaisedButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.0)),
onPressed: _attachments.length < 3 ? () => _pickImage() : null,
padding: EdgeInsets.all(12.0),
color: Colors.blue,
child: Text(CustomResources.strings["add_event_add_photo_button_label"], style: TextStyle(color: Colors.white))),
width: double.infinity,
),
],
),
),
),
),
],
);
有问题的部分是ListView.builder 显示水平滚动的图像列表。如您所见,图片将始终获得固定宽度(200)和未知高度,因为高度取决于图像纵横比。 ListView.builder 被 Container 包裹,所以现在它的高度为 200。
我想强制我的ListView.builder 扩展到子图像高度(它是水平滚动的单行 ListView),图像宽度始终为 200,其他小部件应放置在其下方,没有任何剩余空间。使用当前方法,如果图像高度 200,图像将被缩放(宽度/高度)。
我尝试用Expanded 小部件而不是Container 包装列表视图,但它抛出异常:
RenderFlex 子级具有非零 flex 但传入的高度约束 是无限的。
它说我仍然需要提供高度,但我不想这样做。如何解决?
【问题讨论】:
标签: flutter listview layout horizontal-scrolling