flutter_onlooker
一个状态管理库,可提供简单的方法来更新状态和导航。
- 星标
- 2
- 复刻
- 0
- 最近推送(UTC)
- 2024年4月11日
- 项目状态
- 未归档
原始 README
以下为英文项目原文快照,最新内容请访问 GitHub。
Onlooker
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!"),
),
);
},
)
语言DartSwiftKotlinObjective-C
使用的依赖
依赖清单 3 项
- flutter
{"sdk":"flutter"} - flutter_test开发依赖
{"sdk":"flutter"} - very_good_analysis开发依赖
^5.1.0
如何选择适合项目的代码仓?
先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。
- 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
- 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
- 使用前在 GitHub 核对 README、许可证与 Issues。
来源与维护
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