一、常用组件
1.基础组件
| 组件 | 说明 | 示例 |
|---|
| text | 显示文本 | text(‘hello flutter’, style: textstyle(fontsize: 20)) |
| image | 显示图片 | image.network(‘https://example.com/image.jpg’) |
| icon | 显示图标 | icon(icons.home, size: 30, color: colors.blue) |
| raisedbutton / elevatedbutton | 按钮 | elevatedbutton(onpressed: () {}, child: text(‘click’)) |
| textfield | 输入框 | textfield(decoration: inputdecoration(labeltext: ‘name’)) |
1. text:文本显示组件
- 功能:用于显示一段文本。
- 常用属性:
- style:设置文本样式,如字体大小、颜色等。
- textalign:设置文本对齐方式。
- maxlines:设置最大显示行数。
- overflow:设置文本溢出处理方式,如省略号。
text(
'flutter 组件配置详解',
style: textstyle(fontsize: 20, color: colors.black, fontweight: fontweight.bold),
textalign: textalign.center,
maxlines: 1,
overflow: textoverflow.ellipsis,
)
| 属性 | 类型 | 说明 |
|---|
data | string | 显示的文本内容 |
style | textstyle | 字体大小、颜色、行高等 |
textalign | textalign | 文本对齐(left、right、center、justify) |
maxlines | int | 最大显示行数 |
overflow | textoverflow | 溢出处理:ellipsis(省略号)等 |
2. image:图片显示组件
- 功能:用于显示图片,支持网络图片、本地图片等。
- 常用属性:
- image:图片资源,如 assetimage、networkimage。
- width、height:设置图片宽高。
- fit:设置图片的适应方式,如填充、覆盖等。
image.network(
'https://example.com/image.jpg',
width: 200,
height: 100,
fit: boxfit.cover,
alignment: alignment.center,
)
| 属性 | 类型 | 说明 |
|---|
image | imageprovider | 图片数据,如 assetimage, networkimage |
width / height | double | 宽/高 |
fit | boxfit | 缩放方式:fill、cover、contain、fitwidth、fitheight |
repeat | imagerepeat | 是否重复显示图片 |
alignment | alignment | 对齐方式 |
3. icon:图标组件
- 功能:用于显示图标。
- 常用属性:
- icon:图标数据,如 icons.home。
- size:图标大小。
- color:图标颜色。
- 示例
icon(
icons.home,
size: 30,
color: colors.green,
)
4. elevatedbutton:按钮组件
- 功能:用于创建一个凸起的按钮。
- 常用属性:
- onpressed:按钮点击事件处理
- child:按钮的子组件,通常是文本。
- style:按钮样式,如背景色、形状等。
elevatedbutton(
onpressed: () => print('clicked'),
child: text('提交'),
style: elevatedbutton.stylefrom(
primary: colors.blue,
onprimary: colors.white,
padding: edgeinsets.symmetric(horizontal: 20, vertical: 12),
shape: roundedrectangleborder(borderradius: borderradius.circular(8)),
),
)
| 属性 | 类型 | 说明 |
|---|
onpressed | function() | 按钮点击事件 |
child | widget | 按钮内容 |
style | buttonstyle | 自定义样式:背景色、形状、边距 |
5.textfield:文本输入框组件
- 功能:用于接收用户输入的文本。
- 常用属性:
- controller:控制器,用于获取输入内容。
- decoration:输入框的装饰,如提示文本、边框等。
- obscuretext:是否隐藏输入内容,常用于密码输入。
textfield(
controller: _usernamecontroller,
obscuretext: false,
keyboardtype: textinputtype.text,
maxlength: 20,
decoration: inputdecoration(
labeltext: '用户名',
hinttext: '请输入用户名',
prefixicon: icon(icons.person),
border: outlineinputborder(),
),
onchanged: (value) {
print('输入内容:$value');
},
)
| 属性 | 类型 | 说明 |
|---|
controller | texteditingcontroller | 控制器(获取/设置输入值) |
decoration | inputdecoration | 输入框装饰 |
obscuretext | bool | 是否隐藏内容(密码输入) |
keyboardtype | textinputtype | 键盘类型:text、number、email 等 |
maxlength | int | 最大输入长度 |
onchanged | (value) => void | 内容改变时的回调 |
2.布局组件
| 组件 | 说明 | 示例 |
|---|
| row / column | 横/纵向布局 | row(children: [text(‘a’), text(‘b’)]) |
| container | 容器,可设大小、颜色、边距等 | container(width: 100, height: 100, color: colors.red) |
| padding | 添加内边距 | padding(padding: edgeinsets.all(8), child: text(‘padded’)) |
| expanded | 撑满剩余空间 | expanded(child: container(color: colors.green)) |
| stack | 叠层布局 | stack(children: […]) |
| 组件 | 说明 | 示例代码 |
|---|
container | 常用容器组件,支持 padding、margin、decoration | container(width: 100, margin: edgeinsets.all(10)) |
padding | 仅设置内边距 | padding(padding: edgeinsets.all(10), child: ...) |
align / center | 对齐子组件 | center(child: text("中间")) |
row / column | 横向或纵向排列子组件 | row(children: [text("a"), text("b")]) |
expanded | 填满剩余空间 | expanded(child: container(color: colors.red)) |
sizedbox | 固定宽高或空隙 | sizedbox(height: 20) |
stack | 组件层叠 | stack(children: [container(), positioned(...)]) |
wrap | 自动换行布局 | wrap(children: [...]) |
spacer | 占位间隔 | spacer(flex: 1) |
1. row 和 column:线性布局组件
- 功能:分别用于水平和垂直排列子组件。
- 常用属性:
- mainaxisalignment:主轴对齐方式。
- crossaxisalignment:交叉轴对齐方式。
- children:子组件列表。
row(
mainaxisalignment: mainaxisalignment.spaceevenly,
crossaxisalignment: crossaxisalignment.center,
children: [
icon(icons.star),
icon(icons.star),
icon(icons.star),
],
)
| 属性 | 类型 | 说明 |
|---|
children | list<widget> | 子组件列表 |
mainaxisalignment | mainaxisalignment | 主轴对齐 |
crossaxisalignment | crossaxisalignment | 交叉轴对齐 |
mainaxissize | mainaxissize | 主轴占用空间(max/min) |
2. container:容器组件
- 功能:用于创建一个矩形可视区域,可设置大小、边距、边框、背景色等
- 常用属性:
- width、height:设置宽高。
- padding、margin:内边距和外边距。
- decoration:装饰,如背景色、边框等
container(
width: 200,
height: 100,
padding: edgeinsets.all(10),
margin: edgeinsets.only(top: 20),
alignment: alignment.center,
decoration: boxdecoration(
color: colors.amber,
borderradius: borderradius.circular(10),
boxshadow: [boxshadow(color: colors.grey, blurradius: 4)],
),
child: text('我是容器'),
)
| 属性 | 类型 | 说明 |
|---|
width / height | double | 宽高 |
padding / margin | edgeinsets | 内/外边距 |
decoration | boxdecoration | 背景、边框、阴影、圆角等 |
alignment | alignment | 子组件对齐方式 |
child | widget | 子组件 |
3. stack 和 positioned:堆叠布局组件
- 功能:用于堆叠多个子组件,可以通过 positioned 精确定位。
- 常用属性:
- alignment:设置子组件的对齐方式。
- children:子组件列表
stack(
children: [
container(width: 200, height: 200, color: colors.green),
positioned(
top: 20,
left: 20,
child: container(width: 100, height: 100, color: colors.red),
),
],
)
| 属性 | 类型 | 说明 |
|---|
alignment | alignment | 默认子组件对齐方式 |
children | list<widget> | 子组件列表 |
fit | stackfit | 子组件如何填充 stack |
customscrollview 滚动组件
复杂滚动页面,可组合多个 sliver(静态+动态+吸顶)
customscrollview 适用场景
- 首页复杂页面,比如:
- 顶部轮播图
- 宫格菜单
- 限时活动
- 吸顶一级/二级菜单
- 底部商品瀑布流
customscrollview(
slivers: [
slivertoboxadapter(child: bannerwidget()),
slivertoboxadapter(child: gridmenuwidget()),
sliverpersistentheader(
pinned: true,
delegate: stickytabbardelegate(),
),
sliverstaggeredgrid.countbuilder(
crossaxiscount: 2,
itemcount: productlist.length,
itembuilder: (context, index) => productitemwidget(product: productlist[index]),
staggeredtilebuilder: (index) => staggeredtile.fit(1),
),
],
)
3.功能类组件
| 组件 | 说明 | 示例代码 |
|---|
appbar | 页面顶部导航栏 | appbar(title: text("标题")) |
bottomnavigationbar | 底部导航栏 | bottomnavigationbar(items: [...]) |
drawer | 抽屉菜单 | drawer(child: listview(...)) |
tabbar / tabbarview | 顶部选项卡 | 配合 tabcontroller 使用 |
navigator | 页面跳转 | navigator.push(context, materialpageroute(builder: (_) => page())) |
alertdialog | 弹窗对话框 | showdialog(context: ..., builder: (_) => alertdialog(...)) |
gesturedetector | 手势识别 | gesturedetector(ontap: () { ... }) |
1. listview:可滚动的列表组件
- 功能:用于显示一个可滚动的列
- 常用属性:
- children:子组件列表。
- itembuilder:用于懒加载子组件。
- separatorbuilder:用于添加分隔线。
listview.builder(
itemcount: 10,
itembuilder: (context, index) {
return listtile(
leading: icon(icons.person),
title: text('person $index'),
subtitle: text('subtitle $index'),
);
},
)
| 属性 | 类型 | 说明 |
|---|
children | list<widget> | 静态子组件列表 |
itembuilder | indexedwidgetbuilder | 构建子项 |
itemcount | int | 子项数量 |
scrolldirection | axis | 滚动方向(horizontal, vertical) |
2. gridview:网格布局组件
- 功能:用于创建一个网格布局的可滚动列表
- 常用属性:
- griddelegate:控制网格的布局方式。
- children:子组件列表。
gridview.count(
crossaxiscount: 2,
children: list.generate(4, (index) {
return center(
child: text(
'item $index',
style: theme.of(context).texttheme.headline5,
),
);
}),
)
| 属性 | 类型 | 说明 |
|---|
crossaxiscount | int | 横轴显示个数 |
mainaxisspacing | double | 主轴间距 |
crossaxisspacing | double | 横轴间距 |
childaspectratio | double | 宽高比 |
3. card:卡片组件
- 功能:用于创建具有圆角和阴影的卡片效果。
- 常用属性:
- elevation:设置阴影的大小。
- shape:设置卡片的形状。
- child:卡片的子组件。
card(
elevation: 5,
shape: roundedrectangleborder(
borderradius: borderradius.circular(10),
),
child: padding(
padding: edgeinsets.all(16),
child: text('this is a card'),
),
)
4. 状态管理类
- setstate():最简单的本地状态更新方式
- 插件方式:provider、riverpod、getx、bloc。
表单组件
| 组件 | 说明 | 示例代码 |
|---|
textfield | 输入框 | textfield(decoration: inputdecoration(labeltext: '姓名')) |
textformfield | 带验证的输入框 | textformfield(validator: (val) => val!.isempty ? '必填' : null) |
form | 表单容器 | form(key: _formkey, child: column(...)) |
checkbox / checkboxlisttile | 复选框 | checkbox(value: true, onchanged: ...) |
radio / radiolisttile | 单选框 | radio(value: 1, groupvalue: _val, onchanged: ...) |
switch | 开关 | switch(value: true, onchanged: ...) |
dropdownbutton | 下拉菜单 | dropdownbutton(items: [...], onchanged: ...) |
slider | 滑块 | slider(value: 50, min: 0, max: 100, onchanged: ...) |
5 屏幕适配方案
| 方案 | 描述 | 插件推荐 |
|---|
| 基于设计稿等比缩放 | 将 ui 尺寸按比例缩放适配 | flutter_screenutil |
| mediaquery 自适应 | 读取设备尺寸并调整布局 | 内置 mediaquery.of(context) |
| layoutbuilder 自适应 | 按父容器的尺寸动态布局 | 内置 layoutbuilder(...) |
| 响应式组件封装 | 根据屏幕宽度切换 ui | responsive_framework、flutter_responsive |
插件推荐:flutter_screenutil
dependencies:
flutter_screenutil: ^5.5.3+2
使用实例
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
runapp(
screenutilinit(
designsize: size(375, 812), // 设计稿尺寸(例如 iphone x)
builder: (context, child) => myapp(),
),
);
}
class myhomepage extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: container(
width: 100.w, // 屏幕适配宽度
height: 100.h, // 屏幕适配高度
child: text('hello', style: textstyle(fontsize: 16.sp)), // 字体大小适配
),
),
);
}
}
其他适配逻辑
| 插件 | 功能 | 适合场景 |
|---|
flutter_screenutil | 基于设计稿自动缩放 | 推荐用于大部分项目 |
responsive_builder | 响应式断点布局 | 多平台(web、mobile)适配 |
flutter_responsive_framework | 精准控制不同设备 ui | 企业级适配 |
mediaquery_sizer | 类似 screenutil,但更轻量 | 轻量项目可用 |
常用目录设计方案
lib/
├─ main.dart # 程序入口
├─ common/ # 公共工具、样式、api
│ ├─ api.dart # 接口请求统一管理
│ ├─ constants.dart # 常量、枚举
│ ├─ utils/ # 工具类(日期、网络、加密等)
│ └─ styles/ # 主题、颜色、字体、全局样式
│
├─ widgets/ # 全局可复用组件(跨页面)
│ ├─ custom_button.dart # 通用按钮
│ ├─ empty_view.dart # 空状态占位
│ ├─ loading_more.dart # 加载更多动画
│ └─ custom_appbar.dart # 顶部通用 appbar
│
├─ pages/ # 页面模块
│ ├─ home/ # 首页模块
│ │ ├─ home_page.dart
│ │ ├─ widgets/ # 仅首页专属组件
│ │ │ ├─ notice_bar.dart
│ │ │ ├─ banner_carousel.dart
│ │ │ ├─ menu_grid.dart
│ │ │ ├─ waterfall_list.dart
│ │ │ └─ tab_section.dart
│ │ └─ home_controller.dart # 状态管理(provider / riverpod)
│ │
│ ├─ product/ # 商品模块
│ │ ├─ product_page.dart
│ │ ├─ widgets/ # 商品页面专属组件
│ │ │ ├─ product_card.dart
│ │ │ ├─ product_filter.dart
│ │ │ └─ product_banner.dart
│ │ └─ product_controller.dart
│ │
│ └─ mine/ # 个人中心模块
│ ├─ mine_page.dart
│ └─ widgets/
│ └─ user_info_card.dart
│
└─ routes/ # 路由统一管理
└─ app_routes.dart
结构说明
common/
放项目全局可用的工具、常量、样式和接口
类似前端的 utils/ 或 services/
widgets/
全局复用组件,不属于某个页面
例:按钮、加载动画、空状态组件
pages/
每个页面/模块独立一个目录
内部 widgets/ 只放该页面的专属组件
页面逻辑、状态管理、子组件都归属本模块
routes/
页面路由统一管理,便于导航和跳转
总结
到此这篇关于flutter常用组件详细介绍、屏幕适配方案、常用目录设计方案的文章就介绍到这了,更多相关flutter常用组件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论