Flutter

자식 클래스 위젯에서 부모 클래스 위젯 갱신하는법

beomsuong 2023. 8. 24. 16:33

부모 클래스에서  _를 지워 자식 클래스에서 접근이 가능하도록 해야한다

 

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