ecco 0.0.1 ecco: ^0.0.1 copied to clipboard
A simple, MVVM-focused state management solution for Flutter.
Ecco #
A simple, MVVM-focused state management solution for Flutter.
Features #
- Lightweight and easy to understand
- Built with MVVM (Model-View-ViewModel) pattern in mind
- Efficient rebuilds with fine-grained reactivity
- Debug overlay for easy state visualization
- Supports Equatable for efficient state comparisons
Installation #
Add ecco
to your pubspec.yaml
file:
dependencies:
ecco: ^0.0.1
Then run:
flutter pub get
Usage #
Basic Setup #
- Create your model and viewmodel classes:
class CounterModel {
final int value;
CounterModel(this.value);
}
class CounterViewModel {
void increment(EccoNotifier<CounterModel> model) {
model.ripple(CounterModel(model.state.value + 1));
}
}
- Create
EccoNotifier
s for your model and viewmodel:
final counterModel = EccoNotifier<CounterModel>(CounterModel(0));
final counterViewModel = EccoNotifier<CounterViewModel>(CounterViewModel());
- Wrap your app with
EccoProvider
s:
EccoProvider<CounterModel>(
notifier: counterModel,
child: EccoProvider<CounterViewModel>(
notifier: counterViewModel,
child: MyApp(),
),
)
Using EccoBuilder #
Use EccoBuilder
to rebuild parts of your UI when the state changes:
EccoBuilder<CounterModel>(
builder: (context, model) {
return Text('Count: ${model.value}');
},
)
Using EccoConsumer #
Use EccoConsumer
to access both model and viewmodel in a widget:
EccoConsumer<CounterModel, CounterViewModel>(
builder: (context, model, viewModel) {
return ElevatedButton(
onPressed: () => viewModel.increment(EccoProvider.of<CounterModel>(context)),
child: Text('Increment'),
);
},
)
Debug Overlay #
To enable the debug overlay for state visualization, wrap your app with EccoDebugOverlay
:
EccoDebugOverlay(
child: MyApp(),
)
Advanced Usage #
Using Equatable #
Ecco supports the use of Equatable
for efficient state comparisons:
class User with Equatable {
final String name;
final int age;
User(this.name, this.age);
@override
List<Object> get props => [name, age];
}
Logging #
Ecco provides built-in logging functionality:
// Enable logging
Eccoes.enable();
// Set log level
Eccoes.setLogLevel(EccoesLogLevel.debug);
// Log a message
Eccoes.log('This is a debug message', level: EccoesLogLevel.debug);
Best Practices #
- Keep your models immutable
- Put business logic in your models
- Put presentation logic in your viewmodels
- Use
EccoConsumer
when you need access to both model and viewmodel - Use
EccoBuilder
for simple state-dependent UI updates
Contribution #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.