Last active
November 13, 2024 19:37
-
-
Save fonkamloic/913ab636ef14329d4679064df1cbf707 to your computer and use it in GitHub Desktop.
Gist of Widget caching. Original thread with explanation from https://x.com/pierremartin_/status/1853394898361233485?s=46
This file contains hidden or 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
class WidgetCache<T> extends StatefulWidget { | |
const WidgetCache({ | |
super.key, | |
required this.value, | |
required this.builder, | |
}); | |
final T value; | |
final Widget Function(BuildContext context, T value) builder; | |
@override | |
WidgetCacheState<T> createState() => WidgetCacheState<T>(); | |
} | |
class WidgetCacheState<T> extends State<WidgetCache<T>> { | |
late Widget cache; | |
T? previousValue; | |
@override | |
Widget build(BuildContext context) { | |
if (identical(widget.value, previousValue) == false) { | |
previousValue = widget.value; | |
cache = Builder( | |
builder: (context) => widget.builder(context, widget.value), | |
); | |
} | |
return cache; | |
} | |
} |
Author
fonkamloic
commented
Nov 13, 2024
Original question: Can we discuss widget catching and the FPS performance we observe, focusing particularly on drawbacks, if any?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment