Last active
January 22, 2025 19:39
-
-
Save callmephil/a79db481fe9e7aca2a775a34d7416e2f to your computer and use it in GitHub Desktop.
Widget Caching (Pierre Martin)
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class ChildBuilder extends StatelessWidget { | |
const ChildBuilder({ | |
super.key, | |
required this.builder, | |
this.child, | |
}); | |
final TransitionBuilder builder; | |
final Widget? child; | |
@override | |
Widget build(BuildContext context) => builder(context, child); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: ChildBuilder( | |
builder: (context, child) { | |
return SizedBox( | |
height: MediaQuery.sizeOf(context).height, child: child); | |
}, | |
child: Center( | |
child: Text('Hello, World!'), | |
), | |
), | |
), | |
); | |
} | |
} |
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
import 'package:flutter/widgets.dart'; | |
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 | |
State<WidgetCache<T>> createState() => _WidgetCacheState<T>(); | |
} | |
class _WidgetCacheState<T> extends State<WidgetCache<T>> { | |
late Widget cache; | |
T? previousValue; | |
@override | |
Widget build(BuildContext _) { | |
if (!identical(widget.value, previousValue)) { | |
previousValue = widget.value; | |
cache = Builder( | |
builder: (context) { | |
return widget.builder(context, widget.value); | |
}, | |
); | |
} | |
return cache; | |
} | |
} |
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int index = 0; | |
String text = 'Some text'; | |
@override | |
void initState() { | |
super.initState(); | |
print('init state'); | |
WidgetsBinding.instance.addPostFrameCallback((_) { | |
print('first rebuild starts'); | |
setState(() {}); | |
print('first rebuild ends'); | |
Future.delayed(Duration(seconds: 2), () { | |
print('second rebuild'); | |
setState(() { | |
index = 1; | |
text = 'Some other text'; | |
}); | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: WidgetCache( | |
value: index, | |
builder: (context, value) { | |
return Text(text); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} | |
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 | |
State<WidgetCache<T>> createState() => WidgetCacheState<T>(); | |
} | |
class WidgetCacheState<T> extends State<WidgetCache<T>> { | |
late Widget cache; | |
T? previousValue; | |
@override | |
Widget build(BuildContext _) { | |
if (!identical(widget.value, previousValue)) { | |
print('not identical'); | |
previousValue = widget.value; | |
cache = Builder( | |
builder: (context) { | |
return widget.builder(context, widget.value); | |
}, | |
); | |
} | |
return cache; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment