Layout의 기본적인 요구사항
- 위젯이 어떻게 배치되는가
- ex)위젯의 상하 배치
예시
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Placeholder(
child: Text('Place holer'),
);
}
}
결과

위젯 위아래로 배치하기
- column을 주로 사용
- column 안에 container 들을 이용하여 각각의 width와 height등을 사용해 각 container의 높이와 너비등을 조정가능
예시
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
width: 100,
height: 80,
child: Text('container 1'),
color: Colors.red,),
Container(
width: 100,
height: 80,
child: Text('container 2'),
color: Colors.green),
Container(
width: 100,
height: 80,
child: Text('container 3'),
color: Colors.blue),
],
);
}
}
결과

만약 column의 위치를 container안에 있게 한다면
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: double.infinity,
color: Colors.grey,
child: Column(
children: [
Container(
width: 100,
height: 80,
child: Text('container 1'),
color: Colors.red,),
Container(
width: 100,
height: 80,
child: Text('container 2'),
color: Colors.green),
Container(
width: 100,
height: 80,
child: Text('container 3'),
color: Colors.blue),
],
),
);
}
}
이런식으로 코드를 작성할 수 있는데 이렇게 되면 container 1,2,3들이 가운데로 이동하게 되고 배경이 회색이 된다.
결과

만약에 이 상태로 container들을 완전 한 가운데로 옮기고 싶다면 코드 한 줄을 추가하면 된다
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: double.infinity,
color: Colors.grey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 100,
height: 80,
child: Text('container 1'),
color: Colors.red,),
Container(
width: 100,
height: 80,
child: Text('container 2'),
color: Colors.green),
Container(
width: 100,
height: 80,
child: Text('container 3'),
color: Colors.blue),
],
),
);
}
}
결과

container를 맨 왼쪽 혹은 맨 오른쪽으로 보내고싶다면
- 'crossAxisAlignment: CrossAxisAlignment.start,' 추가 //시작지점(맨 왼쪽)부터
- 'crossAxisAlignment: CrossAxisAlignment.end,' 추가 //끝(맨 오른쪽) 부터
container를 상하로 쌓는게 아닌 좌우로 쌓고싶다면
- 코드에 보이는 'child: Column(' 이 부분에서 Column을 Row로 바꾸면 된다.
예시
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 좌우로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: double.infinity,
color: Colors.grey,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 100,
height: 80,
child: Text('container 1'),
color: Colors.red,),
Container(
width: 100,
height: 80,
child: Text('container 2'),
color: Colors.green),
Container(
width: 100,
height: 80,
child: Text('container 3'),
color: Colors.blue),
],
),
);
}
}
결과

예시
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하좌우로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: double.infinity,
color: Colors.grey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 100,
height: 80,
child: Text('container 1'),
color: Colors.red,),
Container(
width: 100,
height: 80,
child: Text('container 2'),
color: Colors.green),
Container(
width: 100,
height: 80,
child: Text('container 3'),
color: Colors.blue),
],
),
Container(
width: 300,
height: 120,
color: Colors.purple,
child: Text('container 4'),
)
],
),
);
}
}
결과

Widget을 스크롤하는 방법
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('widget을 상하좌우로 배치하기'),
),
body: Body(),
),
),
);
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
Container(
color: Colors.grey,
width: double.infinity,
height: 100,
margin: EdgeInsets.symmetric(vertical: 8),
),
],
),
);
}
}
결과
