cubix 0.0.1 cubix: ^0.0.1 copied to clipboard
A enhanced state management of Bloc
Cubix #
A enhanced state management of Bloc
Features #
- State dependencies
- Auto sync state
- Async state handling
- State updating cancellation
Usages #
Simple Cubix #
// create a cubix
class CounterCubix extends Cubix<int> {
// init cubix with state 0
CounterCubix(): super(0);
// methods
void increment() {
// change state
emit(state + 1);
}
void decrement() {
// change state
emit(state - 1);
}
}
class App extends StatelessWidget {
@override
build(context) {
return CubixProvider(
// build widget from CounterCubix
// the builder receives BuildContext and Cubix objects
child: CounterCubix.new.build((context, cubix) {
return Column(
children: [
// getting state of cubix
Text(cubix.state.toString()),
ElevatedButton(
// incoking cubix method
onPressed: cubix.increment,
child: const Text('Increment'),
),
ElevatedButton(
onPressed: cubix.decrement,
child: const Text('Decrement'),
)
]
);
})
);
}
}
Auto Sync #
class CounterCubix extends Cubix<int> {
CounterCubix(): super(0);
void increment() {
emit(state + 1);
}
void decrement() {
emit(state - 1);
}
}
class DoubleCounterCubix extends Cubix<int> {
late final CounterCubix counterCubix;
DoubleCounterCubix(): super(0);
@override
onCreate(context) {
super.onResolve(context);
// call enableSync to allow this cubix updates whenever its dependency cubixes are updated
// if you want to debouce an update, just call enableSync(debounce: Duration(seconds: 1))
context.enableSync();
counterCubix = context.from(CounterCubix.new);
}
@override
onInit() {
super.onInit();
emit(counterCubix.state * 2);
}
}