반응형

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

  • 상위 위젯에서 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>();
반응형

'Dart' 카테고리의 다른 글

Dart 상속  (0) 2023.11.17
Dart Class 생성자  (0) 2023.11.17
Dart 예외처리 (try on finally)  (0) 2023.11.16
Dart Function  (0) 2023.11.16
Dart Null safety  (0) 2023.11.16
Dart 컬렉션 타입 (List, Set, Map)  (0) 2023.11.16
Dart 변수 타입 (var, dynamic)  (0) 2023.11.16
Dart 상수 (const, final)  (0) 2023.11.16
Posted by 까칠코더
,