프로그래밍/flutter

Flutter 레이아웃기초 강좌 Row와 Column

싯타마 2020. 1. 21. 18:02

 오늘 Flutter에서 가장 기본적인 레이아웃 함수인 Row Column을 배워 보겠습니다.

 

말 그대로 Row는 물체를 가로로 나열하는 것을 의미하고  Column은 세로로 배치하는 것을 의미합니다.

 

먼저, Row로 사각형을 가로로 배치해보겟습니다.

 

<Row함수 코드>

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Row')
      ),
      body: Container(
        child: Row(
          children: <Widget>[
            Square(),
            Square(),
            Square(),
            Square(),

          ],

        ),
      ),
    );
  }
}

class Square extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      decoration: BoxDecoration(
        color: Colors.yellow,
        border: Border.all(),
      ),
    );
  }
}

 

Row함수 결과

 

square클래스로 사각형을 만든 건 무시하시고 Row함수를 통해 가로로 배열하는 것만 신경 쓰시면 됩니다.

다음은 Column함수로 바꿔보겠습니다.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Row')
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Square(),
            Square(),
            Square(),
            Square(),

          ],

        ),
      ),
    );
  }
}

class Square extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      decoration: BoxDecoration(
        color: Colors.yellow,
        border: Border.all(),
      ),
    );
  }
}

Column 함수 결과

body부분의 Row->Column으로만 바뀌어 주시면 됩니다.



이상 오늘의 포스팅을 마칩니다.