Skip to content

Instantly share code, notes, and snippets.

@esDotDev
Last active February 18, 2022 19:47
Show Gist options
  • Save esDotDev/2214ff7c41a11bb9affb445dbea1a093 to your computer and use it in GitHub Desktop.
Save esDotDev/2214ff7c41a11bb9affb445dbea1a093 to your computer and use it in GitHub Desktop.
class Benchmark extends StatefulWidget {
const Benchmark({Key? key}) : super(key: key);
@override
_BenchmarkState createState() => _BenchmarkState();
}
class _BenchmarkState extends State<Benchmark> {
double _tweenSliderValue = .2;
bool _enableRendering = false;
bool _useGTween = false;
int get _tweenCount => 10 + (_tweenSliderValue * 5000).round();
@override
Widget build(BuildContext context) {
final rnd = Random(0);
return Center(
child: SizedBox(
width: 400,
height: 400,
child: Stack(children: [
...List.generate(_tweenCount, (index) {
return Positioned(
top: 20.0 + rnd.nextInt(300),
left: 20.0 + rnd.nextInt(300),
child: Opacity(
opacity: _enableRendering ? 1 : 0,
child: buildLogo(),
),
);
}),
Column(
children: [
CheckboxListTile(
title: Text('Show Tweens'),
value: _enableRendering,
onChanged: _handleShowTweensToggled,
),
CheckboxListTile(
title: Text('use gtween'),
value: _useGTween,
onChanged: _handleUseGTweenToggled,
),
Row(
children: [
Expanded(child: Slider(value: _tweenSliderValue, onChanged: _handleSliderChanged)),
Text('$_tweenCount'),
],
),
],
)
]),
),
);
}
StatefulWidget _buildLogo() => !_useGTween
? _FadeInOut()
: const FlutterLogo().gTweener.fade().withInit(
(controller) => controller.animation.repeat(reverse: true),
);
void _handleSliderChanged(v) => setState(() => _tweenSliderValue = v);
void _handleShowTweensToggled(_) => setState(() => _enableRendering = !_enableRendering);
void _handleUseGTweenToggled(_) => setState(() => _useGTween = !_useGTween);
}
/// ///////////////////////////////////
/// Vanilla Flutter Version
class _FadeInOut extends StatefulWidget {
@override
__FadeInOutState createState() => __FadeInOutState();
}
class __FadeInOutState extends State<_FadeInOut> with SingleTickerProviderStateMixin {
late final _anim = AnimationController(vsync: this, duration: .3.seconds)..repeat(reverse: true);
@override
void dispose() {
_anim.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => FadeTransition(opacity: _anim, child: const FlutterLogo());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment