• Flutter组件--Align和AnimatedAlign


    示意图:

    Align介绍

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

    什么情况下使用Align?

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

    Align构造函数

    1. const Align({
    2. Key? key,
    3. this.alignment = Alignment.center, // 子组件在父组件中的对齐方式
    4. this.widthFactor, // 如果设置该值,Align的宽度始终是child宽度的两倍
    5. this.heightFactor, // 如果设置该值,Align的高度始终是child高度的两倍
    6. Widget? child, // 子widget
    7. }) : assert(alignment != null),
    8. assert(widthFactor == null || widthFactor >= 0.0),
    9. assert(heightFactor == null || heightFactor >= 0.0),
    10. super(key: key, child: child);
    1. import 'package:flutter/material.dart';
    2. class AlignExample extends StatefulWidget {
    3. @override
    4. _AlignExampleState createState() => _AlignExampleState();
    5. }
    6. class _AlignExampleState extends State<AlignExample> {
    7. @override
    8. Widget build(BuildContext context) {
    9. return Scaffold(
    10. appBar: AppBar(
    11. title: Text("AlignExample"),
    12. ),
    13. body: Center(
    14. child: Column(
    15. mainAxisAlignment: MainAxisAlignment.center,
    16. children: [
    17. Container(
    18. width: 200,
    19. height: 200,
    20. color: Colors.blueAccent,
    21. child: Align(
    22. alignment: Alignment.topRight,
    23. widthFactor: 100.0,
    24. heightFactor: 2.0,
    25. child: Text("Align"),
    26. ),
    27. )
    28. ],
    29. ),
    30. ),
    31. );
    32. }
    33. }

    AlignmentGeometry介绍

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

    Alignment详解

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

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

     Alignment属性

    1. /// 顶部左侧对齐
    2. static const Alignment topLeft = Alignment(-1.0, -1.0);
    3. /// 顶部中间对齐
    4. static const Alignment topCenter = Alignment(0.0, -1.0);
    5. /// 顶部右侧对齐
    6. static const Alignment topRight = Alignment(1.0, -1.0);
    7. /// 中间左侧对齐
    8. static const Alignment centerLeft = Alignment(-1.0, 0.0);
    9. /// 垂直居中对齐
    10. static const Alignment center = Alignment(0.0, 0.0);
    11. /// 中间右侧对齐
    12. static const Alignment centerRight = Alignment(1.0, 0.0);
    13. /// 底部左侧对齐
    14. static const Alignment bottomLeft = Alignment(-1.0, 1.0);
    15. /// 底部中间对齐
    16. static const Alignment bottomCenter = Alignment(0.0, 1.0);
    17. /// 底部右侧对齐
    18. static const Alignment bottomRight = Alignment(1.0, 1.0);

    AnimatedAlign构造函数

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

    AnimatedAlign完整示例代码

    1. import 'package:flutter/material.dart';
    2. class AnimatedAlignExample extends StatefulWidget {
    3. @override
    4. _AnimatedAlignExampleState createState() => _AnimatedAlignExampleState();
    5. }
    6. class _AnimatedAlignExampleState extends State<AnimatedAlignExample> {
    7. bool selected = false;
    8. @override
    9. Widget build(BuildContext context) {
    10. return Scaffold(
    11. appBar: AppBar(
    12. title: Text("AnimatedAlignExample"),
    13. ),
    14. body: Center(
    15. child: Column(
    16. mainAxisAlignment: MainAxisAlignment.center,
    17. children: [
    18. Container(
    19. width: 250.0,
    20. height: 250.0,
    21. color: Colors.green,
    22. child: AnimatedAlign(
    23. alignment: selected ? Alignment.topRight : Alignment.bottomLeft,
    24. duration: Duration(milliseconds: 1000),
    25. curve: Curves.fastOutSlowIn,
    26. child: Icon(Icons.ac_unit, size: 40,),
    27. widthFactor: 2.0,
    28. heightFactor: 2.0,
    29. onEnd: () {
    30. print("动画结束时进入");
    31. },
    32. ),
    33. ),
    34. ElevatedButton(
    35. child: Text('改变alignment参数'),
    36. onPressed: () {
    37. setState(() {
    38. selected = !selected;
    39. });
    40. }),
    41. ],
    42. ),
    43. ),
    44. );
    45. }
    46. }

    总结

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

  • 相关阅读:
    c# -- 初始.Net
    python sqlalchemy(ORM)- 01 简单使用
    八种button的hover效果
    在Centos7.9_2207安装CDH6.3.2
    柯桥英语口语学校,商务英语BEC考前须知
    Linux开发——GCC编译器(八)
    SAP Client Copy 参数文件
    弹出框,使用树结构查询数据
    Jenkins 脚本命令行应用总结
    ahook-状态切换hooks-useToggle
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/127967208