Skip to content

UniState is an adapter package designed to provide an agnostic approach to state management in Flutter, offering a unified interface that seamlessly integrates with various state management systems while maintaining Flutter's native code style.

Notifications You must be signed in to change notification settings

aissat/unistate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

UniState: A Universal State Management Adapter ๐Ÿ“ฆ

Package Overview ๐ŸŒ

UniState is an adapter package designed to provide an agnostic approach to state management in Flutter, offering a unified interface that seamlessly integrates with various state management systems while maintaining Flutter's native code style.

Core Purpose ๐ŸŽฏ

The primary goal of UniState is to:

  • Create an Adapter Layer: Provide a flexible adapter for different state management systems ๐Ÿ› ๏ธ
  • Maintain Code Flexibility: Allow easy switching between state management approaches ๐Ÿ”„
  • Preserve Flutter's Code Style: Ensure consistency and idiomatic Flutter development ๐Ÿ’ป

Why UniState? ๐Ÿค”

Developers often face challenges when:

  • Choosing a state management solution โš–๏ธ
  • Migrating between different state management systems ๐Ÿ”„
  • Maintaining a consistent code structure ๐Ÿ“‚

UniState solves these problems by providing a universal adapter that:

  • Decouples your application logic from specific state management implementations ๐Ÿงฉ
  • Provides a consistent interface across different state management approaches ๐Ÿ› ๏ธ
  • Allows for easy experimentation and migration between state management systems ๐Ÿ”„

The State Management Landscape ๐ŸŒ

Flutter offers numerous state management solutions, each with its unique code style and philosophy. Developers often face challenges when:

  • Choosing a state management approach ๐Ÿ”
  • Migrating between different state management systems ๐Ÿ”„
  • Avoiding tight coupling to a specific state management library ๐Ÿ”’

Our Philosophy ๐Ÿ’ก

The primary purpose of UniState is to:

  • Create an Adapter Layer: Provide a flexible adapter for different state management systems ๐Ÿงฉ
  • Maintain Code Flexibility: Allow easy switching between state management approaches ๐Ÿ”„
  • Preserve Flutter's Code Style: Ensure consistency and idiomatic Flutter development ๐Ÿ’ป
  • Preserve Your Code: Protect your application's core logic from changes caused by state management system shifts ๐Ÿ›ก๏ธ

Supported State Managers ๐Ÿงณ

UniState is designed to work with multiple state management approaches, including but not limited to:

  • BLoC (Business Logic Component) ๐Ÿ’ผ
  • Provider ๐Ÿท๏ธ
  • Riverpod ๐ŸŒฑ
  • Cubit ๐Ÿช„
  • Custom State Management Solutions ๐Ÿ› ๏ธ

Key Features ๐Ÿ”‘

  • Agnostic Integration: Seamlessly work with different state management libraries ๐Ÿ”„
  • Minimal Overhead: Lightweight adapter that doesn't compromise performance โšก
  • Flutter-Friendly: Maintains the natural flow and style of Flutter development ๐ŸŒŸ
  • Easy Migration: Simplify transitions between state management approaches ๐Ÿ›ฃ๏ธ

Key Components ๐Ÿงฉ

UniState Package ๐Ÿ“ฆ

Overview

The unistate package provides a structured approach to state management, allowing developers to manage application state efficiently and integrate it seamlessly with the Flutter widget tree ๐ŸŒณ.

Key Components
  • UniState<T>: An abstract class defining a contract for managing state, with methods for getting, updating, and resetting the state ๐Ÿ”„.
  • UniStateProvider<T, S>: A StatefulWidget that manages the lifecycle of a UniState instance and provides it to the widget tree ๐ŸŒณ.
  • Extensions: Methods like read and watch on BuildContext to interact with the state ๐Ÿ‘€.

UniStateAdapter Package ๐Ÿ“ฆ

Overview

The unistate_adapter package extends the functionality of the unistate package by integrating with the flutter_adapter package, providing adapters for bloc and cubit state management ๐Ÿ”„.

Key Components
  • BlocAdapter<S, E>: An adapter that listens to a Bloc and updates its value for reactive UI updates ๐Ÿ”„.
  • CubitAdapter<T>: An adapter that listens to a Cubit and updates its value for reactive UI updates ๐Ÿ”„.
  • Extensions: Methods to convert Bloc and Cubit instances into ValueListenable ๐Ÿ“ˆ.

