Flutter(플러터)/UI

Flutter Stateless & Stateful Widget

병홍 2022. 1. 4. 20:26

Flutter는 크게 2개의 타입의 위젯으로 나뉩니다. 

  • Stateless Widgets
  • Stateful Widgets

Stateless Widgets
Stateless 위젯은 런타임에 강력하게 변경되지 않으므로 상태를 처리할 필요가 없는 위젯입니다.
변수, 버튼, 기호 등 정보를 복구하기 위해 응용 프로그램에서 변경할 수 없도록 영구화됩니다. 빌드 메서드를 덮어써 위젯을 반환합니다.
우리는 실제 아이템 내부의 데이터에 따라 UI가 달라질 때 사용합니다.

mport 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

 

Stateful Widgets
Stateful Widgets는 State와 UI가 런타임에 점진적으로 변경 될 수 있는 위젯입니다. 변경 가능한 위젯이므로 지속적으로 변경됩니다. 사용자가 애플리케이션 화면을 점진적으로 업데이트할 때 활용합니다. 이 위젯은 상태 위젯을 가지고 있기 때문에 화면에 무언가가 업데이트되었다는 것을 모두가 인식하기 때문에 여러 위젯 중 가장 중요합니다

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}