ihorbokov/flutter_onlooker
상태 및 탐색 업데이트를 위한 간단한 솔루션을 제공하는 상태 관리 라이브러리입니다.
- Stars
- 2
- Forks
- 0
- 최근 푸시(UTC)
- 2024. 4. 11.
- 프로젝트 상태
- 활성
언어DartSwiftKotlinObjective-C
사용 중인 의존성
의존성 목록 3 개
- flutter
{"sdk":"flutter"} - flutter_test개발 의존성
{"sdk":"flutter"} - very_good_analysis개발 의존성
^5.1.0
원본 README
아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
README 펼치기 / 접기
Onlooker
Pub style: very good analysis License: MIT
A state management library that provides a simple solution for updating state and navigation.
Usage
Lets take a look at how to use flutter_onlooker. The library provides: StateNotifier, StateObserver and StateNotifierProvider classes.
StateNotifieris responsible for registering and notifying states. Also it can emit navigation events. The main methods of this class:
observable<S>registers observable state ofStype.initial<S>returns initial value for state ofStype.latest<S>returns the latest value for state ofStype.contains<S>checks whether a state ofStype was registered before.notify<S>notifies a new state ofStype.navigate<T extends Object?>notifies a new navigation event.
class IncrementStateNotifier extends StateNotifier {
IncrementStateNotifier() {
observable<int>(initial: _counter);
}
var _counter = 0;
void useCase() {
final initialState = initial<int>();
final latestState = latest<int>();
final containsState = contains<int>();
notify<int>(++_counter);
navigate<void>('/route', arguments: _counter);
}
}
StateObserverhandles building a widget in response to newstates. It takes 3 parameters:
- required
builderfunction that takes theBuildContextandstate, this function is responsible for returning a widget that is to be rendered. - An optional
notifierthat can be passed directly or with usingStateNotifierProvider. - An optional
buildWhencan be implemented for more granular control over how oftenStateObserverrebuilds.
StateObserver<IncrementStateNotifier, int>(
builder: (_, state) {
return Text(
'$state',
style: Theme.of(context).textTheme.headlineMedium,
);
},
)
StateNotifierProvidertakes acreatefunction that is responsible for creating theStateNotifier,childwidget that will have access to theStateNotifierinstance viaProvider.of<StateNotifier>(context)orcontext.read<StateNotifier>()and optionalrouterfunction that will receive navigation events.
StateNotifierProvider<IncrementStateNotifier>(
create: (_) => IncrementStateNotifier(),
child: const MyHomePage(title: 'Onlooker Demo'),
router: (context, routeName, arguments) {
return showDialog<void>(
context: context,
builder: (_) => AlertDialog(
title: Text("Route is '$routeName'"),
content: Text("You've clicked $arguments times!"),
),
);
},
)