목표

Flutter의 StatelessWidget과 StatefulWidget의 차이점을 알아보고, 동적 UI를 구현합니다.

1.1 소개

Flutter에서 모든 UI는 위젯으로 구성됩니다. 위젯은 크게 StatelessWidget(변하지 않는 UI)과 StatefulWidget(변화하는 UI)으로 나뉩니다. 이번에는 이 둘의 차이점을 알아보고, 상태를 관리하는 방법을 배워 보겠습니다.

1.2 사전 준비

  • 이전 강좌에서 만든 프로젝트를 이어서 사용하거나 새 프로젝트를 생성하세요.
  • Flutter 개발 환경이 올바르게 설정되어 있어야 합니다.

1.3 핵심 내용

1단계: StatelessWidget

StatelessWidget은 상태가 변하지 않는 정적인 UI를 정의합니다.

새 프로젝트 또는 이전 프로젝트의 소스코드를 다음과 같이 작성합니다.

결과

  • Text 위젯은 변하지 않고 항상 동일한 텍스트를 표시합니다.

2단계: StatefulWidget 이해하기

StatefuilWidget은 상태가 변할 수 있는 동적인 UI를 정의합니다.

다음과 같이 코드를 수정하여 UI가 어떻게 변화하는지 확인해 보세요.

결과

  • 버튼을 누를 때마다 _counter 값이 증가하고, UI가 업데이트됩니다.
  • ‘값 초기화’ 버튼을 누르면 _counter 값이 0으로 초기화 됩니다.
  • setState()를 호출하여 상태를 업데이트하고 화면을 다시 그립니다.
구분StatelessWidgetStatefulWidget
상태 관리상태를 가지지 않음상태를 가질 수 있음
UI 변경고정된 UI, 변경되지 않음상태 변화에 따라 UI가 동적으로 변경됨
용도도단순 텍스트, 아이콘 등동적인 UI, 버튼 클릭, 입력 폼 등

1.5 실습

1단계: 상태 변화

  • StatefulWidget을 사용하여 버튼 클릭 시 배경색이 변겅되도록 구현해보세요.

2단계: ‘값 감소’ 버튼 추가

  • 값 감소’ 버튼을 추가하여 _counter 값을 감소 시킬 수 있도록 수정 해보세요.

3단계: 조건 추가

  • _counter 값이 10 이상일 경우 “최대값에 도달했습니다.”라는 텍스트를 표시되게 해보세요.

1.6 실습 결과

1.7 전체 소스코드

1.8 결론

이번에는 StatelessWidget과 StatefulWidget의 차이점을 알아보고, StatefulWidget을 사용하여 상태를 관리하는 학습했습니다. 이를 통해 Flutter의 동적인 UI를 구현할 수 있습니다.

다음 단계에서는 Flutter에서 사용자 입력 폼을 학습하고, 입력 데이터를 처리해 보겠습니다.

1.9 Q & A

Q: StatefulWidget에서 setState()를 언제 사용해야 하나요?
A: UI가 변경될 필요가 있는 경우에만 setState()를 호출합니다. 불필요한 호출은 성능에 영향을 줄 수 있습니다.

Q: StatelessWidget에서 상태를 변경하려면 어떻게 해야 하나요?
A: 상태를 변경하려면 StatefulWidget으로 전환해야 합니다. StatelessWidget은 상태를 가지지 않으므로 동적 변경이 불가능합니다.

감사합니다.


TechTinkerer's에서 더 알아보기

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

댓글 남기기

  • **Iran Protests Turn Deadly as Death Toll Surges**

    The situation in Iran has taken a deadly turn as protests against the government continue to escalate. The death toll has risen significantly, with over 60 protesters reported to have died since the demonstrations began. The Iranian authorities have responded by tightening their grip on the internet and warning of severe consequences for those who…

  • The Chaebum Conspiracy: A Web of Intrigue Surrounding Yoon Seok-yeol’s Election

    The 2022 South Korean presidential election has been marred by controversy and scandal, with one figure at the center of it all being Yoon Seok-yeol. As a key witness in the impeachment trial against him, Chaebum Lee has shed light on the inner workings of the group that allegedly orchestrated his rise to power. The…

  • **North Korea Threatens to Shoot Down South Korean Drones Again**

    A recent statement from North Korean officials has raised concerns about the safety of drone operations over the Demilitarized Zone (DMZ) between North and South Korea. The country’s military claimed that it will take action if South Korean drones enter its airspace again, sparking fears of a potential conflict. The tensions surrounding drone flights over…

  • **Nuclear Disarmament Talks Stall Amid US-Greenland Tensions**

    Tensions between the United States and Greenland are escalating as nuclear disarmament talks stall. The situation is further complicated by a recent proposal from the Trump administration to deploy intermediate-range missiles in the region. The lack of progress in nuclear disarmament talks has raised concerns about the stability of international relations. The US, under the…

  • The Uncertain Fate of Yoon Seok-yeol: A Nation Held Hostage by a Presidential Trial

    The trial of President Yoon Seok-yeol has been ongoing for months, leaving the nation in limbo. As the special prosecutor’s office prepares to present its findings, one question remains: will Yoon face the death penalty or life imprisonment? The uncertainty surrounding his fate has sparked intense debate and anxiety among Koreans. The trial began as…

← 뒤로

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

TechTinkerer's에서 더 알아보기

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

계속 읽기

TechTinkerer's에서 더 알아보기

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

계속 읽기