Skip to content

Instantly share code, notes, and snippets.

@fonkamloic
Last active November 13, 2024 19:37
Show Gist options
  • Save fonkamloic/913ab636ef14329d4679064df1cbf707 to your computer and use it in GitHub Desktop.
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
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;
}
}
@fonkamloic
Copy link
Author

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