8

【笔记】Flutter 的 Align 对齐组件

 1 year ago
source link: https://loli.fj.cn/2023/06/25/Flutter%E7%9A%84Align%E5%AF%B9%E9%BD%90%E7%BB%84%E4%BB%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Flutter 的 Align 对齐组件学习笔记

Align 对齐组件

alignment:子元素的位置

Alignment.topLeft:左上
Alignment.topCenter:上
Alignment.topRight:右上
Alignment.centerLeft:左
Alignment.center:中
Alignment.centerRight:右
Alignment.bottomLeft:左下
Alignment.bottomCenter:下
Alignment.bottomRight:右下

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: const App(),
),
));
}

class App extends StatelessWidget {
const App({super.key});

@override
Widget build(BuildContext context) {
return Container(
width: 200,
height: 200,
color: Colors.red,
child: Align(
alignment: Alignment.center,
child: Container(
width: 100,
height: 100,
color: Colors.yellow,
),
),
);
}
}

自定义子元素的位置

  • 通过给定 x 轴和 y 轴的坐标,实现自定义子元素的位置
    • x 轴和 y 轴的取值范围为 [0,1]
    • 左上角为原点,x 轴向右为正方向,y 轴向下为正方向
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: const App(),
),
));
}

class App extends StatelessWidget {
const App({super.key});

@override
Widget build(BuildContext context) {
return Container(
width: 200,
height: 200,
color: Colors.red,
child: Align(
alignment: const Alignment(1, 1),
child: Container(
width: 100,
height: 100,
color: Colors.yellow,
),
),
);
}
}

哔哩哔哩 —— 筱筱知晓


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK