부모 클래스에서 _를 지워 자식 클래스에서 접근이 가능하도록 해야한다
class Parent extends StatefulWidget {
@override ParentState createState() => ParentState();
}
class ParentState extends State<Parent> {
int page = 1; @override Widget build(BuildContext context) {
return 어쩌구저쩌구위젯();
}
}
class Child extends StatefulWidget { SearchBar(); @override ChildState createState() => ChildState(); } class ChildState extends State<Child> { @override Widget build(BuildContext context) { ParentState parent = context.findAncestorStateOfType<ParentState>(); return BlaBlaButton(text: Text('자식'), onTap: () { parent.setState() { parent.page += 1; } }); } }
출처: https://roseline.oopy.io/dev/flutter-tips/findancestorstateoftype
'Flutter' 카테고리의 다른 글
Row안에서 글자 높이 맞추기 (0) | 2024.01.17 |
---|---|
Flutter vsc 기본 설정 (0) | 2023.12.19 |
비동기 함수 일정시간 지연 시키기 (0) | 2023.11.16 |
Flutter) 파이어베이스 배열안에 일치하는 값만 가져오기 (0) | 2023.08.30 |
로딩중 화면 터치 불가 및 흐릿하게 하기 (0) | 2023.08.25 |