当前位置:网站首页>【Flutter -- 布局】流式布局(Flow和Wrap)
【Flutter -- 布局】流式布局(Flow和Wrap)
2022-07-24 15:16:00 【Kevin-Dev】

Flow
1. 简介
Flow 是一个实现流式布局算法的控件。流式布局在大前端是很常见的布局方式,但是一般使用 Flow 很少,因为其过于复杂,很多场景下都会去使用 Wrap 。
2. 属性
delegate:影响 Flow 具体布局的 FlowDelegate。
其中 FlowDelegate 包含如下几个方法:
- getConstraintsForChild: 设置每个 child 的布局约束条件,会覆盖已有的;
- getSize:设置 Flow 的尺寸;
- paintChildren:child 的绘制控制代码,可以调整尺寸位置,写起来比较的繁琐;
- shouldRepaint:是否需要重绘;
- shouldRelayout:是否需要重新布局。
其中,我们平时使用的时候,一般会使用到 paintChildren 以及 shouldRepaint 两个方法。
3. 使用场景
Flow 在一些定制化的流式布局中,有可用场景,但是一般写起来比较复杂,但胜在灵活性以及其高效。
4. 实例
1. 效果图

2. 示例代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('流式布局'),
),
body: Center(
child: Flow(
delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
children: <Widget>[
Container(width: 80.0, height:80.0, color: Colors.red,),
Container(width: 80.0, height:80.0, color: Colors.green,),
Container(width: 80.0, height:80.0, color: Colors.blue,),
Container(width: 80.0, height:80.0, color: Colors.yellow,),
Container(width: 80.0, height:80.0, color: Colors.brown,),
Container(width: 80.0, height:80.0, color: Colors.purple,),
],
),
)
)
);
}
}
class TestFlowDelegate extends FlowDelegate {
EdgeInsets margin;
TestFlowDelegate({
this.margin = EdgeInsets.zero});
double width = 0;
double height = 0;
@override
void paintChildren(FlowPaintingContext context) {
var x = margin.left;
var y = margin.top;
//计算每一个子widget的位置
for (int i = 0; i < context.childCount; i++) {
var w = context.getChildSize(i)!.width + x + margin.right;
if (w < context.size.width) {
context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
x = w + margin.left;
} else {
x = margin.left;
y += context.getChildSize(i)!.height + margin.top + margin.bottom;
//绘制子widget(有优化)
context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
x += context.getChildSize(i)!.width + margin.left + margin.right;
}
}
}
@override
Size getSize(BoxConstraints constraints) {
// 指定Flow的大小,简单起见我们让宽度竟可能大,但高度指定为200,
// 实际开发中我们需要根据子元素所占用的具体宽高来设置Flow大小
return Size(double.infinity, 200.0);
}
@override
bool shouldRepaint(FlowDelegate oldDelegate) {
return oldDelegate != this;
}
}
Wrap
1. 简介
流式布局组件,如果内容宽度超出屏幕宽度,会自动折行显示。
2. 属性
direction:主轴(mainAxis)的方向,默认为水平。
alignment:主轴方向上的对齐方式,默认为start。
spacing:主轴方向上的间距。
runAlignment:run的对齐方式。run可以理解为新的行或者列,如果是水平方向布局的话,run可以理解为新的一行。
runSpacing:run的间距。
crossAxisAlignment:交叉轴(crossAxis)方向上的对齐方式。
textDirection:文本方向。
verticalDirection:定义了children摆放顺序,默认是down,见Flex相关属性介绍。
3. 使用场景
对于一些需要按宽度或者高度,让child自动换行布局的场景,可以使用,但是Wrap可以满足的场景,Flow一定可以实现,只不过会复杂很多,但是相对的会灵活以及高效很多。
4. 实例
1. 效果图

2. 代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('流式布局'),
),
body: Center(
child: Wrap(
spacing: 8.0,
runSpacing: 4.0,
children: <Widget>[
Chip(
avatar: CircleAvatar(
backgroundColor: Colors.green.shade300, child: new Text('AH', style: TextStyle(fontSize: 10.0),)),
label: Text('Kevin'),
),
Chip(
avatar: CircleAvatar(
backgroundColor: Colors.green.shade300, child: new Text('ML', style: TextStyle(fontSize: 10.0),)),
label: Text('Lafayette'),
),
Chip(
avatar: CircleAvatar(
backgroundColor: Colors.green.shade300, child: new Text('HM', style: TextStyle(fontSize: 10.0),)),
label: Text('Aaron'),
),
Chip(
avatar: CircleAvatar(
backgroundColor: Colors.green.shade300, child: new Text('JL', style: TextStyle(fontSize: 10.0),)),
label: Text('Laurens'),
),
],
),
)
)
);
}
}
边栏推荐
- Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)---第一题 不要浪费金币 (已完结)
- ZABBIX administrator forgot login password
- Explain the edge cloud in simple terms | 2. architecture
- Cloud development standalone image Jiugongge traffic main source code
- Spark: specify the date and output the log of the corresponding date (entry level - simple implementation)
- Kali concise language transformation method (illustration)
- 【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测
- zabbix管理员忘记登录密码
- File upload and download and conversion between excel and data sheet data
猜你喜欢

【机器学习基础】——另一个视角解释SVM

ZABBIX administrator forgot login password

"After 00" is coming! Digital data ushers in a new generation of "codeless" forces

Activity Registration: how to quickly start the open source tapdata live data platform on a zero basis?

Cloud development standalone image Jiugongge traffic main source code

Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
![Property datasource is required exception handling [idea]](/img/f3/50d36ed9445695c02e2bd622109d66.png)
Property datasource is required exception handling [idea]

Detailed explanation of document operation

JSON file editor

华为无线设备配置WPA2-802.1X-AES安全策略
随机推荐
pytorch with torch.no_grad
27. Directory and file system
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
Preparation of mobile end test cases
DDD based on ABP -- Entity creation and update
Extjs4 instance address and Chinese document address
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)---第一题 不要浪费金币 (已完结)
Performance test - Preparation of test plan
Leetcode 1288. delete the covered interval (yes, solved)
【Bug解决】Win10安装pycocotools报错
Vector introduction and underlying principle
Simple encapsulation of wechat applet wx.request
Spark: get the access volume of each time period in the log (entry level - simple implementation)
深入浅出边缘云 | 2. 架构
Calculate the M-day moving average price of two stocks
云开发单机版图片九宫格流量主源码
Leetcode-09 (next rank + happy number + full rank)
Overall testing framework for performance testing
Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals