상태 관리 | CoUI

상태 관리

CoUI 컴포넌트의 controlled / uncontrolled 패턴

상태 관리#

CoUI 폼/인터랙티브 컴포넌트는 두 가지 상태 패턴을 지원합니다 — Flutter / Web 동일.

1. Controlled (value + onChanged)#

부모가 값을 소유하고 onChanged 로 갱신받는 기본 패턴. 대부분의 폼 컴포넌트가 이 형태입니다.

bool checked = false;

Checkbox(
  state: checked ? CheckboxState.checked : CheckboxState.unchecked,
  onChanged: (next) => setState(() => checked = next == CheckboxState.checked),
)

StarRating(
  value: rating,
  onChanged: (v) => setState(() => rating = v),
)

enabled 는 nullable 이며 핸들러 존재로 유도됩니다 — onChanged == null 이면 자동 비활성화.

2. Controller / Controlled 변형 (advanced)#

프로그래밍적으로 값을 제어하거나 폼/리액티브 시스템과 통합할 때는 컴포넌트별 컨트롤러 또는 Controlled{X} 변형을 사용합니다.

final controller = CheckboxController(false);
controller.addListener(() => print(controller.value));
controller.value = true;

대표 예: CheckboxController · CarouselController · ControlledStarRating · ControlledDatePicker · ComponentValueController.

트리 공유 — Data.inherit#

부모가 자식 트리로 상태를 흘릴 때는 Data.inherit 를 씁니다 (예: 버튼 그룹이 하위 버튼에 상태 publish). ==/hashCode 에 rebuild 에 영향 주는 모든 필드를 포함해야 자식이 정확히 갱신됩니다.