Flutter setState로 재랜더링하기

바다구름·2023년 3월 4일
0

Flutter

목록 보기
7/19
  1. StatelessWidget -> StatefulWidget으로 바꾸기
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {

  var a = 1;  //여기 만드는 변수는 state가 됩니다 
  
  Widget build(BuildContext context) {
    return Scaffold(생략);
  }
}  
  1. setState() 사용
MaterialApp(
  home: Scaffold(
    floatingActionButtion: FloatingActionButton(
      child : Text(a.toString()),
      onPressed: (){ 
        setState((){
          a++
        });
      }
    ),
    appBar: AppBar(),
    body: 생략
  ),
)
profile
안녕하세요.

0개의 댓글