FocusDetector는 위젯이 포커스를 얻거나 잃었을 때 이를 감지하는 Flutter 패키지로, 개발자가 실시간으로 가시성 변경에 반응할 수 있게 해줍니다. 위젯이 화면에 나타날 때(포커스 획득) 또는 사라질 때(포커스 상실) 콜백을 제공하여 애니메이션, 사용자 상호작용 추적, 가시성 기반 상태 관리에 이상적입니다. 이 패키지는 Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼에서 작동하며, 타겟 환경에 관계없이 일관된 동작을 보장합니다. 기존의 Flutter 앱에 최소한의 설정으로 원활하게 통합할 수 있습니다.
사용 사례
분석을 위한 트래킹 위젯 가시성
등장 시 애니메이션 트리거
포커스 상태에 따라 상태 관리
비활성 위젯 일시 정지로 성능 최적화
포커스 변경에 반응하는 반응형 UI 구현
주요 기능
실시간 초점 감지
크로스 플랫폼 호환성
간단한 콜백 기반 API
경량이며 통합이 쉬움
포커스 획득 및 상실 이벤트 모두 지원
적합한 대상
인터랙티브 UI를 구축하는 개발자
가시성 인식 로직이 필요한 앱
사용자 포커스에 의해 트리거되는 애니메이션
성능에 민감한 애플리케이션
라이프사이클 인식이 필요한 위젯
고려 사항
올바른 위젯 트리 구조가 필요합니다
복잡한 중첩 레이아웃과 함께 예상대로 작동하지 않을 수 있습니다
상호작용이 없는 위젯에는 적합하지 않습니다
포커스 관련 이벤트에만 제한
상태를 가진 위젯이나 컨트롤러와 함께 사용하는 것이 가장 좋습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Focus Detector
Get notified every time your widget appears or disappears from the screen.
Similar to onResume()/onPause() on Android and viewDidAppear()/viewDidDisappear() on iOS.
Focus Detector fires callbacks for you whenever something happens to take or give your widget focus. Such an event might be, for instance, the user:
Navigating to/from another screen;
Turning the device’s screen on/off while your widget is visible;
Switching to/from another app while your widget is visible;
Scrolling your widget in/out the screen;
Usage
@overrideWidgetbuild(BuildContext context) =>
FocusDetector(
onFocusLost: () {
logger.i(
'Focus Lost.''\nTriggered when either [onVisibilityLost] or [onForegroundLost] ''is called.''\nEquivalent to onPause() on Android or viewDidDisappear() on iOS.',
);
},
onFocusGained: () {
logger.i(
'Focus Gained.''\nTriggered when either [onVisibilityGained] or [onForegroundGained] ''is called.''\nEquivalent to onResume() on Android or viewDidAppear() on iOS.',
);
},
onVisibilityLost: () {
logger.i(
'Visibility Lost.''\nIt means the widget is no longer visible within your app.',
);
},
onVisibilityGained: () {
logger.i(
'Visibility Gained.''\nIt means the widget is now visible within your app.',
);
},
onForegroundLost: () {
logger.i(
'Foreground Lost.''\nIt means, for example, that the user sent your app to the background by opening ''another app or turned off the device\'s screen while your ''widget was visible.',
);
},
onForegroundGained: () {
logger.i(
'Foreground Gained.''\nIt means, for example, that the user switched back to your app or turned the ''device\'s screen back on while your widget was visible.',
);
},
child: Container(),
);
Usage Scenarios
Turn on and off resource-consuming features, such as camera, location or bluetooth;
Sync your data with a remote API or local database;
Pause and resume video/audio playback or streaming;