Expanded와 Flexible
요거 두개 굉장히 헷갈린다, 그렇다는건 Flutter 로 취업을 하고자 할때 자주 나올수있는 질문이라는것이다ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
Flexible
-Inheritance
Object → DiagnosticableTree → Widget → ProxyWidget → ParentDataWidget → Flexible
-Properties
class FlexibleDemo extends StatelessWidget {
const FlexibleDemo({Key key}) : super(key: key);
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('test'),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Flexible(
fit: FlexFit.tight,
child: YellowBox(
title: 'YellowBox',
),
),
BlueBox(title: 'BlueBox'),
],
),
),
),
);
}
}
class YellowBox extends StatelessWidget {
final String title;
const YellowBox({Key key, this.title}) : super(key: key);
Widget build(BuildContext context) {
return Container(
//width: 50,
height: 100,
color: Colors.yellow,
child: Text(title),
);
}
}
class BlueBox extends StatelessWidget {
final String title;
const BlueBox({
this.title,
Key key,
}) : super(key: key);
Widget build(BuildContext context) {
return Container(
//width: 50,
height: 100,
color: Colors.blue,
child: Text(title),
);
}
}
둘의 크기는 기본적으로 같게해놨으나 FlexFit.tight를 쓴 위젯은 남은공간을 모두 차지하게 되었다.
flexFit.loose : 기본축의 남은 공간을 채우도록 하되 필요한 공간만 차지하도록 합니다.
기본값과 같은 상태가 된다.
flex: Column 혹은 Row 기본축의 남은공간을 flex 값 / 전체 자식들의 flex의 합만큼 차지하도록 합니다.
class FlexibleDemo extends StatelessWidget {
const FlexibleDemo({Key key}) : super(key: key);
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('test'),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Flexible(
flex: 5,
child: YellowBox(
title: 'YellowBox',
),
),
Flexible(
flex: 1,
child: BlueBox(title: 'BlueBox'),
),
Flexible(
flex: 1,
child: YellowBox(
title: 'yellowbox',
),
),
],
),
),
),
);
}
}
class YellowBox extends StatelessWidget {
final String title;
const YellowBox({Key key, this.title}) : super(key: key);
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: Colors.yellow,
child: Text(title),
);
}
}
class BlueBox extends StatelessWidget {
final String title;
const BlueBox({
this.title,
Key key,
}) : super(key: key);
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: Colors.blue,
child: Text(title),
);
}
}
Expanded
-Inheritance
Object → DiagnosticableTree → Widget → ProxyWidget → ParentDataWidget → Expanded
class FlexibleDemo extends StatelessWidget {
const FlexibleDemo({Key key}) : super(key: key);
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('test'),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: YellowBox(
title: 'YellowBox',
),
),
BlueBox(title: 'BlueBox'),
YellowBox(
title: 'yellowbox',
),
],
),
),
),
);
}
}
class YellowBox extends StatelessWidget {
final String title;
const YellowBox({Key key, this.title}) : super(key: key);
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: Colors.yellow,
child: Text(title),
);
}
}
class BlueBox extends StatelessWidget {
final String title;
const BlueBox({
this.title,
Key key,
}) : super(key: key);
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: Colors.blue,
child: Text(title),
);
}
}
동일한 크기의 위젯이지만 Expanded를 사용한 YellowBox가 남은 위젯의 여백으로 최대한 확장되었다.