본문 바로가기

Flutter (with 코딩셰프)

[2023.12.04] 플러터(flutter) 조금 매운맛 강좌 01 | Sateful Widget #1

<< 소스 코드 >>

import 'package:flutter/material.dart';

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

//stateless widget이라서 화면에 counter가 올라가지 않음
//ctrl + s 눌러 저장 시 변경된 counter 값이 화면에 반영됨.
class MyApp extends StatelessWidget {

  int counter = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              //화면에 문구와 counter 변수값 출력
              Text('You have pushed the button this many times:'),
              Text(
                  '$counter',
                style: Theme.of(context).textTheme.displayLarge,
              )
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          //버튼 클릭 시 counter 변수 값. 증가
          onPressed: (){
            counter++;
            print("$counter");
          },
        ),
      ),
    );
  }
}

 

<< 출력 화면 >>