name27 / flutter

0 stars 0 forks source link

Stateless, Stateful #37

Open name27 opened 1 year ago

name27 commented 1 year ago

Flutter는 그림을 그리는 툴이고 그것을 빌드라고 한다. 모든 위젯은 빌드를 통해 그려진다 Widget build(BuildContext context) {}로 빌드가 진행 됨 Flutter 위젯은 Stateless 위젯, Stateful 위젯 둘 중 하나이다. Stateless

Stateful

image

name27 commented 1 year ago

StatefulWidget 예제

image

// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';

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

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

  static const String _title = 'Flutter Code Sample';

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String message = '안녕';
  int level = 1;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Text(message),
              InkWell(
                onTap: () {
                  level ++;
                  setState(() {}); //이 함수를 넣어줘야 재 빌드 됨
                },
                child: Text('$level레벨 입니다.')
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            message = '반가워';
            setState(() {});
          },
          label: Icon(Icons.change_circle)
        ),
      ),
    );
  }
}