๐Ÿ“šSTUDY/๐ŸŽซDart&Flutter

[flutter] widget 1 : ํ™”๋ฉด ๋ฐฐ์น˜๋ฅผ ์œ„ํ•œ ๋ ˆ์ด์•„์›ƒ

ํ•ด๋Š”์„  2021. 2. 7. 22:22

 

์œ„์ ฏ ์ค‘ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋ณธ ์œ„์ ฏ๋“ค์ธ container, column, row

 

1. Container

ํ•˜๋‚˜์˜ ์ž์‹์„ ๊ฐ€์ง€๋Š” ๋ ˆ์ด์•„์›ƒ ์œ„์ ฏ. ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค. 

Container({
    Key key,
    this.alignment,
    this.padding,
    Color color,
    Decoration decoration,
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
    this.child,
  })

์œ„์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ๊ฐ€์ ธ ๋‹ค์–‘ํ•œ ์‘์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. child๋ฅผ ์ด์šฉํ•ด ๋˜ ๋‹ค๋ฅธ ์œ„์ ฏ์„ ์ž์‹์œผ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

padding์ด๋‚˜ margin ๋ถ€๋ถ„์—์„œ EdgeInsets.only, EdgeInsets.all, EdgeInsets.symmetric ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ์—ฌ๋ฐฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch : Colors.blue,),
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar : AppBar(
        title : Text("hello!"),
      ),
      body : Container(
        color : Colors.red,
        width:150,
        height:150,
      ),
    );
  }
}

 

 

2. Column

์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ์œ„์ ฏ๋“ค์„ ๋ฐฐ์น˜ํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ ์œ„์ ฏ. 

Column({
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })

mainAxisAlignment, crossAxisAlignment, children์ด ์ฃผ์š” ์†์„ฑ.

children์—๋Š” ์—ฌ๋Ÿฌ widget list๋ฅผ ์ง€์ • ๊ฐ€๋Šฅ. column์ด๋ผ ์œ„์ ฏ๋“ค์ด ์„ธ๋กœ๋กœ ๋ฐฐ์—ด๋จ.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch : Colors.blue,),
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar : AppBar(
        title : Text("hello!"),
      ),
      body : Column(
        children: <Widget>[
          Container(
            color : Colors.amber,
            width : 150,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
          
          Container(
            color : Colors.blue,
            width : 150,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
          
          Container(
            color : Colors.yellow,
            width : 150,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
          

        ]
      ),
    );
  }
}

 

 

3. row

์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ์œ„์ ฏ๋“ค์„ ๋ฐฐ์น˜ํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ ์œ„์ ฏ. 

Row({
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })

mainAxisAlignment, crossAxisAlignment, children์ด ์ฃผ์š” ์†์„ฑ.

column๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ children์—๋Š” ์—ฌ๋Ÿฌ widget list๋ฅผ ์ง€์ • ๊ฐ€๋Šฅ. row์ด๋ผ ์œ„์ ฏ๋“ค์ด ๊ฐ€๋กœ๋กœ ๋ฐฐ์—ด๋จ.

 

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch : Colors.blue,),
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar : AppBar(
        title : Text("hello!"),
      ),
      body : Row(
        mainAxisSize : MainAxisSize.max,       
        
        children: <Widget>[
          Container(
            color : Colors.amber,
            width : 100,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
          
          Container(
            color : Colors.blue,
            width : 100,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
          
          Container(
            color : Colors.yellow,
            width : 100,
            height : 100,
            margin : const EdgeInsets.all(8.0),
          ),
        ]
      ),
    );
  }
}

 

 

column๊ณผ row์ฒ˜๋Ÿผ ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š” ์œ„์ ฏ์€ mainAxis์™€ crossAxis ์†์„ฑ์ด ์žˆ๋‹ค. ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•  ๊ฑฐ ์—†์ด column์ด๋ฉด ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์—ดํ•˜๋‹ˆ ์ˆ˜์ง์ด ์ฃผ(main) ์ถ•(axis), ์ˆ˜ํ‰์ด ํšก์ถ• (CrossAxis)์ด ๋œ๋‹ค. row๋ฉด ๋ฐ˜๋Œ€๊ฐ€ ๋˜๊ฒ ๋‹ค.

 

