Flutter组件--Align和AnimatedAlign
创始人
2024-02-05 03:34:27
0

示意图:

Align介绍

Align 一般是用来确定子控件在父布局中的位置,比如居中、左上等多个对齐方式。

什么情况下使用Align?

当子组件需要设置位于父组件的某个位置时,需要用到Align.

Align构造函数

const Align({Key? key,this.alignment = Alignment.center,  // 子组件在父组件中的对齐方式this.widthFactor,  // 如果设置该值,Align的宽度始终是child宽度的两倍this.heightFactor, // 如果设置该值,Align的高度始终是child高度的两倍Widget? child,	// 子widget}) : assert(alignment != null),assert(widthFactor == null || widthFactor >= 0.0),assert(heightFactor == null || heightFactor >= 0.0),super(key: key, child: child);
import 'package:flutter/material.dart';class AlignExample extends StatefulWidget {@override_AlignExampleState createState() => _AlignExampleState();
}class _AlignExampleState extends State {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("AlignExample"),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Container(width: 200,height: 200,color: Colors.blueAccent,child: Align(alignment: Alignment.topRight,widthFactor: 100.0,heightFactor: 2.0,child: Text("Align"),),)],),),);}
}

AlignmentGeometry介绍

AlignmentGeometry 是一个如何对齐child 的一个组件,一般我们都是使用它的子类 Alignment 来进行对齐设置。

Alignment详解

Alignment继承自AlignmentGeometry,表示矩形内的一个点,他有两个属性xy,分别表示在水平和垂直方向的偏移。

const Alignment(this.x, this.y): assert(x != null),assert(y != null);

 Alignment属性

/// 顶部左侧对齐
static const Alignment topLeft = Alignment(-1.0, -1.0);/// 顶部中间对齐
static const Alignment topCenter = Alignment(0.0, -1.0);/// 顶部右侧对齐
static const Alignment topRight = Alignment(1.0, -1.0);/// 中间左侧对齐
static const Alignment centerLeft = Alignment(-1.0, 0.0);/// 垂直居中对齐
static const Alignment center = Alignment(0.0, 0.0);/// 中间右侧对齐
static const Alignment centerRight = Alignment(1.0, 0.0);/// 底部左侧对齐
static const Alignment bottomLeft = Alignment(-1.0, 1.0);/// 底部中间对齐
static const Alignment bottomCenter = Alignment(0.0, 1.0);/// 底部右侧对齐
static const Alignment bottomRight = Alignment(1.0, 1.0);

AnimatedAlign构造函数

const AnimatedAlign({Key? key,required this.alignment,  //必传, 子组件在父组件中的对齐方式 this.child,		// 子组件this.heightFactor,  // 如果设置该值,Align的高度始终是child高度的两倍this.widthFactor,  // 如果设置该值,Align的宽度始终是child宽度的两倍 Curve curve = Curves.linear,  // 动画的运动速率required Duration duration,   // 必传,动画的持续时间VoidCallback? onEnd,  // 动画结束时的回调
}) : assert(alignment != null),
assert(widthFactor == null || widthFactor >= 0.0),
assert(heightFactor == null || heightFactor >= 0.0),
super(key: key, curve: curve, duration: duration, onEnd: onEnd);

AnimatedAlign完整示例代码

import 'package:flutter/material.dart';class AnimatedAlignExample extends StatefulWidget {@override_AnimatedAlignExampleState createState() => _AnimatedAlignExampleState();
}class _AnimatedAlignExampleState extends State {bool selected = false;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("AnimatedAlignExample"),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Container(width: 250.0,height: 250.0,color: Colors.green,child: AnimatedAlign(alignment: selected ? Alignment.topRight : Alignment.bottomLeft,duration: Duration(milliseconds: 1000),curve: Curves.fastOutSlowIn,child: Icon(Icons.ac_unit, size: 40,),widthFactor: 2.0,heightFactor: 2.0,onEnd: () {print("动画结束时进入");},),),ElevatedButton(child: Text('改变alignment参数'),onPressed: () {setState(() {selected = !selected;});}),],),),);}
}

总结

当子组件需要设置位于父组件的某个位置时,需要用到Align组件,而AnimatedAlign 是Align 组件的动画版本,只要对齐方式发生改变,它就会在给定的持续时间内自动转换子组件的位置.

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...