flutter_piper 0.0.2
flutter_piper: ^0.0.2 copied to clipboard
Lifecycle-aware state management for Flutter. ViewModels, async state, stream bindings, and task cancellation.
flutter_piper #
Flutter widgets for Piper state management.
Installation #
dependencies:
piper: ^0.0.1
flutter_piper: ^0.0.1
Widgets #
ViewModelScope #
Provides multiple ViewModels to the widget tree:
ViewModelScope(
create: [
() => AuthViewModel(authRepo),
() => TodosViewModel(todoRepo),
],
child: MyApp(),
)
// Access anywhere below:
final vm = context.vm<AuthViewModel>();
Scoped<T> #
Scopes a single typed ViewModel with a builder pattern:
Scoped<DetailViewModel>(
create: () => DetailViewModel(id),
builder: (context, vm) => DetailPage(),
)
// Access via context:
final vm = context.vm<DetailViewModel>();
// Or use the semantic alias:
final vm = context.scoped<DetailViewModel>();
Named Scopes #
Share ViewModels across multiple routes with named scopes:
ViewModelScope.named(
name: 'checkout',
create: [() => CheckoutViewModel()],
child: CheckoutFlow(),
)
// Access by name from any descendant:
final vm = context.vm<CheckoutViewModel>(scope: 'checkout');
StateBuilder #
Rebuilds when state changes:
vm.count.build((count) => Text('$count'))
StateListener #
Side effects without rebuilding:
vm.isDeleted.listen(
onChange: (prev, curr) {
if (curr) Navigator.of(context).pop();
},
child: // your UI
)
StateEffect #
Post-frame side effects with conditions:
StateEffect<bool>(
listenable: vm.isLoggedIn.listenable,
when: (prev, curr) => !prev && curr,
effect: (_, ctx) => Navigator.of(ctx).pushReplacement(...),
child: // your UI
)