当前位置:网站首页>【Flutter -- 布局】Align、Center、Padding 使用详解
【Flutter -- 布局】Align、Center、Padding 使用详解
2022-07-26 09:16:00 【Kevin-Dev】

本文主要介绍 Flutter 布局中的 Padding 、Align 以及 Center 控件。
Align
1. 简介
在其他端的开发,Align 一般都是当做一个控件的属性,并没有拿出来当做一个单独的控件。Align 本身实现的功能并不复杂,设置 child 的对齐方式,例如居中、居左居右等,并根据 child 尺寸调节自身尺寸。
2. 属性
alignment:对齐方式,一般会使用系统默认提供的9种方式,但是并不是说只有这9种,例如如下的定义。系统提供的9种方式只是预先定义好的。
widthFactor:宽度因子,如果设置的话,Align的宽度就是child的宽度乘以这个值,不能为负数。
heightFactor:高度因子,如果设置的话,Align的高度就是child的高度乘以这个值,不能为负数。
3. 实例
1. 效果图
2. 代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Align'),
),
body: Container(
height: 120.0,
width: 120.0,
color: Colors.blue.shade50,
child: Align(
alignment: Alignment.topRight,
child: FlutterLogo(
size: 60,
),
),
)
)
);
}
}
Center
1. 简介
Center 继承自 Align,只不过是将 alignment 设置为 Alignment.center,其他属性例如 widthFactor、heightFactor,布局行为,都与Align完全一样,在这里就不再单独做介绍了。Center源码如下,没有设置 alignment 属性,是因为 Align 默认的对齐方式就是居中。
class Center extends Align {
/// Creates a widget that centers its child.
const Center({
Key key, double widthFactor, double heightFactor, Widget child })
: super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
}
Padding
1. 简介
Padding在Flutter中用的也挺多的,作为一个基础的控件,功能非常单一,给子节点设置padding属性。写过其他端的都了解这个属性,就是设置内边距属性,内边距的空白区域,也是widget的一部分。
2. 属性
padding:padding的类型为EdgeInsetsGeometry,EdgeInsetsGeometry是EdgeInsets以及EdgeInsetsDirectional的基类。在实际使用中不涉及到国际化,例如适配阿拉伯地区等,一般都是使用EdgeInsets。EdgeInsetsDirectional光看命名就知道跟方向相关,因此它的四个边距不限定上下左右,而是根据方向来定的。
3. 示例代码
new Padding(
padding: new EdgeInsets.all(8.0),
child: const Card(child: const Text('Hello World!')),
)
边栏推荐
- 力扣链表题
- 性格测试系统v1.0
- JS - DataTables control on the number of displays per page
- 【线上问题】Timeout waiting for connection from pool 问题排查
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- Processing of inconsistent week values obtained by PHP and MySQL
- Cat安装和使用
- Study notes of automatic control principle -- correction and synthesis of automatic control system
- 原根与NTT 五千字详解
- PAT 甲级 A1034 Head of a Gang
猜你喜欢

Uploading pictures on Alibaba cloud OSS

十大蓝筹NFT近半年数据横向对比

Cat安装和使用

volatile 靠的是MESI协议解决可见性问题?(下)

围棋智能机器人阿法狗,阿尔法狗机器人围棋

Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain

ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知

jvm命令归纳

The essence of attack and defense strategy behind the noun of network security

优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5
随机推荐
187. Repeated DNA sequence
MySQL 强化知识点
zsh: command not found: nvm
The essence of attack and defense strategy behind the noun of network security
堆外内存的使用
QtCreator报错:You need to set an executable in the custom run configuration.
Redis principle and usage - installation and distributed configuration
语音聊天app源码——钠斯直播系统源码
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
Simple message mechanism of unity
760. 字符串长度
redis原理和使用-安装和分布式配置
Study notes of automatic control principle -- correction and synthesis of automatic control system
Object 的Wait Notify NotifyAll 源码解析
分布式跟踪系统选型与实践
Probability model in machine learning
Grain College of all learning source code
2B和2C
Summary of common activation functions for deep learning
对标注文件夹进行清洗