Key Features and Benefits ๐ŸŽ‰

  • Agnostic Integration: Seamlessly work with different state management libraries ๐Ÿ”„
  • Minimal Overhead: Lightweight adapter that doesn't compromise performance โšก
  • Flutter-Friendly: Maintains the natural flow and style of Flutter development ๐ŸŒŸ
  • Easy Migration: Simplify transitions between state management approaches ๐Ÿ›ฃ๏ธ
  • Decoupled Architecture: Keep your core application logic independent of state management details ๐Ÿงฉ
  • Future-Proof Development: Easily adapt to new state management trends and technologies ๐Ÿ”ฎ
  • Consistent Developer Experience: Maintain a uniform approach to state management across different parts of your application ๐Ÿง‘โ€๐Ÿ’ป

Example Concept ๐Ÿ“

// Unified state management interface
abstract class UniState<T> {
  T get state;
  void updateState(T newState);
  void dispose();
}
// Adapter for different state managers
class StateManagerAdapter<T> implements UniState<T> {
  // Adapt various state management systems
  // (BLoC, Provider, Cubit, etc.)
}

Getting Started ๐Ÿš€

Installation ๐Ÿ”ง

Add the following dependencies to your pubspec.yaml:

dependencies:
  unistate: ^latest_version
  unistate_bloc: ^latest_version

Basic Usage Example ๐Ÿ–ฅ๏ธ

Here's a simple counter app demonstrating UniState's flexibility:

// Define your state
class CounterState {
  final int count;
  CounterState({required this.count});
}

// Create a state manager implementing UniState
class CounterCubit extends Cubit<CounterState> implements UniState<CounterState> {
  CounterCubit() : super(CounterState(count: 0));

  @override
  void performAction(String actionType, [dynamic payload]) {
    switch (actionType) {
      case 'increment':
        emit(CounterState(count: state.count + 1));
        break;
      case 'decrement':
        emit(CounterState(count: state.count - 1));
        break;
    }
  }

  @override
  void updateState(CounterState newState) {
    emit(newState);
  }

  @override
  void resetState() {
    emit(CounterState(count: 0));
  }
}

// Use in a Flutter widget
class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final stateManager = UniStateProvider.of<CounterCubit, CounterState>(context);
  
    return Scaffold(
      body: Column(
        children: [
          ValueListenableBuilder(
            valueListenable: stateManager.asValueListenable(),
            builder: (context, state, child) {
              return Text('Count: ${state.count}');
            },
          ),
          ElevatedButton(
            onPressed: () => stateManager.performAction('increment'),
            child: Text('Increment'),
          ),
        ],
      ),
    );
  }
}

Key Concepts ๐Ÿง 

  • UniState Interface: Provides a consistent method for state management ๐Ÿ› ๏ธ
  • Flexible Adapters: Works with various state management libraries ๐Ÿ”„
  • Easy State Manipulation: Use performAction() for state changes ๐Ÿ”„

Supported State Managers โœ…

  • BLoC [WIP]

  • Cubit [WIP]

  • Provider

  • Riverpod

  • Custom State Management Solutions ๐Ÿ› ๏ธ

Migration and Compatibility ๐Ÿ”„

UniState makes it easy to:

  • Switch between state management approaches ๐Ÿ”„
  • Maintain consistent code structure ๐Ÿ“‚
  • Decouple application logic from state management details ๐Ÿงฉ

Troubleshooting ๐Ÿ› ๏ธ

  • Ensure you've imported the necessary packages ๐Ÿ“ฆ
  • Check that your state manager implements the UniState interface ๐Ÿ› ๏ธ
  • Use UniStateProvider to wrap your widget tree ๐ŸŒณ

Contributing ๐Ÿค

Contributions are welcome! Please feel free to submit a Pull Request ๐Ÿ”„.

License ๐Ÿ“œ

This project is licensed under the MIT License.

About

UniState is an adapter package designed to provide an agnostic approach to state management in Flutter, offering a unified interface that seamlessly integrates with various state management systems while maintaining Flutter's native code style.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published