본문 바로가기
프로그래밍/Flutter-Dart

[Flutter] SizedBox, Spacer

by 채연2 2022. 2. 16.

SizedBox

자식의 크기를 지정할 수 있다

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(
      home: Row(
        children: [
          BlueBox(),
          SizedBox(
            width: 200,
            height: 150,
            child: BlueBox(),
          ),
          BlueBox()
        ],
      ),
    );
  }
}

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

 

 

빈 여유 공간을 만드는데 유용하다고 한다.

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(
      home: Row(
        children: [
          BlueBox(),
          SizedBox(
            width: 200,
          ),
          BlueBox()
        ],
      ),
    );
  }
}

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

 

 


Spacer

spacer는 flex 속성을 사용하여 공간을 생성하고, SizedBox는 특정 픽셀을 지정하여 공간을 생성할 수 있다

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(
      home: Row(
        children: [
          BlueBox(),
          Spacer(
            flex: 1,
          ),
          BlueBox()
        ],
      ),
    );
  }
}

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

320x100

'프로그래밍 > Flutter-Dart' 카테고리의 다른 글

[Flutter] Google Map API 사용하기  (0) 2022.02.28
[Flutter] Text, Icon, Image  (0) 2022.02.16
[Flutter] Flexible, Expanded  (0) 2022.02.16
[Flutter] Row, Column  (0) 2022.02.16
[Flutter] Login App (3)  (0) 2021.03.29

댓글