Skip to content

Instantly share code, notes, and snippets.

@Nash0x7E2

Nash0x7E2/main.dart

Created Apr 17, 2020
Embed
What would you like to do?
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