lifecycle_widget 0.0.2

Flutter Android iOS web

Flutter widget with lifecycle methods

Lifecycle widget #

Flutter widget with lifecycle methods

lesnitsky.dev GitHub stars Twitter Follow

Installation #

Add dependency to pubspec.yaml

dependencies:
  ...
  lifecycle_widget: ^0.0.2

Run in your terminal

flutter packages get

Example #

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() {
    // no access to context in willUnmount
    print('will unmount');
    super.willUnmount();
  }

  @override
  Widget build(BuildContext context) {
    return Center(child: Text("number is $number"));
  }
}

License #

MIT

lesnitsky.dev GitHub stars Twitter Follow