Provide the builder state to illustrations below, reduces boilerplate when adding pieces.

This commit is contained in:
Shawn 2022-11-28 15:39:37 -07:00
parent da59938c13
commit 859f76d8ce

View File

@ -13,37 +13,39 @@ class WonderIllustrationBuilder extends StatefulWidget {
required this.fgBuilder, required this.fgBuilder,
required this.mgBuilder, required this.mgBuilder,
required this.bgBuilder, required this.bgBuilder,
required this.wonderType,
}) : super(key: key); }) : super(key: key);
final List<Widget> Function(BuildContext context, Animation<double> animation) fgBuilder; final List<Widget> Function(BuildContext context, Animation<double> animation) fgBuilder;
final List<Widget> Function(BuildContext context, Animation<double> animation) mgBuilder; final List<Widget> Function(BuildContext context, Animation<double> animation) mgBuilder;
final List<Widget> Function(BuildContext context, Animation<double> animation) bgBuilder; final List<Widget> Function(BuildContext context, Animation<double> animation) bgBuilder;
final WonderIllustrationConfig config; final WonderIllustrationConfig config;
final WonderType wonderType;
@override @override
State<WonderIllustrationBuilder> createState() => _WonderIllustrationBuilderState(); State<WonderIllustrationBuilder> createState() => WonderIllustrationBuilderState();
} }
class _WonderIllustrationBuilderState extends State<WonderIllustrationBuilder> with SingleTickerProviderStateMixin { class WonderIllustrationBuilderState extends State<WonderIllustrationBuilder> with SingleTickerProviderStateMixin {
late final _anim = AnimationController(vsync: this, duration: $styles.times.med * .75) late final anim = AnimationController(vsync: this, duration: $styles.times.med * .75)
..addListener(() => setState(() {})); ..addListener(() => setState(() {}));
bool get isShowing => widget.config.isShowing; bool get isShowing => widget.config.isShowing;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
if (isShowing) _anim.forward(from: 0); if (isShowing) anim.forward(from: 0);
} }
@override @override
void dispose() { void dispose() {
_anim.dispose(); anim.dispose();
super.dispose(); super.dispose();
} }
@override @override
void didUpdateWidget(covariant WonderIllustrationBuilder oldWidget) { void didUpdateWidget(covariant WonderIllustrationBuilder oldWidget) {
if (isShowing != oldWidget.config.isShowing) { if (isShowing != oldWidget.config.isShowing) {
isShowing ? _anim.forward(from: 0) : _anim.reverse(from: 1); isShowing ? anim.forward(from: 0) : anim.reverse(from: 1);
} }
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
} }
@ -51,13 +53,19 @@ class _WonderIllustrationBuilderState extends State<WonderIllustrationBuilder> w
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Optimization: no need to return all of these children if the widget is fully invisible. // Optimization: no need to return all of these children if the widget is fully invisible.
if (_anim.value == 0 && widget.config.enableAnims) return SizedBox.expand(); if (anim.value == 0 && widget.config.enableAnims) return SizedBox.expand();
Animation<double> anim = widget.config.enableAnims ? _anim : AlwaysStoppedAnimation(1); Animation<double> animation = widget.config.enableAnims ? anim : AlwaysStoppedAnimation(1);
return Stack(key: ValueKey(anim.value == 0), children: [ return Provider<WonderIllustrationBuilderState>.value(
if (widget.config.enableBg) ...widget.bgBuilder(context, _anim), value: this,
if (widget.config.enableMg) ...widget.mgBuilder(context, _anim), child: Stack(
if (widget.config.enableFg) ...widget.fgBuilder(context, _anim), key: ValueKey(animation.value == 0),
]); children: [
if (widget.config.enableBg) ...widget.bgBuilder(context, animation),
if (widget.config.enableMg) ...widget.mgBuilder(context, animation),
if (widget.config.enableFg) ...widget.fgBuilder(context, animation),
],
),
);
} }
} }