Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Last active January 9, 2025 10:38
Show Gist options
  • Save MelbourneDeveloper/66e5257c4d17e29172241039e3f90b74 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/66e5257c4d17e29172241039e3f90b74 to your computer and use it in GitHub Desktop.
Forward Piping with Rotating Animation
// ignore_for_file: unreachable_from_main
import 'dart:math';
import 'package:flutter/material.dart';
extension ForwardPipeOperator<T extends Widget, T2 extends Widget> on T
Function(Widget) {
/// Returns a new function that applies the given transformation to the result
/// of this function
T Function(T2) operator |(Widget Function(Widget) next) =>
(w) => this(next(w));
}
// These functions take advantage of Dart's tear-off syntax to achieve a
// a similar result to F#'s forward pipe operator
MaterialApp materialApp(Widget home) => MaterialApp(
debugShowCheckedModeBanner: false,
home: home,
);
Scaffold scaffold(Widget body) => Scaffold(
body: body,
);
Center center(Widget body) => Center(
child: body,
);
// This is the cool part!
void main() => runApp(
( materialApp |
scaffold |
Rotating.new |
center )(const Text('Hello world')),
);
class Rotating extends StatefulWidget {
// ignore: public_member_api_docs, use_key_in_widget_constructors
const Rotating(this.child);
final Widget child;
@override
State<Rotating> createState() => _RotatingState();
}
class _RotatingState extends State<Rotating>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
)..repeat();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
builder: (context, child) => Transform.rotate(
angle: _controller.value * 2 * pi,
child: Transform.scale(
scale: 0.8 + (_controller.value * 0.4),
child: child,
),
),
child: widget.child,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment