From 859f76d8ce7cb3cbb6c2cccc8caee5d0228c5874 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 28 Nov 2022 15:39:37 -0700 Subject: [PATCH] Provide the builder state to illustrations below, reduces boilerplate when adding pieces. --- .../common/wonder_illustration_builder.dart | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/ui/wonder_illustrations/common/wonder_illustration_builder.dart b/lib/ui/wonder_illustrations/common/wonder_illustration_builder.dart index 58041910..a0358851 100644 --- a/lib/ui/wonder_illustrations/common/wonder_illustration_builder.dart +++ b/lib/ui/wonder_illustrations/common/wonder_illustration_builder.dart @@ -13,37 +13,39 @@ class WonderIllustrationBuilder extends StatefulWidget { required this.fgBuilder, required this.mgBuilder, required this.bgBuilder, + required this.wonderType, }) : super(key: key); final List Function(BuildContext context, Animation animation) fgBuilder; final List Function(BuildContext context, Animation animation) mgBuilder; final List Function(BuildContext context, Animation animation) bgBuilder; final WonderIllustrationConfig config; + final WonderType wonderType; @override - State createState() => _WonderIllustrationBuilderState(); + State createState() => WonderIllustrationBuilderState(); } -class _WonderIllustrationBuilderState extends State with SingleTickerProviderStateMixin { - late final _anim = AnimationController(vsync: this, duration: $styles.times.med * .75) +class WonderIllustrationBuilderState extends State with SingleTickerProviderStateMixin { + late final anim = AnimationController(vsync: this, duration: $styles.times.med * .75) ..addListener(() => setState(() {})); bool get isShowing => widget.config.isShowing; @override void initState() { super.initState(); - if (isShowing) _anim.forward(from: 0); + if (isShowing) anim.forward(from: 0); } @override void dispose() { - _anim.dispose(); + anim.dispose(); super.dispose(); } @override void didUpdateWidget(covariant WonderIllustrationBuilder oldWidget) { 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); } @@ -51,13 +53,19 @@ class _WonderIllustrationBuilderState extends State w @override Widget build(BuildContext context) { // 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(); - Animation anim = widget.config.enableAnims ? _anim : AlwaysStoppedAnimation(1); + if (anim.value == 0 && widget.config.enableAnims) return SizedBox.expand(); + Animation animation = widget.config.enableAnims ? anim : AlwaysStoppedAnimation(1); - return Stack(key: ValueKey(anim.value == 0), children: [ - if (widget.config.enableBg) ...widget.bgBuilder(context, _anim), - if (widget.config.enableMg) ...widget.mgBuilder(context, _anim), - if (widget.config.enableFg) ...widget.fgBuilder(context, _anim), - ]); + return Provider.value( + value: this, + child: Stack( + 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), + ], + ), + ); } }