quick_change 1.0.0 quick_change: ^1.0.0 copied to clipboard
A simple, lightweight quick state-management package for Flutter.
quick_change ๐งฉ #
A lightweight, intuitive state management solution for Flutter.
Effortlessly manage state transitions with simple, type-safe states and powerful listening and building capabilities.
quick_state
is designed to simplify state management in Flutter apps, making it suitable for both small and medium-scale applications, with the flexibility to extend for large apps. This package provides easy-to-use state controllers, built-in state types, and intuitive UI listening and building capabilities.
Features โจ #
- Simple and Lightweight: Manage state without heavy dependencies.
- Flexible States: Use predefined states like
QuickLoading
,QuickData
, andQuickError
. - Easy Listeners: React to state changes with a clean, intuitive listener API.
- Modular Design: Suitable for modular and scalable Flutter applications.
- Optimized for Performance: Only rebuilds widgets when necessary.
Table of Contents ๐ #
Installation #
Add quick_change
to your pubspec.yaml
:
dependencies:
quick_change: ^1.0.0
Then run:
flutter pub get
Quick Start ๐ #
- Define a
QuickChangeController
to manage your state. - Use
QuickChangeBuilder
to listen to state changes and update the UI. - Use
.quickListen
to respond to state changes outside of rebuilds.
API Overview #
QuickStateController
: Manages state changes and notifies listeners.- States:
QuickInitial
: Represents the initial state.QuickLoading
: Represents a loading state.QuickData<T>
: Holds data of typeT
.QuickError
: Represents an error state with a message.
QuickStateBuilder
: A widget that builds UI based on the controllerโs state..quickListen
: An extension on widgets to listen to state changes without rebuilding.
Examples #
Basic Counter Example #
Hereโs how to create a simple counter using quick_state
.
import 'package:flutter/material.dart';
import 'package:quick_change/quick_change.dart';
class CounterScreen extends StatefulWidget {
@override
_CounterScreenState createState() => _CounterScreenState();
}
class _CounterScreenState extends State<CounterScreen> {
final QuickChangeController<int> counterController = QuickChangeController<int>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Counter Example")),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
QuickChangeBuilder<int>(
controller: counterController,
onInitial: (context) => Text("Welcome! Tap the button to start."),
onLoading: (context) => CircularProgressIndicator(),
onData: (context, data) => Text("Counter: $data", style: TextStyle(fontSize: 24)),
onError: (context, message) => Text("Error: $message"),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
counterController.setLoading();
Future.delayed(Duration(seconds: 1), () {
final currentValue = counterController.getCurrentData() ?? 0;
counterController.setData(currentValue + 1);
});
},
child: Text("Increment Counter"),
),
],
),
);
}
}
Listening for Errors #
You can use .quickListen
to display a Custom message such as SnackBar
when an error occurs.
// Listen for errors
Widget.quickListen(
controller: counterController,
listener: (state) {
if (state is QuickError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.message)),
);
}
},
);
}
Advanced Usage #
Custom State Types #
Define custom states by extending QuickState
.
class QuickSuccess extends QuickState {
final String message;
QuickSuccess(this.message);
}
counterController.setSuccess(String message) {
_state = QuickSuccess(message);
notifyListeners();
}
Middleware and Logging #
Add logging to track state changes.
void quickListenWithLogging<T>({
required QuickStateController<T> controller,
required void Function(QuickState state) listener,
}) {
controller.addListener(() {
print("State changed to: ${controller.state}");
listener(controller.state);
});
}
Contributing ๐ค #
We welcome contributions! Please read our Contributing Guide for more details on how to get involved.
License ๐ #
This project is licensed under the MIT License. See the LICENSE file for more details.
Happy coding! ๐๐ With quick_state & Farooq
, managing Flutter state transitions has never been simpler.