StatefulWidget

이원석·2023년 11월 13일
0

Flutter

목록 보기
11/46

StatefulWidget

StatefulWidget은 변경 가능한 상태를 가진 widget
stful을 치고 엔터를 누르면 자동완성이 된다.
setState(() {});을 호출해야 flutter에서 state변경을 감지하고 적용한다.

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

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

  
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  int count = 0;

  void counter() {
    setState(() {
      count += 1;
    });
  }

  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: const Color(0xFFFF1515),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                "Counter",
                style: TextStyle(fontSize: 40),
              ),
              Text(
                "$count",
                style: const TextStyle(fontSize: 40),
              ),
              IconButton(
                  iconSize: 40,
                  padding: const EdgeInsets.all(16),
                  onPressed: counter,
                  icon: const Icon(
                    Icons.add_box_outlined,
                  )),
            ],
          ),
        ),
      ),
    );
  }
}

0개의 댓글