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