Skip to content

Instantly share code, notes, and snippets.

@Nash0x7E2
Created April 17, 2020 00:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nash0x7E2/49ca9ebba8957d66968f710e50d10941 to your computer and use it in GitHub Desktop.
Save Nash0x7E2/49ca9ebba8957d66968f710e50d10941 to your computer and use it in GitHub Desktop.
Apple's Breathe Animation in Flutter
// MIT License
//
// Copyright (c) 2020 Neevash Ramdial
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Home(),
),
);
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: SizedBox.expand(child: CupertinoBreathe()),
);
}
}
class CupertinoBreathe extends StatefulWidget {
@override
_CupertinoBreatheState createState() => _CupertinoBreatheState();
}
class _CupertinoBreatheState extends State<CupertinoBreathe>
with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..repeat(reverse: true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: AspectRatio(
aspectRatio: 1.0,
child: CustomPaint(
painter: _BreathePainter(
CurvedAnimation(parent: _controller, curve: Curves.ease),
),
size: Size.infinite,
),
),
);
}
}
class _BreathePainter extends CustomPainter {
_BreathePainter(
this.animation, {
this.count = 6,
Color color = const Color(0xFF61bea2),
}) : circlePaint = Paint()
..color = color
..blendMode = BlendMode.screen,
super(repaint: animation);
final Animation<double> animation;
final int count;
final Paint circlePaint;
@override
void paint(Canvas canvas, Size size) {
final center = size.center(Offset.zero);
final radius = (size.shortestSide * 0.25) * animation.value;
for (int index = 0; index < count; index++) {
final indexAngle = (index * math.pi / count * 2);
final angle = indexAngle + (math.pi * 1.5 * animation.value);
final offset = Offset(math.sin(angle), math.cos(angle)) * radius * 0.985;
canvas.drawCircle(center + offset * animation.value, radius, circlePaint);
}
}
@override
bool shouldRepaint(_BreathePainter oldDelegate) =>
animation != oldDelegate.animation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment