-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle_sweep_transition_animation
More file actions
127 lines (107 loc) · 3.51 KB
/
rectangle_sweep_transition_animation
File metadata and controls
127 lines (107 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import 'dart:async';
import 'dart:developer';
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
_controller = AnimationController(vsync: this, duration: Duration(seconds: 1))..forward();
Timer.periodic(Duration(seconds: 4), (timer) {_controller.forward(from: 0.0);});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(body: AnimatedLine(_controller));
}
}
class AnimatedLine extends StatefulWidget {
final AnimationController controller;
AnimatedLine(this.controller);
@override
_AnimatedLineState createState() => _AnimatedLineState();
}
class _AnimatedLineState extends State<AnimatedLine> {
AnimationController _controller;
double _progress;
@override
void initState() {
_progress = 0.0;
_controller = widget.controller;
_controller.addListener(() {
setState(() {
_progress = _controller.value;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: TestPainter(_progress),
child: Container(),
);
}
}
///Transition the pages in between the controllers values 0.5 to 0.8
///The entire screen is covered during this time
///Put the painter on a stack with the pages/routes and call forward()
class TestPainter extends CustomPainter {
double progress;
TestPainter(this.progress);
@override
void paint(Canvas canvas, Size size) {
var lineSize = size.width - 60;
var paint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
var paintLight = Paint()
// ..color = Colors.teal
..color = Colors.red.withAlpha(150)
..style = PaintingStyle.fill;
var bottomOffset = Offset(size.width, (1-progress.animatedInRange(0.8, 1.0)) * size.height);
var topOffset = Offset(0.0, size.height * (1 - progress.animatedInRange(0.0, 0.5)));
var lightTopOffset = Offset(0.0, size.height * (1 - progress.animatedInRange(0.0, 0.2)));
var lightBottomOffset = Offset(size.width, (1-progress.animatedInRange(0.8, 1.0))*size.height);
canvas.drawRect(Rect.fromPoints(lightTopOffset, lightBottomOffset), paintLight);
canvas.drawRect(Rect.fromPoints(bottomOffset, topOffset), paint);
}
@override
bool shouldRepaint(TestPainter oldDelegate) {
return this.progress != oldDelegate.progress;
}
}
extension RangeExtention on double {
///Makes an animation(double) go from zero to one while the given value is between the bound
///If the number is smaller than the lower range, return zero; if its greater than the upper range,
///return 1 otherwise return (original value - lower range) scaled between upper and lower range
double animatedInRange(double lowerRange, double upperRange) {
if (this < lowerRange)
return 0.0;
else if (this > upperRange)
return 1.0;
else
return (this - lowerRange) / (upperRange - lowerRange);
}
}