• Flutter组件--Padding和AnimatedPadding


    示意图:

    Padding介绍

    在应用程序中有许多widget 时,这个时候画面常常会变得很拥挤,这个时候如果想要在widget之间来保留一些间距,那就用 Padding

    为什么使用 Padding 而不使用 Container.padding 属性的 Container?

    Container 是将许多更简单的 widget 组合在一个方便的包中,如果只需要设置 padding ,那我们最好使用 Padding 而不是 Container


    Padding属性和说明

    序列号字段属性描述
    1paddingEdgeInsetsGeometry给子widget的间距
    2childWidget子widget

    Padding属性详细使用

    1、padding 、child

    padding 给子widget的间距

    child 接收一个子 Widget

    1. import 'package:flutter/material.dart';
    2. class PaddingExample extends StatefulWidget {
    3. @override
    4. _PaddingExampleState createState() => _PaddingExampleState();
    5. }
    6. class _PaddingExampleState extends State<PaddingExample> {
    7. @override
    8. Widget build(BuildContext context) {
    9. return Scaffold(
    10. appBar: AppBar(
    11. title: Text("Padding example"),
    12. ),
    13. body: Center(
    14. child: Column(
    15. mainAxisAlignment: MainAxisAlignment.center,
    16. children: [
    17. Padding(
    18. padding: EdgeInsets.all(0),
    19. child: Container(
    20. width: 100,
    21. height: 100,
    22. color: Colors.red,
    23. ),
    24. ),
    25. Padding(
    26. padding: EdgeInsets.all(0),
    27. child: Container(
    28. width: 100,
    29. height: 100,
    30. color: Colors.green,
    31. ),
    32. ),
    33. Padding(
    34. padding: EdgeInsets.all(0),
    35. child: Container(
    36. width: 100,
    37. height: 100,
    38. color: Colors.orange,
    39. ),
    40. )
    41. ],
    42. ),
    43. ),
    44. );
    45. }
    46. }

     

    EdgeInsetsGeometry详解

    EdgeInsetsGeometry 是一个描述边距的组件,一般都是使用它的子类 EdgeInsets 来进行设置。

    1、fromLTRB

    设置左、上、右、下的边距,可设定不同的值

    1. Padding(
    2. padding: EdgeInsets.fromLTRB(10, 20, 30, 40),
    3. child: Container(
    4. width: 100,
    5. height: 100,
    6. color: Colors.red,
    7. ),
    8. ),

     2、all

    同时设置所有的边距为同一个值

    1. Padding(
    2. padding: EdgeInsets.all(10),
    3. child: Container(
    4. width: 100,
    5. height: 100,
    6. color: Colors.green,
    7. ),
    8. )

     3、only

    根据需要设置某一个边的间距

    1. Padding(
    2. padding: EdgeInsets.only(
    3. left: 10,
    4. right: 10
    5. ),
    6. child: Container(
    7. width: 100,
    8. height: 100,
    9. color: Colors.orange,
    10. ),
    11. )

     4、symmetric

    设置水平(上下)、或者垂直(左右)的间距

    1. Padding(
    2. padding: EdgeInsets.symmetric(
    3. vertical: 10,
    4. horizontal: 10
    5. ),
    6. child: Container(
    7. width: 100,
    8. height: 100,
    9. color: Colors.orange,
    10. ),
    11. )

     完整代码

    1. import 'package:flutter/material.dart';
    2. class PaddingExample extends StatefulWidget {
    3. @override
    4. _PaddingExampleState createState() => _PaddingExampleState();
    5. }
    6. class _PaddingExampleState extends State<PaddingExample> {
    7. @override
    8. Widget build(BuildContext context) {
    9. return Scaffold(
    10. appBar: AppBar(
    11. title: Text("Padding example"),
    12. ),
    13. body: Center(
    14. child: Column(
    15. mainAxisAlignment: MainAxisAlignment.center,
    16. children: [
    17. Padding(
    18. padding: EdgeInsets.fromLTRB(10, 20, 30, 40),
    19. child: Container(
    20. width: 100,
    21. height: 100,
    22. color: Colors.red,
    23. ),
    24. ),
    25. Padding(
    26. padding: EdgeInsets.all(10),
    27. child: Container(
    28. width: 100,
    29. height: 100,
    30. color: Colors.green,
    31. ),
    32. ),
    33. Padding(
    34. padding: EdgeInsets.only(
    35. left: 10,
    36. right: 10
    37. ),
    38. child: Container(
    39. width: 100,
    40. height: 100,
    41. color: Colors.orange,
    42. ),
    43. ),
    44. Padding(
    45. padding: EdgeInsets.symmetric(
    46. vertical: 10,
    47. horizontal: 10
    48. ),
    49. child: Container(
    50. width: 100,
    51. height: 100,
    52. color: Colors.orange,
    53. ),
    54. )
    55. ],
    56. ),
    57. ),
    58. );
    59. }
    60. }

    AnimatedPadding介绍

    AnimatedPadding构造函数

    1. AnimatedPadding({
    2. Key? key,
    3. required this.padding, // 边距
    4. this.child, // 子Widget
    5. Curve curve = Curves.linear, // 动画的运动速率
    6. required Duration duration, // 动画的持续时间
    7. VoidCallback? onEnd, // 动画结束时的回调
    8. }) : assert(padding != null),
    9. assert(padding.isNonNegative),
    10. super(key: key, curve: curve, duration: duration, onEnd: onEnd);

    AnimatedPadding完整示例代码

    1. import 'package:flutter/material.dart';
    2. class AnimatedPaddingExample extends StatefulWidget {
    3. @override
    4. _AnimatedPaddingExampleState createState() => _AnimatedPaddingExampleState();
    5. }
    6. class _AnimatedPaddingExampleState extends State<AnimatedPaddingExample> {
    7. double paddingAllValue = 0.0;
    8. @override
    9. Widget build(BuildContext context) {
    10. return Scaffold(
    11. appBar: AppBar(
    12. title: Text("AnimatedPaddingExample"),
    13. ),
    14. body: Column(
    15. mainAxisAlignment: MainAxisAlignment.center,
    16. children: [
    17. Text('Padding: $paddingAllValue'),
    18. AnimatedPadding(
    19. padding: EdgeInsets.all(paddingAllValue),
    20. duration: Duration(milliseconds: 1000),
    21. curve: Curves.easeInOut,
    22. child: Container(
    23. width: MediaQuery.of(context).size.width,
    24. height: MediaQuery.of(context).size.height / 4,
    25. color: Colors.blue,
    26. ),
    27. onEnd: () {
    28. print("动画结束时的回调");
    29. },
    30. ),
    31. ElevatedButton(
    32. child: Text('改变padding的值'),
    33. onPressed: () {
    34. setState(() {
    35. paddingAllValue = paddingAllValue == 0.0 ? 50.0 : 0.0;
    36. });
    37. }),
    38. ],
    39. ),
    40. );
    41. }
    42. }

  • 相关阅读:
    面试算法 二叉树的遍历,方法递归,前序遍历: 中序遍历: 后序遍历: 层序遍历
    【SwitchyOmega】SwitchyOmega 安装及使用
    docker运行javaWeb服务,操作文件异常
    MySQL进阶篇2-索引的创建和使用
    RabbitMQ(四):RabbitMQ高级特性
    深度学习在计算机视觉领域的最新进展
    内核开发-同步场景与概念
    金仓数据库KingbaseES客户端编程接口指南-ado.net(6. 安全和加密)
    SpringBoot是如何做到自动装配的
    Java 中的 void 和 Kotlin 的 Unit
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/127966941