๐ฆ Declare
A lightweight Flutter package to simplify Publish ViewModel-driven UI with a declarative approach. It enables a clean separation of UI and logic using ViewModel
, Prop
, and Declare
widget components.
โจ Features
- ๐ง Easy ViewModel management
- ๐ Publish state updates using
Prop
- ๐งผ Clean separation of UI and business logic
- ๐ฆ Lightweight and dependency-free (other than Flutter)
๐ Installation
Add declare
to your pubspec.yaml
:
dependencies:
declare: ^1.3.2
Or
$ flutter pub add declare
๐ ๏ธ Getting Started
1. Define a ViewModel
Extend the ViewModel
class to hold your Prop values and logic.
import 'package:declare/declare.dart';
class CounterViewModel extends ViewModel {
final counter = Prop(0);
CounterViewModel() {
register(counter);
}
@override
void onInit() {
print('Initialized');
}
@override
void dispose() {
// cleanup if needed
super.dispose();
}
}
2. Use Declare
Widget in Your UI
Use the Declare
widget to bind your ViewModel
to your widget tree.
Declare<CounterViewModel>(
create: () => CounterViewModel(),
builder: (context, viewModel) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: ${viewModel.counter.value}'),
ElevatedButton(
onPressed: viewModel.increment,
child: Text('Increment'),
),
],
);
},
);
๐ Core Concepts
Prop<T>
A wrapper around ValueNotifier<T>
that allows you to declare Prop fields inside your ViewModel.
final Prop<String> name = Prop('Hello');
PropRegistrable
A mixin that allows the ViewModel to register its Prop fields so that UI can rebuild on updates.
@override
List<Prop> get props => [name, counter];
ViewModel
An abstract class meant to be extended by your logic classes. Override dispose()
if necessary.
๐ฆ File Structure
File | Purpose |
---|---|
declare.dart |
Exports all core functionality |
view_model.dart |
Defines ViewModel base class |
prop.dart |
Defines the Prop wrapper for state |
prop_registrable.dart |
Mixin for exposing Prop fields |
widgets.dart |
Main Declare widget logic for binding UI |
๐งช Example
Check out the example/ directory for a full working demo.
โ Best Practices
- Group your
Prop
s inside a ViewModel to isolate state. - Always return Publish fields in
get props
for proper rebuilds. - Dispose resources or subscriptions in
ViewModel.dispose
.
๐ License
MIT License ยฉ 2025 Siva Sankar