Last active
February 10, 2019 20:28
-
-
Save jogboms/813d48a72e933b2a5e35da4d22fda3b0 to your computer and use it in GitHub Desktop.
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 OneTimeBuilder extends StatefulWidget { | |
const OneTimeBuilder({ | |
Key key, | |
@required this.once, | |
@required this.child, | |
}) : assert(once != null), | |
assert(child != null), | |
super(key: key); | |
final VoidCallback once; | |
final Widget child; | |
@override | |
_OneTimeBuilderState createState() => _OneTimeBuilderState(); | |
} | |
class _OneTimeBuilderState extends State<OneTimeBuilder> { | |
bool _hasBuilt = false; | |
@override | |
Widget build(BuildContext context) { | |
if (!_hasBuilt) { | |
_hasBuilt = true; | |
widget.once(); | |
} | |
return widget.child; | |
} | |
} |
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 OneTimeCacheBuilder<T> extends StatefulWidget { | |
const OneTimeCacheBuilder({ | |
Key key, | |
@required this.once, | |
@required this.builder, | |
}) : assert(once != null), | |
assert(builder != null), | |
super(key: key); | |
final T Function() once; | |
final Widget Function(BuildContext context, T cache) builder; | |
@override | |
_OneTimeCacheBuilderState<T> createState() => _OneTimeCacheBuilderState<T>(); | |
} | |
class _OneTimeCacheBuilderState<T> extends State<OneTimeCacheBuilder<T>> { | |
bool _hasBuilt = false; | |
T _cache; | |
@override | |
Widget build(BuildContext context) { | |
if (!_hasBuilt) { | |
_hasBuilt = true; | |
_cache = widget.once(); | |
} | |
return widget.builder(context, _cache); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment