How to Use Container Widgets

 

[Ko]

 - Container widget 대하여 알아봅시다. Container widget web development에서의 div 비슷한 개념이며, layout box 역할을 한다.

 - Container widget child 받아서 대상 widget size, position 등을 정해줌.

 - layout widget child 숫자에 따라서 Single-child layout widgets Multi-child layout widgets 나누어지며 Container widget Single-child layout widget 속함.

 - children 가지지 않는 Container 가능한 최대의 공간을 차지하려는 속성을 가지며, children 가지는 경우는 Container size children size 같아짐.

 - Text widget 대상 string font size 만큼의 크기를 가짐.

 - SafeArea : 핸드폰 상하단의 기기 모양에 따라 잘리는 부분을 제외한 안전한 공간에 배치시키는 widget.

 - flutter inspector 파란 사각 버튼(Show Debug Paint) 누르면 margin, padding 등의 layout 구조를 보다 정확하게 있음. (아래 그림) 

 

 

 - widget 바깥 공간 여백 조정 : margin

 - widget 안쪽 공간 여백 조정 : padding

 - margin, padding 모두 Edgeinsets 사용하며, Edgeinsets class all, symmetric, fromLTRB, only 등의 다양한 constructor 가짐.

 - final code

import 'package:flutter/material.dart';

 

void main() {

  runApp(

    MyApp()

  );

}

 

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return     MaterialApp(

      home: Scaffold(

        backgroundColor: Colors.teal,

        body: SafeArea(

          child: Container(

            height: 100.0,

            width: 100.0,

//            margin: EdgeInsets.all(20),

//            margin: EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),

//            margin: EdgeInsets.fromLTRB(30.0, 10.0, 50.0, 20.0),

            margin: EdgeInsets.only(left: 30.0, top: 30.0),

            padding: EdgeInsets.all(20.0),

            color: Colors.white,

            child: Text('Hello'),

          ),

        ),

      ),

    );

  }

}

 

 - container 1개의 child만을 가질 있음. 복수의 child 이용해서 layout 구성하기 위해서는 Column widget이나 Row widget 사용할 있음.

 - Container 배경색을 color property 시용해서 설정하는 것은 괜찮지만 decoration property BoxDecoration 위젯을 사용하는 경우에는 color property BoxDecoration 내부로 옮겨야 . (옮기지 않으면 app crash 발생), Container 가지고 있는 color property 원애 BoxDecoration color property 편의를 위해 밖으로 빼놓은 것임.

 

[En]

 - Let's find out about Container widget. Container widget is a concept similar to div in web development, and serves as a layout box.

 - Container widget receives child and determines size, position, etc. of target widget.

 - The layout widget is divided into single-child layout widgets and multi-child layout widgets according to the number of children, and the container widget belongs to the single-child layout widget.

 - Containers that do not have children have the property of occupying the maximum space possible, and if they have children, Container size is the same as children size.

 - Text widget has the size of the font size of the target string.

 - SafeArea: A widget that is placed in a safe space except for the cut portion depending on the shape of the device at the top and bottom of the mobile phone.

 - If you press the blue square button (Show Debug Paint) of the flutter inspector, you can see the layout structure such as margin and padding more accurately. (Picture below)

 

- Adjust the margin of the outer space of the widget: margin

 - Adjust the inner space margin of the widget: padding

 - Both margin and padding use Edgeinsets, and Edgeinsets class has various constructors such as all, symmetric, fromLTRB, and only.

 - final code

import 'package:flutter/material.dart';

 

void main() {

  runApp(

    MyApp()

  );

}

 

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return     MaterialApp(

      home: Scaffold(

        backgroundColor: Colors.teal,

        body: SafeArea(

          child: Container(

            height: 100.0,

            width: 100.0,

//            margin: EdgeInsets.all(20),

//            margin: EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),

//            margin: EdgeInsets.fromLTRB(30.0, 10.0, 50.0, 20.0),

            margin: EdgeInsets.only(left: 30.0, top: 30.0),

            padding: EdgeInsets.all(20.0),

            color: Colors.white,

            child: Text('Hello'),

          ),

        ),

      ),

    );

  }

}

 

 - A container can have only one child. Column or Row widget can be used to configure layout using multiple children.
 - It is okay to set the background color of the container by using the color property, but when using the BoxDecoration widget for the decoration property, the color property must be moved inside the BoxDecoration. (If it doesn't move, an app crash occurs), and the color property of the container is the color property of the original BoxDecoration taken out for convenience.

 

 

 

 

'플러터(Flutter) > 플러터 위젯(Flutter Widget)' 카테고리의 다른 글

FlatButton widget  (0) 2020.03.18
Expanded Widget to Create Flexible Layouts  (0) 2020.03.18
Card & ListTile Widget  (0) 2020.03.18
CircleAvatar  (0) 2020.03.18
Column & Row Widgets for Layout  (0) 2020.03.18

+ Recent posts