-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathblob.dart
163 lines (149 loc) · 3.85 KB
/
blob.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
import 'dart:async';
import 'package:blobs/blobs.dart';
import 'package:blobs/src/config.dart';
import 'package:blobs/src/models.dart';
import 'package:blobs/src/services/blob_generator.dart';
import 'package:blobs/src/widgets/animated_blob.dart';
import 'package:blobs/src/widgets/simple_blob.dart';
import 'package:flutter/material.dart';
class Blob extends StatefulWidget {
final double size;
final bool debug;
final BlobStyles? styles;
final BlobController? controller;
final Widget? child;
final int? edgesCount;
final int? minGrowth;
final List<String>? id;
final Duration? duration;
final bool loop;
final bool isAnimated;
final Curve? animationCurve;
static int count = 0;
Blob.random({
required this.size,
this.edgesCount = BlobConfig.edgesCount,
this.minGrowth = BlobConfig.minGrowth,
this.debug = false,
this.styles,
this.controller,
this.child,
}) : loop = false,
id = null,
duration = null,
isAnimated = false,
animationCurve = null;
Blob.animatedRandom({
required this.size,
this.edgesCount = BlobConfig.edgesCount,
this.minGrowth = BlobConfig.minGrowth,
this.debug = false,
this.styles,
this.duration = const Duration(
milliseconds: BlobConfig.animDurationMs,
),
this.loop = false,
this.controller,
this.child,
this.animationCurve = Curves.easeInOutCubic,
}) : isAnimated = true,
id = null;
Blob.fromID({
required this.id,
required this.size,
this.debug = false,
this.styles,
this.controller,
this.child,
}) : loop = false,
edgesCount = null,
minGrowth = null,
duration = null,
isAnimated = false,
animationCurve = null;
Blob.animatedFromID({
required this.id,
required this.size,
this.debug = false,
this.styles,
this.duration = const Duration(
milliseconds: BlobConfig.animDurationMs,
),
this.loop = false,
this.controller,
this.child,
this.animationCurve = Curves.easeInOutCubic,
}) : isAnimated = true,
edgesCount = null,
minGrowth = null;
@override
_BlobState createState() => _BlobState();
BlobData _randomBlobData() {
String? randomID = (id == null || id!.isEmpty) ? null : _randomID();
return BlobGenerator(
edgesCount: edgesCount,
minGrowth: minGrowth,
size: Size(size, size),
id: randomID,
).generate();
}
String _randomID() {
Blob.count++;
if (id!.length == 1) return id![0];
return id![Blob.count % id!.length];
}
}
class _BlobState extends State<Blob> {
BlobData? blobData;
BlobData? fromBlobData;
Timer? timer;
@override
void initState() {
super.initState();
_updateBlob();
if (widget.loop) {
timer = Timer.periodic(
Duration(milliseconds: widget.duration!.inMilliseconds),
(_) => _updateBlob(),
);
} else if (widget.controller != null) {
widget.controller!.onChange(_updateBlob);
}
}
@override
Widget build(BuildContext context) {
if (!widget.isAnimated) {
return SimpleBlob(
blobData: blobData!,
size: widget.size,
styles: widget.styles,
debug: widget.debug,
child: widget.child,
);
}
return AnimatedBlob(
fromBlobData: fromBlobData,
toBlobData: blobData!,
size: widget.size,
styles: widget.styles,
debug: widget.debug,
duration: widget.duration,
child: widget.child,
animationCurve: widget.animationCurve,
);
}
BlobData _updateBlob() {
if (widget.isAnimated) {
fromBlobData = blobData;
}
blobData = widget._randomBlobData();
setState(() {});
return blobData!;
}
@override
void dispose() {
if (timer != null) timer!.cancel();
if (widget.controller != null) widget.controller!.dispose();
super.dispose();
}
}