Flutter widget with lifecycle methods
Add dependency to pubspec.yaml
dependencies:
...
lifecycle_widget: ^1.0.0
Run in your terminal
flutter packages get
class TestWidget extends LifecycleWidget {
final int number;
const TestWidget({Key key, this.number}) : super(key: key);
void notify(BuildContext context, String text) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(text),
));
}
@override
void didMount(BuildContext context) {
notify(context, 'did mount');
super.didMount(context);
}
@override
void didUpdate(
BuildContext context,
covariant TestWidget oldWidget,
covariant TestWidget widget,
) {
notify(context, 'update ${oldWidget.number} => ${widget.number}');
super.didUpdate(context, oldWidget, widget);
}
@override
void willUnmount(BuildContext context) {
print('will unmount');
super.willUnmount(context);
}
@override
Widget build(BuildContext context) {
return Center(child: Text("number is $number"));
}
}
MIT