当前位置:网站首页>Flutter Widget : Flow
Flutter Widget : Flow
2022-07-03 12:05:00 【J_ D_ Chi】
Write it at the front
\Flow Components need to be matched \FlowDelegate Use , stay \FlowDelegate Write logic in ,\Flow You can adjust its children Size and location of .
Use \Flow Is used to optimize children Use transformation matrix (transformation matrices) Rearrange . Be similar to Stack.
That is, when we want to customize a pair children Layout Widget When , have access to \Flow.
Content
Basic usage
From the official example, a basic usage . In this case , In addition to showing the right children Layout , It also demonstrates how to use Animation To make the children The transformation matrix used changes , So as to achieve the change of position .
class FlowMenu extends StatefulWidget {
const FlowMenu({Key? key}) : super(key: key);
@override
_FlowMenuState createState() => _FlowMenuState();
}
class _FlowMenuState extends State<FlowMenu> with SingleTickerProviderStateMixin {
late AnimationController menuAnimation;
IconData lastTapped = Icons.notifications;
final List<IconData> menuItems = <IconData>[
Icons.home,
Icons.new_releases,
Icons.notifications,
Icons.settings,
Icons.menu,
];
void _updateMenu(IconData icon) {
if (icon != Icons.menu) {
setState(() => lastTapped = icon);
}
}
@override
void initState() {
super.initState();
menuAnimation = AnimationController(
duration: const Duration(milliseconds: 250),
vsync: this,
);
}
Widget flowMenuItem(IconData icon) {
final double buttonDiameter = MediaQuery.of(context).size.width / menuItems.length;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: RawMaterialButton(
fillColor: lastTapped == icon ? Colors.amber[700] : Colors.blue,
splashColor: Colors.amber[100],
shape: const CircleBorder(),
constraints: BoxConstraints.tight(Size(buttonDiameter, buttonDiameter)),
onPressed: () {
_updateMenu(icon);
menuAnimation.status == AnimationStatus.completed
? menuAnimation.reverse()
: menuAnimation.forward();
},
child: Icon(
icon,
color: Colors.white,
size: 45.0,
),
),
);
}
@override
Widget build(BuildContext context) {
return Flow(
delegate: FlowMenuDelegate(menuAnimation: menuAnimation),
children: menuItems.map<Widget>((IconData icon) => flowMenuItem(icon)).toList(),
);
}
}
class FlowMenuDelegate extends FlowDelegate {
FlowMenuDelegate({required this.menuAnimation}) : super(repaint: menuAnimation);
final Animation<double> menuAnimation;
@override
bool shouldRepaint(FlowMenuDelegate oldDelegate) {
return menuAnimation != oldDelegate.menuAnimation;
}
@override
void paintChildren(FlowPaintingContext context) {
double dx = 0.0;
for (int i = 0; i < context.childCount; ++i) {
dx = context.getChildSize(i)!.width * i;
context.paintChild(
i,
transform: Matrix4.translationValues(
dx * menuAnimation.value,
0,
0,
),
);
}
}
}
Compared to the layout Stage layout children, Here it is. paint Stage , stay \FlowDelegate.paintChildren In the method , Give Way children Use the transformation matrix to determine the position . This is done simply by \Flow Redraw , So as to achieve children Efficient relocation . There is no need to let children Then rearrange .
Most effectively promote \Flow The way to redraw is to \FlowDelegate The construction method of provides a Animation, such \Flow Will monitor this Animation, Then when Animation When there is a callback ,\Flow Will redraw , Avoided build and layout.
Construction method
class Flow extends MultiChildRenderObjectWidget {
Flow({
Key? key,
required this.delegate,
List<Widget> children = const <Widget>[],
this.clipBehavior = Clip.hardEdge,
}) : assert(delegate != null),
assert(clipBehavior != null),
super(key: key, children: RepaintBoundary.wrapAll(children));
Flow.unwrapped({
Key? key,
required this.delegate,
List<Widget> children = const <Widget>[],
this.clipBehavior = Clip.hardEdge,
}) : assert(delegate != null),
assert(clipBehavior != null),
super(key: key, children: children);
...
}
\Flow There are two constructors , The difference is that by default RepaintBoundary Package , It wraps every children, Avoid when \Flow When redrawing , Lead to children Also redrawn .
边栏推荐
猜你喜欢

解决msvcp120d.dll和msvcr120d.dll缺失

(database authorization - redis) summary of unauthorized access vulnerabilities in redis

Vulnhub pyexp

QT OpenGL rotate, pan, zoom

4000字超详解指针

C language improvement article (wchar_t) character type

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements

(构造笔记)ADT与OOP

Download address and installation tutorial of vs2015

错排问题 (抽奖,发邮件)
随机推荐
CGroup introduction
Unity3d learning notes 5 - create sub mesh
Groovy测试类 和 Junit测试
【mysql专项】读锁和写锁
Interview experience in summer camp of Central South University in 2022
OPenGL 基本知识(根据自己理解整理)
OpenGL index cache object EBO and lineweight mode
(构造笔记)ADT与OOP
rxjs Observable filter Operator 的实现原理介绍
Go语言实现静态服务器
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Download address and installation tutorial of vs2015
Vulnhub pyexp
Slam mapping and autonomous navigation simulation based on turnlebot3
Dart: About zone
Colleagues wrote a responsibility chain model, with countless bugs
MySQL uses the method of updating linked tables with update
Redis 笔记 01:入门篇
cgroup简介