목표

Flutter의 주요 위젯인 Text, Button, Row, Column을 알아보고 이를 활용하여 간단한 UI를 구성합니다.

1.1 소개

Flutter에서 UI는 위젯으로 구성됩니다. Text, Button, Row, Column은 Flutter의 가장 기본적인 위젯으로, UI를 배치하고 상호작용을 구현할 때 자주 사용됩니다. 이번에는 위젯들을 알아보고 간단한 레이아웃을 만들어 보겠습니다.

1.2 사전 준비

  • Flutter 개발 환경이 준비되어 있어야 합니다.
  • 이전 단계에서 만든 프로젝트(또는 새 프로젝트)를 열어야 합니다.

이전 단계를 진행하지 않았다면 아래 버튼을 눌러 이전 단계를 완료하세요.

1.2 핵심 내용

1단계: Text 위젯

Text 위젯은 화면에 텍스트를 출력합니다. 이전 단계에서 작성한 hello_flutter 또는 새로운 프로젝트를 생성한 후 다음과 같이 작성합니다.

결과:

  • 텍스트 내용: “Flutter는 재미있어요!” 문구가 출력.
  • 스타일: 폰트 크기 24, 텍스트 색상 파란색.

2단계: ElevatedButton 위젯

ElevatedButton은 클릭 가능한 버튼을 생성합니다. 다음과 같이 소스코드를 변경하여 어떻게 변화되는지 확인해 보세요.

결과:

  • 버튼 텍스트:”클릭하세요”.
  • 클릭 이벤트: 버튼을 클릭하면 “버튼 클릭됨”이 콘솔에 출력됩니다.

3단계: Row와 Column 위젯

Row는 가로 방향, Column 세로 방향 위젯을 배치합니다.

다음과 같이 소스코드를 수정해 보세요.

결과:

  • Row: “Row 1”, “Row 2”, “Row 3” 텍스트가 가로로 정렬.
  • Column: “Column text 1”, “Column text 2” 텍스트가 세로로 정렬.
  • 정렬 방식: mainAxisAlignment를 사용하여 정렬.

1.4 실습

1단계: Text 스타일 추가

  • Text 위젯에 폰트 굵기, 배경색, 글꼴 스타일을 추가해 보세요.

2단계: Row와 Column 혼합

  • Row 내부에 Column을 추가하여 복잡한 레이아웃을 만들어 보세요.

3단계: 버튼 클릭 이벤트

  • 버튼을 클릭하면 화면에 “Hello, World!”가 나타나도록 구현 해보세요.
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    String txt1 = 'Change Text 1';

    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(title: const Text('Row와 Column 위젯')),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                      style: TextStyle(fontSize: 30, color: Colors.blue),
                      'text 1  '),
                  Text(
                      style: TextStyle(fontSize: 30, color: Colors.red),
                      'text 2  '),
                  Text(
                      style: TextStyle(fontSize: 30, color: Colors.purple),
                      'text 3  '),
                ],
              ),
              const SizedBox(height: 20),
              Text(txt1),
              const Text('Column Text 2'),
              ElevatedButton(
                onPressed: () {
                  print('text');
                },
                child: const Text('Change Text'),
              ),
            ],
          )),
    );
  }
}

1.5 결론

이번에는 Flutter의 기본 위젯 Text, Button, Row, Column을 알아 보았습니다. 이를 조합하면 간단한 레이아웃을 쉽게 구성할 수 있습니다. 다음 단계에서는 Flutter의 상태 관리를 알아보며 동적 UI를 구현해 보겠습니다.

1.6 Q&A

Q:Row와 Column에 위젯을 어떻게 배치하나요?

A: children 속성세 추가할 위젯들을 리스트 형식으로 전달하면 됩니다.

Q: 버튼 클릭시 상태를 업데이트할 수 있나요?

A: StatefulWidget을 사용하면 버튼 클릭 이벤트를 통해 상태를 업대이트할 수 있습니다. 다음 강좌에서 다룹니다.

다음 단계에서는 *Flutter의 상태 관리(StatelessWidget & StatefulWidget)*에 대해 알아 보겠습니다.

감사합니다.


TechTinkerer's에서 더 알아보기

구독을 신청하면 최신 게시물을 이메일로 받아볼 수 있습니다.

댓글 남기기

  • 7. Flutter 애니메이션과 트랜지션 기초

    Flutter에서 AnimatedContainer와 Hero 위젯을 통해 애니메이션 구현 방법을 소개합니다.

  • 파이썬으로 학생 정보 관리 프로그램 만들기

    학생 정보 관리 프로그램 구현을 통해 변수와 반복문 활용 경험을 배웁니다.

  • **The Green Light for NATO Expansion?**

    The recent developments surrounding the Greenland dispute have sent shockwaves throughout Europe, with many wondering if this could be the spark that ignites a broader debate on NATO expansion. As tensions rise between Denmark and Greenland over the former’s decision to establish a military presence in the latter, we take a closer look at the…

  • Corruption Scandal Rocks Local Government

    A developing scandal has emerged in the local government of a major city, with several high-ranking officials facing allegations of corruption and embezzlement. At the center of the controversy is the former chairman of the city council, Kim Bong-ki, who was accused of accepting large sums of money from two individuals. The Allegations According to…

  • **Denmark Stands Firm Against Trump’s Aggression**

    In a bold move, Denmark has declared its unwavering commitment to NATO and its territorial integrity, following a series of provocative statements from US President Donald Trump. The Danish government’s resolute stance serves as a testament to the country’s unshakeable resolve in the face of aggressive rhetoric. Background The recent exchange between Trump and Danish…

← 뒤로

응답해 주셔서 감사합니다. ✨

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기