flutter_compose 0.0.4%2B1
SDKflutter
Platformandroidioswindowslinuxmacosweb
Inspired by Vue Composables, flutter_compose offers a streamlined way to create reusable components in Flutter. Improve code organization and reduce boilerplate.
Flutter Compose
Inspired by Vue Composables, Flutter Compose streamlines the creation and management of reusable components in Flutter applications.
By encapsulating stateful logic and separating concerns, Flutter Compose helps you build more modular, maintainable, and efficient Flutter apps.
Key features:
- Reusable components: Create and reuse
Composableobjects to encapsulate complex logic and improve code organization. - Simplified state management: Manage state within
Composableobjects, reducing boilerplate and making your code easier to understand. - Improved code structure: Separate component logic from widget rendering for better maintainability and testability.
- Enhanced flexibility: Customize your
Composableobjects to fit your specific needs and project requirements.
Usage:
- Create a Composable: Define a class that extends
Composableand implements the necessary methods.
class ValueNotifierComposable<T> extends Composable<ValueNotifier<T>> {
ValueNotifierComposable(this.value);
final T value;
late final ValueNotifier<T> notifier;
@override
void onInit() {
notifier = ValueNotifier(value);
notifier.addListener(notifyListeners);
super.onInit();
}
@override
void onDispose() {
notifier.removeListener(notifyListeners);
super.onDispose();
}
@override
ValueNotifier<T> build() => notifier;
}
- Make it available to the Use instance: Use the
valueNotifierextension method to attach yourComposableto aUseinstance.
extension ValueNotifierComposableExtension on Use {
ValueNotifier<T> valueNotifier<T>(T value) {
return attach(ValueNotifierComposable(value));
}
}
- Create ComposeWidget and access the Composable: Access the
Composableobject within yourComposeWidget'sbuildmethod.
class CounterWidget extends ComposeWidget {
const CounterWidget({super.key});
@override
Widget build(BuildContext context, Use use) {
final counter = use.valueNotifier(0);
return Scaffold(
body: Center(
child: Text('${counter.value}'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
counter.value++;
},
child: const Icon(Icons.add),
),
);
}
}
Benefits of Flutter Compose:
- Enhanced reusability:
Composableobjects can be easily reused across different parts of your application. - Improved maintainability: By separating component logic from widget rendering, your code becomes more modular and easier to understand and maintain.
- Simplified state management:
Composableobjects can handle their own state management, reducing the complexity of managing state within your widgets.
Additional notes:
- The
ValueNotifierComposableexample demonstrates how to use aValueNotifierto manage state within aComposable. You can customize this approach to suit your specific use cases. - Flutter Compose offers flexibility in how you structure your
Composableobjects and integrate them into your application's architecture.