반응형

위젯들 간에 서로의 상태값을 가져오는 방법은 다음과 같습니다.

  • 상위 위젯에서 currentState를 사용해서 하위 위젯의 상태값 가져오기 (하위위젯 생성시 키를 사용)
class ParentWidgetState extends State<ParentWidget> {
  GlobalKey<ChildWidgetState> childKey = GlobalKey();
  
  // Child 생성
  @overrider
  Widget build(BuildContext context) {
    return ChildWidget(key: childKey);
  }
  
  // Child 상태값 사용
  ...
  ChildeWidgetState? childState = childKey.currentState;
  childState?.childValue
  ...
}

 

  • 하위 위젯에서 findAncestorStateOfType을 사용해서 상위 위젯의 상태값 가져오기
ParentWidgetState? state = context.findAncestorStateOfType<ParentWidgetState>();
반응형
Posted by 까칠코더
,