Last active
January 27, 2023 08:46
-
-
Save PlugFox/4d6c3554d0962f2abeaa4776d4f60495 to your computer and use it in GitHub Desktop.
StatelessWidget & StatelessElement as a StatefulWidget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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