Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[flutter] 21๊ฐ• | Column, Row ์œ„์ ฏ ๋ณธ๋ฌธ

flutter/์ˆœํ•œ๋ง›(๊ธฐ์ดˆ)

[flutter] 21๊ฐ• | Column, Row ์œ„์ ฏ

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2021. 8. 16. 12:07

โœ… ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Column ์œ„์ ฏ๊ณผ Row ์œ„์ ฏ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ์˜ˆ์ •์ด์•ผ

์‰ฌ์šฐ๋‹ˆ๊นŒ ์“ฑ ๋ณด๊ธฐ

 

โญ๏ธ ๋ ˆ์ด์•„์›ƒ์„ ํ•™์Šต์ด๋‚˜ ๊ธ‰ํ•  ๋•Œ, ํ…Œ์ŠคํŠธ ํ•˜์ง€ ์•Š๊ณ  ์ด๋ ‡๊ฒŒ ๋˜๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์‚ฌ์ดํŠธ๊ฐ€ ์žˆ๋‹ค.

https://medium.com/flutter-community/flutter-layout-cheat-sheet-5363348d037e

 

Flutter Layout Cheat Sheet

Do you need simple layout samples for Flutter? I present you my set of Flutter layout code snippets. I will keep it short, sweet and simple…

medium.com

 

โœ… Columns ์œ„์ ฏ์€ ์„ธ๋กœ์ถ•์˜ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ธธ์ด๋ฅผ ๊ฐ–๊ณ  ๊ฐ€๋กœ์ถ•์˜ ๊ธธ์ด๋งŒ ์ œํ•œ๋œ๋‹ค.

import 'package:flutter/material.dart'; // ๋ฐ์Šคํฌํƒ‘, ์•ฑ ๋“ฑ์— ๊ณ ๋ฃจ UI๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ตฌ๊ธ€์ด ์ œ๊ณตํ•ด์ฃผ๋Š” ํŒจํ‚ค

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // ์‹ค์งˆ์ ์œผ๋กœ ๋ชจ๋“  ์•ฑ์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋‹ค.
      title: 'Appbar', // ์•ฑ์„ ์ด์นญํ•˜๋Š” ์ด๋ฆ„ -> ์Šค๋งˆํŠธ ํฐ ์•ฑ์—์„œ ์ตœ๊ทผ ์‚ฌ์šฉํ•œ ์•ฑ ๋ณด์—ฌ์ค„ ๋•Œ์˜ ์ด๋ฆ„
      theme:
          ThemeData(primarySwatch: Colors.green // ํŠน์ •์ƒ‰์˜ ์‘์šฉ๋“ค์„ ๊ธฐ๋ณธ ์ƒ‰์ƒ์œผ๋กœ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฒ 
              ),
      home: MyPage(), // home์€ ์•ฑ์ด ์‹คํ–‰๋  ๋•Œ ๊ฐ€์žฅ๋จผ์ € ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด
    );
  }
}

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // ์นผ๋Ÿผ์˜ ์ค‘์•™์— ์œ„์น˜
            children: [
              Container(
                width: 100,
                height : 100,
                color : Colors.green,
                child: Text('container 1'),
              ),
              Container(
                width: 100,
                height : 100,
                color : Colors.white,
                child: Text('container 2'),
              ),
              Container(
                width: 100,
                height : 100,
                color : Colors.red,
                child: Text('container 3'),
              ),

            ],
          ),
        ),

      ),
    );
  }
}

์™ผ์ชฝ : ์„ผํ„ฐ ์œ„์ ฏ์œผ๋กœ ๊ฐ์‹ธ์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๊ฐ€์šด๋ฐ : ์„ผํ„ฐ ์œ„์ ฏ๋งŒ ์ ์šฉ, ์˜ค๋ฅธ์ชฝ : ์„ผํ„ฐ ์œ„์ ฏ๊ณผ ์นผ๋Ÿผ์— mainAxisAlignment ์ ์šฉ 

