-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmutable_banner.widget.dart
231 lines (209 loc) · 6.71 KB
/
mutable_banner.widget.dart
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import 'package:figma_squircle/figma_squircle.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:safe/core.dart';
import 'package:safe/utils/constants/constants.util.dart';
import 'package:safe/widgets/mutable_button/mutable_button.widget.dart';
import 'package:safe/widgets/mutable_text/mutable_text.widget.dart';
enum MessageType {
error,
success,
warning,
}
class MutableBanner extends StatefulWidget {
final MessageType type;
final String title;
final BannerController? controller;
final Duration? duration;
final String description;
final void Function()? onSlideUp;
final void Function()? onTap;
final void Function()? onForward;
final void Function()? onReverse;
MutableBanner({
this.type = MessageType.success,
this.controller,
this.onSlideUp,
this.duration,
this.title = "",
this.description = "",
this.onTap,
this.onForward,
this.onReverse,
});
@override
State<MutableBanner> createState() => _MutableBannerState();
}
class _MutableBannerState extends State<MutableBanner>
with TickerProviderStateMixin {
late Core core;
late MediaQueryData queryData;
// Properties for animation
late AnimationController controller;
late Animation positionAnimation;
late Animation opacityAnimation;
late Animation scaleAnimation;
double topPosition = -(kMutableBannerHeight + 1);
double opacity = 0;
bool dismissed = false;
@override
void initState() {
super.initState();
core = Provider.of<Core>(context, listen: false);
// Used to attach external controller
if (widget.controller != null) {
widget.controller!.setState(this);
}
// Initialize controller
controller = AnimationController(
vsync: this,
duration: kMutableBannerDuration,
);
// Initialize animations
positionAnimation =
Tween<double>(begin: -(kMutableBannerHeight + 1), end: 0.0).animate(
CurvedAnimation(parent: controller, curve: Curves.decelerate),
);
opacityAnimation = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: controller, curve: Curves.easeOutCirc),
);
// Listens for any changes
controller.addListener(() {
setState(() {
topPosition = positionAnimation.value;
opacity = opacityAnimation.value;
});
});
}
Future<void> show() async {
dismissed = false;
if (widget.onForward != null) {
widget.onForward!();
}
await controller.forward();
await Future.delayed(widget.duration ?? Duration(seconds: 5));
if (!dismissed) {
if (widget.onReverse != null) {
widget.onReverse!();
}
await controller.reverse();
}
}
Future<void> dismiss() async {
dismissed = true;
if (widget.onReverse != null) {
widget.onReverse!();
}
await controller.reverse();
}
@override
Widget build(BuildContext context) {
queryData = MediaQuery.of(context);
return Positioned(
top: topPosition,
child: Opacity(
opacity: opacity,
child: Container(
width: queryData.size.width,
height: kMutableBannerHeight,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
kColorMap[MutableColor.neutral10]!
.withOpacity(kTransparencyMap[Transparency.v80]!),
kColorMap[MutableColor.neutral10]!.withOpacity(0),
],
),
),
padding: EdgeInsets.symmetric(
horizontal: kSideScreenMargin,
),
child: Align(
alignment: Alignment.bottomCenter,
child: MutableButton(
onTap: () {
dismiss();
if (widget.onTap != null) {
widget.onTap!();
}
},
onSlide: () {
dismiss();
if (widget.onSlideUp != null) {
widget.onSlideUp!();
}
},
child: Container(
width: double.infinity,
height: 64,
padding: EdgeInsets.symmetric(horizontal: 15),
decoration: ShapeDecoration(
color: core.utils.color.translucify(
widget.type == MessageType.success
? MutableColor.secondaryGreen
: (widget.type == MessageType.error)
? MutableColor.secondaryRed
: MutableColor.secondaryYellow,
Transparency.v20,
),
shape: SmoothRectangleBorder(
borderRadius: SmoothBorderRadius(
cornerSmoothing: kCornerSmoothing,
cornerRadius: 16,
),
side: BorderSide(
color: kColorMap[widget.type == MessageType.success
? MutableColor.secondaryGreen
: (widget.type == MessageType.error)
? MutableColor.secondaryRed
: MutableColor.secondaryYellow]!,
width: kBorderWidth,
),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
MutableText(
widget.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
weight: TypeWeight.semiBold,
style: TypeStyle.body,
),
SizedBox(height: 2),
MutableText(
widget.description,
maxLines: 1,
size: 13,
overflow: TextOverflow.ellipsis,
weight: TypeWeight.regular,
color: MutableColor.neutral2,
),
],
),
),
),
),
),
),
);
}
}
class BannerController {
_MutableBannerState? _state;
// ignore: library_private_types_in_public_api
void setState(_MutableBannerState state) => _state = state;
bool get isAttached => _state != null;
Future<void> show() async {
assert(_state != null, "Controller has not been attached");
await _state!.show();
}
Future<void> dismiss() async {
assert(_state != null, "Controller has not been attached");
await _state!.dismiss();
}
}