- 1
- 0
- 约2.4千字
- 约 4页
- 2025-05-24 发布于四川
- 举报
第
Flutter搞定宽高不统一布局开发的方法详解
目录前言Wrap组件简介Wrap使用示例总结
前言
我们在开发移动端界面的时候,经常会遇到一组尺寸不一的组件需要作为同一组展示,典型的就是下面这种搜索历史。搜索内容的文字长短不一,导致显示的宽度不一致。而且,需要根据屏幕的宽度来自动换行显示。这个时候,用行布局或者网格布局都满足不了要求,那怎么办呢?其实Flutter为我们提供了很好的组件,那就是Wrap组件。
Wrap组件简介
Wrap组件是一种动态布局组件,非常适用于需要动态调整子组件布局的场景,当子组件的总宽度或高度超过父容器的宽度或高度时,它会自动将子组件放置到新的行或列中。Wrap组件的定义如下:
Wrap({
Keykey,
this.direction=Axis.horizontal,
this.alignment=WrapAlignment.start,
this.spacing=0.0,
this.runAlignment=WrapAlignment.start,
this.runSpacing=0.0,
this.crossAxisAlignment=WrapCrossAlignment.start,
this.textDirection,
this.verticalDirection=VerticalDirection.down,
this.clipBehavior=Clip.none,
ListWidgetchildren=constWidget[],
})
其中关键参数如下:
direction:布局方向,默认横向,即横向摆不下的时候会另起一行;如果更改为纵向(vertical),那么会按纵向排布,纵向排不下的时候会另起一列。通常横向布局会多一点。alignment:主轴对齐方式,默认是start,横向来说是左对齐。也可以设置为右对齐(end),或居中(center).spacing:横轴方向的间距大小。runSpacing:纵轴方向的间距大小。verticalDirection:换行或换列的方向,默认是往下,如果改成向上(up),那么超出后会往上(横向)另起一行,也就是从底部往上换行。这种其实比较少见。children:也就是要子组件,通常会配合Chip、InputChip这样的组件使用,其实任何组件都可以,比如图片,按钮都没问题。
Wrap使用示例
我们这里使用Wrap组件包裹了Chip组件和图片组件,代码如下。
@override
Widgetbuild(BuildContextcontext){
returnScaffold(
backgroundColor:Colors.grey[50],
appBar:
AppBar(title:constText(Wrap),backgroundColor:Colors.red[400]!),
body:Center(
child:Column(
mainAxisAlignment:MainAxisAlignment.center,
children:[
Wrap(
direction:Axis.horizontal,
spacing:8.0,
alignment:WrapAlignment.start,
verticalDirection:VerticalDirection.down,
children:List.generate(
_tagsLists.length,
(index)=Chip(
label:Text(_tagsLists[index]),
backgroundColor:Colors.grey[300],
labelStyle:constTextStyle(color:Colors.black87),
constSizedBox(
height:20.0,
Wrap(
spacing:8.0,
runSpacing:8.0,
children:List.generate(
_imagesLists.length,
(index)=Container(
width:120,
height:100,
decoration:BoxDecoration(
borderRadius:Bord
您可能关注的文档
最近下载
- 2024年英语高考一轮复习专题精练 非谓语动词之不定式含详解.pdf VIP
- 2024年恩施职业技术学院高职单招(英语/数学/语文)笔试历年真题摘选含答案解析.docx VIP
- 剪映课件演示教学.ppt VIP
- 2025年辽宁地质工程职业学院单招《数学》试题及参考答案详解【综合题】.docx VIP
- Unit 1 Looking Forwards Understanding ideas Facing the Future 说课 讲课课件-2024-2025学年高二下学期英语外研版(2019)选择性必修四册.pptx VIP
- 新解读《GB 12268-2012危险货物品名表》.docx VIP
- 2024年恩施职业技术学院高职单招(英语/数学/语文)笔试历年真题摘选含答案解析.docx VIP
- 5eDnD_凡戴尔的失落矿坑_模组_中译(二校).pdf VIP
- 可信高速数据网研究报告 2025.pdf
- 门窗安装技术交底.docx VIP
原创力文档

文档评论(0)