Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active January 27, 2023 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlugFox/4d6c3554d0962f2abeaa4776d4f60495 to your computer and use it in GitHub Desktop.
Save PlugFox/4d6c3554d0962f2abeaa4776d4f60495 to your computer and use it in GitHub Desktop.
StatelessWidget & StatelessElement as a StatefulWidget
import 'package:flutter/widgets.dart';
class Example extends StatelessWidget {
const Example({super.key});
@override
StatelessElement createElement() => ExampleElement(this);
@override
Widget build(BuildContext context) => const Placeholder();
}
class ExampleElement extends StatelessElement {
ExampleElement(this.widget) : super(widget);
@override
StatelessWidget widget;
@override
void mount(Element? parent, Object? newSlot) {
super.mount(parent, newSlot);
print('initState');
}
@override
void update(covariant Example newWidget) {
super.update(newWidget);
print('didUpdateWidget');
}
@override
void unmount() {
print('dispose');
super.unmount();
}
void setState(VoidCallback fn) {
fn();
markNeedsBuild();
}
@override
Widget build() => widget.build(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment