stateful_widget_binder 1.0.0
stateful_widget_binder: ^1.0.0 copied to clipboard
A lightweight, type-safe Flutter package to bind controllers to StatefulWidget with automatic lifecycle management and reactivity.
example/lib/main.dart
import 'package:stateful_widget_binder/stateful_widget_binder.dart';
void main() {
runApp(const MaterialApp(home: HomePage()));
}
/// A simple controller for the example app.
class CounterController extends ListenableController {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
void decrement() {
_count--;
notifyListeners();
}
@override
void dispose() {
debugPrint('CounterController disposed');
super.dispose();
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return ExampleWidget(controller: CounterController());
}
}
class ExampleWidget extends StatefulWidgetBinder {
const ExampleWidget({super.key, required super.controller});
@override
State<ExampleWidget> createState() => _ExampleWidgetState();
}
class _ExampleWidgetState extends StateController<ExampleWidget, CounterController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('StatefulWidgetBinder Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Count:'),
Text('${controller.count}', style: Theme.of(context).textTheme.headlineMedium),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(onPressed: controller.decrement, child: const Icon(Icons.remove)),
const SizedBox(width: 20),
ElevatedButton(onPressed: controller.increment, child: const Icon(Icons.add)),
],
),
],
),
),
);
}
}