๊ฐ€์šด๋ฐ ์‚ฌ์ง„์€ ์„ผํ„ฐ๋ฅผ ํ–ˆ์Œ์—๋„ Column ์œ„์ ฏ์ด ์„ธ๋กœ์ถ•์„ ๋‹ค ์ฐจ์ง€ํ•ด์„œ Center ์œ„์ ฏ์ด ์„ธ๋กœ์ถ•์„ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ณด์—ฌ์ฃผ์ง€ ๋ชปํ•œ๋‹ค.

 

โœ… ๊ทธ๋ ‡๋‹ค๋ฉด Column ์œ„์ ฏ์ด ์„ธ๋กœ์ถ•์„ ๋‹ค ์ฐจ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

 

mainAxisSize : MainAxisSize.min ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Center(
          child: Column(
            //mainAxisAlignment: MainAxisAlignment.center, // ์นผ๋Ÿผ์˜ ์ค‘์•™์— ์œ„์น˜
            mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ์ด ์ „์ฒด ๋‹ค ์ฐจ์ง€ ์•ˆํ•˜๋„๋ก ๋งŒ๋“ค๊ธฐ
            children: [
              Container(
                width: 100,
                height : 100,
                color : Colors.green,
                child: Text('container 1'),
              ),
              Container(
                width: 100,
                height : 100,
                color : Colors.white,
                child: Text('container 2'),
              ),
              Container(
                width: 100,
                height : 100,
                color : Colors.red,
                child: Text('container 3'),
              ),

            ],
          ),
        ),

      ),
    );
  }
}

์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ

 

โœ… ์ด๋ฒˆ์—๋Š” verticalDirection๋ฅผ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ๋˜๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),
          ],
        ),
      ),
    );
  }
}

์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ

 

โœ…  ๋‹ค๋ฅธ ์†์„ฑ์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด์ž

mainAxisAlignment

 - MainAxisAlignment.spaceEvenly : ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ ( ์ฒ˜์Œ์ด๋ž‘ ๋ ๊ณต๊ฐ„๋„ ํฌํ•จ )

 - MainAxisAlignment.spaceBetween : ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ ( ์ฒ˜์Œ์ด๋ž‘ ๋ ๊ณต๊ฐ„์€ ์ œ์™ธ )

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          //verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          //mainAxisAlignment : MainAxisAlignment.spaceEvenly, // ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ
          mainAxisAlignment: MainAxisAlignment.spaceBetween , // ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ
         
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),
          ],
        ),
      ),
    );
  }
}

 

์ขŒ spaceEvenly , ์šฐ spaceBetween

 

crossAxisAlignment

 - CrossAxisAlignment.end : ์ปจํ…Œ์ด๋„ˆ์˜ ๋์— ๋งž๊ฒŒ ์ •๋ ฌ 

๐Ÿ’ก ํ”Œ๋Ÿฌํ„ฐ ์—์„œ๋Š” width๊ฐ€ ํ™”๋ฉด์˜ ํฌ๊ธฐ ๋ฐ–์œผ๋กœ ๋ฒ—์–ด๋‚˜๋„ ๊ทธ ์ด์ƒ์œผ๋กœ ์ปค์ง€์ง€ ์•Š๊ณ  ์•Œ์•„์„œ ํ™”๋ฉด ๋ ํฌ๊ธฐ์— ๋งž์ถ”๊ฒŒ ๋œ๋‹ค.

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          //verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          //mainAxisAlignment : MainAxisAlignment.spaceEvenly, // ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ
          //mainAxisAlignment: MainAxisAlignment.spaceBetween , // ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ
          //crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),
          ],
        ),
      ),
    );
  }
}

crossAxisAlignment.end

 

- CrossAxisAlignment.stretch : ํ™”๋ฉด์˜ ๊ฐ€๋กœ์ถ•์„ ๊ฐ€๋Šฅํ•œํ•œ ์ตœ๋Œ€๋กœ ๋Š˜๋ฆผ

