Line data Source code
1 : import 'dart:math';
2 :
3 : import 'package:flutter/animation.dart';
4 : import 'package:flutter/cupertino.dart';
5 :
6 : abstract class TransitionContainerBuilder {
7 : final Curve curve;
8 :
9 1 : TransitionContainerBuilder(this.curve);
10 :
11 : Animation animation(AnimationController controller);
12 :
13 : Widget build(Animation animation);
14 : }
15 :
16 : class ScaleBuilder extends TransitionContainerBuilder {
17 : Widget child;
18 :
19 1 : @override
20 : Animation animation(AnimationController controller) {
21 2 : return CurvedAnimation(parent: controller, curve: curve);
22 : }
23 :
24 1 : @override
25 : Widget build(Animation animation) {
26 2 : return ScaleTransition(scale: animation, child: child);
27 : }
28 :
29 2 : ScaleBuilder({Curve curve, this.child}) : super(curve);
30 : }
31 :
32 : class SlideBuilder extends TransitionContainerBuilder {
33 : Widget child;
34 : final bool reverse;
35 :
36 2 : SlideBuilder({Curve curve, this.child, this.reverse}) : super(curve);
37 :
38 1 : @override
39 : Widget build(Animation animation) {
40 2 : return SlideTransition(position: animation, child: child);
41 : }
42 :
43 1 : @override
44 : Animation animation(AnimationController controller) {
45 1 : return Tween<Offset>(
46 1 : begin: reverse ? Offset.zero : const Offset(0.0, 2.0),
47 1 : end: reverse ? const Offset(0.0, 2.0) : Offset.zero,
48 3 : ).animate(CurvedAnimation(parent: controller, curve: curve));
49 : }
50 : }
51 :
52 : /// This flip animation is origin from [https://github.com/deven98/flip_box_bar/blob/master/lib/src/flip_box.dart]
53 : /// UX => 
54 : class FlipBuilder extends TransitionContainerBuilder {
55 : final Widget topChild;
56 : final Widget bottomChild;
57 : final double height;
58 :
59 1 : FlipBuilder(this.height, {Curve curve, this.topChild, this.bottomChild})
60 1 : : super(curve);
61 :
62 1 : @override
63 : Animation animation(AnimationController controller) {
64 3 : return Tween(begin: 0.0, end: pi / 2).animate(
65 2 : CurvedAnimation(parent: controller, curve: curve),
66 : );
67 : }
68 :
69 1 : @override
70 : Widget build(Animation animation) {
71 1 : return Container(
72 1 : child: Stack(
73 1 : children: <Widget>[
74 1 : Transform(
75 : alignment: Alignment.bottomCenter,
76 1 : transform: Matrix4.identity()
77 1 : ..setEntry(3, 2, 0.001)
78 6 : ..translate(0.0, (cos(animation.value) * (height / 2)),
79 5 : ((height / 2) * sin(animation.value)))
80 5 : ..rotateX(-(pi / 2) + animation.value),
81 1 : child: Container(
82 2 : child: Center(child: bottomChild),
83 : ),
84 : ),
85 4 : animation.value < (85 * pi / 180)
86 1 : ? Transform(
87 : alignment: Alignment.bottomCenter,
88 1 : transform: Matrix4.identity()
89 1 : ..setEntry(3, 2, 0.001)
90 1 : ..translate(
91 : 0.0,
92 6 : -(height / 2) * sin(animation.value),
93 5 : ((height / 2) * cos(animation.value)),
94 : )
95 2 : ..rotateX(animation.value),
96 1 : child: Container(
97 : alignment: Alignment.bottomCenter,
98 2 : child: Center(child: topChild),
99 : ),
100 : )
101 1 : : Container(),
102 : ],
103 : ),
104 : );
105 : }
106 : }
|