飞道的博客

Flutter组件--Padding和AnimatedPadding

378人阅读  评论(0)

示意图:

Padding介绍

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

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

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


Padding属性和说明

序列号 字段 属性 描述
1 padding EdgeInsetsGeometry 给子widget的间距
2 child Widget 子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: <Widget>[
  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. }


转载:https://blog.csdn.net/eastWind1101/article/details/127966941
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场