๐Ÿ”ธ width๋Š” ๋ฌด์‹œ๋œ๋‹ค. ์ฆ‰, ์ ์šฉ ์•ˆ๋ผ 

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          //verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          //mainAxisAlignment : MainAxisAlignment.spaceEvenly, // ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ
          //mainAxisAlignment: MainAxisAlignment.spaceBetween , // ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ
          //crossAxisAlignment: CrossAxisAlignment.end, // ์นผ๋Ÿผ์˜ ๋์— ๋งž์ถค
          crossAxisAlignment: CrossAxisAlignment.stretch, // ์นผ๋Ÿผ์˜ ๊ฐ€๋กœ์ถ•์„ ์ตœ๋Œ€๋กœ - ๋Œ€์‹  width ์ ์šฉ์•ˆ๋ผ
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),

          ],
        ),
      ),
    );
  }
}

 

CrossAxisAlignment.stretch

 

โœ… invisible Container ํ™œ์šฉํ•˜๊ธฐ

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          //verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          //mainAxisAlignment : MainAxisAlignment.spaceEvenly, // ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ
          //mainAxisAlignment: MainAxisAlignment.spaceBetween , // ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ
          //crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),
            Container( // invisible Container
              width: double.infinity, // ๊ฐˆ ์ˆ˜ ์žˆ๋Š”๋ฐ๊นŒ์ง€ ๊ฐ€๋กœ์ถ•์œผ๋กœ ํ™•์žฅํ•˜๋ผ
              height: 20,
            )
          ],
        ),
      ),
    );
  }
}

invisible container ๋ฅผ ํ†ตํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋๊นŒ์ง€ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

โ—๏ธ๊ทผ๋ฐ ์ด ๊ฐœ๋…์ด ์กฐ๊ธˆ ์ƒ์†Œํ•ด์„œ ์ถ”์ƒ์ ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋” ์‰ฝ๊ฒŒ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

invisible Container ์‚ฌ์šฉ

 

โœ… SizedBox ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ„๊ฒฉ ์ฃผ๊ธฐ

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: SafeArea(
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center, // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
          //mainAxisSize: MainAxisSize.min, // ์นผ๋Ÿผ ์‚ฌ์ด์ฆˆ ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ด์ฆˆ ๋งŒํผ
          //verticalDirection: VerticalDirection.up, // ์•„๋ž˜๋ถ€ํ„ฐ ์ •๋ ฌ
          //mainAxisAlignment : MainAxisAlignment.spaceEvenly, // ์นผ๋Ÿผ์—์„œ ๊ฐ™์€ ๊ฐ„๊ฒฉ๋งŒํผ ๋„์›Œ์„œ
          //mainAxisAlignment: MainAxisAlignment.spaceBetween , // ๊ณต๊ฐ„๋งŒ ์นผ๋Ÿผ์—์„œ ๋‚˜๋ˆ ์„œ
          //crossAxisAlignment: CrossAxisAlignment.end, // ์นผ๋Ÿผ์˜ ๋์— ๋งž์ถค
          crossAxisAlignment: CrossAxisAlignment.stretch, // ์นผ๋Ÿผ์˜ ๊ฐ€๋กœ์ถ•์„ ์ตœ๋Œ€๋กœ - ๋Œ€์‹  width ์ ์šฉ์•ˆ๋ผ
          children: [
            Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Text('container 1'),
            ),
            SizedBox(
              height: 30,
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Text('container 2'),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text('container 3'),
            ),

          ],
        ),
      ),
    );
  }
}

SizedBox

Row์—์„œ๋Š” height -> width๋กœ ๋ฐ”๊พผ๋‹ค.

 

โœ… Row๋Š” Column ๊ณผ ๋ฐ˜๋Œ€์ด๋ฏ€๋กœ ์œ„์˜ ์„ค๋ช…์„ ๋ณด๊ณ  ๋ฐ˜๋Œ€๋กœ ์ ์šฉํ•œ๋‹ค.

Comments