Flex Expanded
Flex 組件通常與 Expanded 搭配。
Flex:
| 屬性 | 說明 |
|---|---|
| direction方向 | Axis.vertical垂直,Axis.horizontal水平 |
| mainAxisAlignment主軸 | 跟 direction 一樣 |
| crossAxisAlignment交叉軸 | direction的相反 |
| children | 子元素 |
Expanded flex屬性,等比例分配
以下direction方向是垂直,二個Expanded組件的flex為1,代表均分。
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
import 'package:flutter/material.dart';
void main() {
runApp(MainPage());
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: Flex(
direction: Axis.vertical,
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red, width: 100, height: 100)),
Expanded(
flex: 1,
child: Container(color: Colors.green, width: 100, height: 100))
],
),
),
));
}
}

以下direction為水平,二個Expanded組件占的空間為1:2,也就是紅色占1/3,綠色占2/3。
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
import 'package:flutter/material.dart';
void main() {
runApp(MainPage());
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: Flex(
direction: Axis.horizontal,
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red, width: 100, height: 100)),
Expanded(
flex: 2,
child: Container(color: Colors.green, width: 100, height: 100))
],
),
),
));
}
}
