【笔记】Flutter的Container容器组件

前言

Flutter的Container容器组件学习笔记

Container容器组件

设置容器宽高

double.infinty:真无穷大值,占满屏幕
double.maxFinite:理论上很大的值,占满屏幕

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: double.infinty,
height: 100,
),
);
}
}

设置容器背景颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: const BoxDecoration(
color: Colors.red
),
),
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
color: Colors.red,
),
);
}
}

设置容器内元素的对齐方式

元素的位置

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
alignment: Alignment.center,
),
);
}
}

设置容器内文字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Text(
"文本内容"
),
),
);
}
}

设置边框

设置边框颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red
),
),
),
);
}
}

设置边框粗细

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 2
),
),
),
);
}
}

设置边框圆角

100:圆角率,100就是正圆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100)
),
),
);
}
}

设置边框阴影

设置阴影大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(
blurRadius: 10.0
)
]
),
),
);
}
}
设置阴影颜色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(
color: Colors.blue,
)
]
),
),
);
}
}

设置背景颜色渐变

线性渐变

colors:渐变中的颜色
begin:渐变开始位置,默认为左
end:渐变结束位置,默认为右

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.red, Colors.yellow],
begin: Alignment.centerLeft,
end: Alignment.centerRight
)
),
),
);
}
}

镜像渐变

  • 从中间向外渐变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red
),
gradient: RadialGradient(
colors: [Colors.red, Colors.yellow]
)
),
),
);
}
}

设置边距

外边距

同时设置四周外边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.all(10),
),
);
}
}
分别设置四周外边距
  • 依次设置左、上、右、下的外边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
),
);
}
}
设置单边外边距
  • 仅设置单边的外边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.only(
left: 10,
right: 10,
top: 10,
bottom: 10,
),
),
);
}
}

内边距

同时设置四周内边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
padding: const EdgeInsets.all(10),
),
);
}
}
分别设置四周内边距
  • 依次设置左、上、右、下的内边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
),
);
}
}
设置单边内边距
  • 仅设置单边的内边距
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
padding: const EdgeInsets.only(
left: 10,
right: 10,
top: 10,
bottom: 10,
),
),
);
}
}

2D变换

位移

  • 依次设置x轴、y轴、z轴位移
  • 位移距离可以是正数,也可以是复数
    • 如果是正数,则x轴、y轴、z轴分别向右、下、顶位移
    • 如果是负数,则x轴、y轴、z轴分别向左、上、底位移
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
transform: Matrix4.translationValues(10, 10, 10),
),
);
}
}

旋转

  • 可以是正数小数,也可以是负数小数
    • 如果是正数,则顺时针旋转
    • 如果是负数,则逆时针旋转

rotationX:沿x轴旋转
rotationY:沿y轴旋转
rotationZ:沿z轴旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
transform: Matrix4.rotationX(10),
),
);
}
}

倾斜

skewX:沿x轴倾斜
skewY:沿y轴倾斜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
transform: Matrix4.skewX(10)
),
);
}
}

通过Container容器定义一个按钮组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 200,
height: 40,
alignment: Alignment.center,
margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(10)),
child: const Text(
"按钮",
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
),
);
}
}

完成

参考文献

哔哩哔哩——筱筱知晓