๋” ์ž์„ธํ•œ ์ •๋ ฌ์€ ์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ์กฐํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

beomseok95.tistory.com/310

 

Flutter - Row,Column์ •๋ ฌํ•˜๊ธฐ (MainAxisAlignment, CrossAxisAlignment)

Flutter - Row, Column ์ •๋ ฌํ•˜๊ธฐ (MainAxisAlignment, CrossAxisAlignment) axis๋Š” ์ค‘์‹ฌ์„ ์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. crossaxis ํšก์ถ•, mainaxis๋Š” ์ฃผ์ถ• ์ด๋ผ๋Š” ๋œป์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด MainAxixAlignment์™€ Cro..

beomseok95.tistory.com

 

์•„๋ž˜๋Š” ๊ฐ ์†์„ฑ๋“ค์˜ ์ƒ์ˆ˜์™€ ์˜ˆ์‹œ์ด๋‹ค.

 

MainAxisSize

์‹ค์ œ column์˜ ๊ฒฝ์šฐ, ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์—ด์ด ๋˜์ง€๋งŒ ๊ทธ ์ˆ˜ํ‰์ชฝ์˜ ์—ฌ๋ฐฑ๊นŒ์ง€ ๋‹ค ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋‹ค. ์ด ์†์„ฑ์€ ๊ทธ ์—ฌ๋ฐฑ์˜ ์œ ๋ฌด๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

max : ๋‚จ๋Š” ๊ณต๊ฐ„์„ ๋ชจ๋‘ ์ฐจ์ง€

min : ์œ„์ ฏ์˜ ํฌ๊ธฐ ๋งŒํผ๋งŒ ์ฐจ์ง€

 

MainAxisAlignment

start : ์™ผ์ชฝ ์ •๋ ฌ

center : ๊ฐ€์šด๋ฐ ์ •๋ ฌ

end : ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ

spaceEvenly : ์•ž ๋’ค ๊ท ๋“ฑํ•˜๊ฒŒ ๋ฐฐ์น˜

spaceBetween : ์ฒซ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ์ž์‹์„ ์ œ์™ธํ•œ ๊ฐ„๊ฒฉ์ด ๊ท ๋“ฑ ๋ฐฐ์น˜

spaceAround : ์ฒซ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ์ž์‹์˜ ๊ฐ„๊ฒฉ์„ ๊ทธ ์‚ฌ์ด ๊ฐ„๊ฒฉ๋“ค์˜ ์ ˆ๋ฐ˜์œผ๋กœ ๋ฐฐ์น˜

์™ผ์ชฝ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ start, center, end / evenly, between, around

 

CrossAxisAlignment

start : ์™ผ์ชฝ ์ •๋ ฌ

center : ๊ฐ€์šด๋ฐ ์ •๋ ฌ

end : ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ

baseline : ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๊ฑฐ๋‚˜ ํ• ๋•Œ ๊ธฐ๋ณธ ๋ผ์ธ ์ถ•์— ๋งž์ถฐ ์ •๋ ฌ

stretch : ๋๊นŒ์ง€ ์ฑ„์šฐ๊ธฐ

 

 

4. stack

stack ์œ„์ ฏ์€ children์— ๋‚˜์—ดํ•œ ์—ฌ๋Ÿฌ ์œ„์ ฏ์„ ์ˆœ์„œ๋Œ€๋กœ ์Œ“์ด๊ฒŒ ํ•œ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ row, column๊ณผ ๊ฐ™์œผ๋ฉฐ ๋จผ์ € ์“ด ์œ„์ ฏ์ด ์ œ์ผ ์•„๋ž˜์— ๊น”๋ฆฌ๊ฒŒ ๋œ๋‹ค.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("hello!"),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            color: Colors.amber,
            width: 300,
            height: 300,
            margin: const EdgeInsets.all(8.0),
          ),
          Container(
            color: Colors.blue,
            width: 200,
            height: 200,
            margin: const EdgeInsets.all(8.0),
          ),
          Container(
            color: Colors.yellow,
            width: 100,
            height: 100,
            margin: const EdgeInsets.all(8.0),
          ),
        ]),
    );
  }
}


์ฐธ์กฐ

sysocoder.com/flutter-column-row-widgets-%EC%82%AC%EC%9A%A9%EB%B2%95/

 

SysoCoder

SysoCoder ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ

sysocoder.com