Flutter stateful widget은 위젯 안에서 상태(데이터)를 가지고 해당 상태의 변화에 따라 화면에 표시되는 UI를 변경할때 사용 하며 stateless widget은 변경되는 상태가 없을떄 사용 하는 것으로 알고 있습니다.
그런데 provier 사용시는 stateless widget사용하여도 상태 변화가 화면에 바로 표시가 되는 부분에서 의문이 생겨 문의 드립니다.
부모 위젯이 stateful 위젯이면 하위 상태(데이터) 위젯이 stateless 라도 해당 상태 변화에 문제가 없다 라고 생각 되는대 이 경우 provider 를 사용하는 모든 하위 위젯은 stateless로 작성하여도 상태 변화가 반영 되는 statefule widget 같이 동작 할 수 있는 지 문의 드립니다.
< 코드 일부 입니다.>
changeFollower() {
follower == 0 ? follower++ : follower--;
notifyListeners();
}
}
class Profile extends StatelessWidget {
const Profile({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(context.watch<Store2>().name),
),
body: ListView(
padding: EdgeInsets.all(5),
children: [
ListTile(
leading: CircleAvatar(
radius: 30,
backgroundImage: AssetImage('images/A1.gif'),
),
title: Text('팔로워 ${context.watch<Store1>().follower}명'),
trailing: Wrap(
spacing: 8,
children: [
IconButton(
onPressed: () {
context.read<Store1>().changeFollower();
},
icon: Icon(Icons.favorite_border_outlined),
),
IconButton(onPressed: () {
context.read<Store1>().getData();
},
icon: Icon(Icons.add))
],
),
)
],
)