๐Ÿ“ฆ 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 Props 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