Skip to content

Commit 42b6669

Browse files
author
Yan Wong
committed
Migrate to null safety
1 parent fb35007 commit 42b6669

8 files changed

+329
-86
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.2.0
4+
5+
Migrate to null safety.
6+
37
## 0.1.0
48

59
Add backgroundImage param.

example/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ build/
6969
!**/ios/**/default.pbxuser
7070
!**/ios/**/default.perspectivev3
7171
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
72+
73+
# Web related
74+
web

example/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: wave_example
22
description: An example application for Wave.
33
author: Yan Wong <flutter_wave@wong.so>
4-
homepage: https://github.com/TheProtoss/wave/example
4+
homepage: https://github.com/i-protoss/wave/tree/master/example
5+
publish_to: none
56

67
# The following defines the version and build number for your application.
78
# A version number is three numbers separated by dots, like 1.2.43
@@ -22,7 +23,7 @@ dependencies:
2223

2324
# The following adds the Cupertino Icons font to your application.
2425
# Use with the CupertinoIcons class for iOS style icons.
25-
cupertino_icons: ^1.0.1+1
26+
cupertino_icons: ^1.0.2
2627

2728
dev_dependencies:
2829
flutter_test:

lib/config.dart

+13-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum ColorMode {
1414
}
1515

1616
abstract class Config {
17-
final ColorMode colorMode;
17+
final ColorMode? colorMode;
1818

1919
Config({this.colorMode});
2020

@@ -25,21 +25,21 @@ abstract class Config {
2525
}
2626

2727
class CustomConfig extends Config {
28-
final List<Color> colors;
29-
final List<List<Color>> gradients;
30-
final Alignment gradientBegin;
31-
final Alignment gradientEnd;
32-
final List<int> durations;
33-
final List<double> heightPercentages;
34-
final MaskFilter blur;
28+
final List<Color>? colors;
29+
final List<List<Color>>? gradients;
30+
final Alignment? gradientBegin;
31+
final Alignment? gradientEnd;
32+
final List<int>? durations;
33+
final List<double>? heightPercentages;
34+
final MaskFilter? blur;
3535

3636
CustomConfig({
3737
this.colors,
3838
this.gradients,
3939
this.gradientBegin,
4040
this.gradientEnd,
41-
@required this.durations,
42-
@required this.heightPercentages,
41+
required this.durations,
42+
required this.heightPercentages,
4343
this.blur,
4444
}) : assert(() {
4545
if (colors == null && gradients == null) {
@@ -68,7 +68,9 @@ class CustomConfig extends Config {
6868
return true;
6969
}()),
7070
assert(() {
71-
if (colors != null) {
71+
if (colors != null &&
72+
durations != null &&
73+
heightPercentages != null) {
7274
if (colors.length != durations.length ||
7375
colors.length != heightPercentages.length) {
7476
throw FlutterError(

lib/wave.dart

+51-50
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ class WaveWidget extends StatefulWidget {
214214
final double wavePhase;
215215
final double waveFrequency;
216216
final double heightPercentange;
217-
final int duration;
218-
final Color backgroundColor;
219-
final DecorationImage backgroundImage;
217+
final int? duration;
218+
final Color? backgroundColor;
219+
final DecorationImage? backgroundImage;
220220
final bool isLoop;
221221

222222
WaveWidget({
223-
@required this.config,
224-
@required this.size,
223+
required this.config,
224+
required this.size,
225225
this.waveAmplitude = 20.0,
226226
this.wavePhase = 10.0,
227227
this.waveFrequency = 1.6,
@@ -237,17 +237,17 @@ class WaveWidget extends StatefulWidget {
237237
}
238238

239239
class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
240-
List<AnimationController> _waveControllers;
241-
List<Animation<double>> _wavePhaseValues;
240+
late List<AnimationController> _waveControllers;
241+
late List<Animation<double>> _wavePhaseValues;
242242

243243
List<double> _waveAmplitudes = [];
244-
Map<Animation<double>, AnimationController> valueList;
245-
Timer _endAnimationTimer;
244+
Map<Animation<double>, AnimationController>? valueList;
245+
Timer? _endAnimationTimer;
246246

247247
_initAnimations() {
248248
if (widget.config.colorMode == ColorMode.custom) {
249249
_waveControllers =
250-
(widget.config as CustomConfig).durations.map((duration) {
250+
(widget.config as CustomConfig).durations!.map((duration) {
251251
_waveAmplitudes.add(widget.waveAmplitude + 10);
252252
return AnimationController(
253253
vsync: this, duration: Duration(milliseconds: duration));
@@ -280,7 +280,8 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
280280

281281
// If isLoop is false, stop the animation after the specified duration.
282282
if (!widget.isLoop) {
283-
_endAnimationTimer = Timer(Duration(milliseconds: widget.duration), () {
283+
_endAnimationTimer =
284+
Timer(Duration(milliseconds: widget.duration!), () {
284285
for (AnimationController waveController in _waveControllers) {
285286
waveController.stop();
286287
}
@@ -292,10 +293,10 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
292293
_buildPaints() {
293294
List<Widget> paints = [];
294295
if (widget.config.colorMode == ColorMode.custom) {
295-
List<Color> _colors = (widget.config as CustomConfig).colors;
296-
List<List<Color>> _gradients = (widget.config as CustomConfig).gradients;
297-
Alignment begin = (widget.config as CustomConfig).gradientBegin;
298-
Alignment end = (widget.config as CustomConfig).gradientEnd;
296+
List<Color>? _colors = (widget.config as CustomConfig).colors;
297+
List<List<Color>>? _gradients = (widget.config as CustomConfig).gradients;
298+
Alignment? begin = (widget.config as CustomConfig).gradientBegin;
299+
Alignment? end = (widget.config as CustomConfig).gradientEnd;
299300
for (int i = 0; i < _wavePhaseValues.length; i++) {
300301
paints.add(
301302
Container(
@@ -306,7 +307,7 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
306307
gradientBegin: begin,
307308
gradientEnd: end,
308309
heightPercentange:
309-
(widget.config as CustomConfig).heightPercentages[i],
310+
(widget.config as CustomConfig).heightPercentages![i],
310311
repaint: _waveControllers[i],
311312
waveFrequency: widget.waveFrequency,
312313
wavePhaseValue: _wavePhaseValues[i],
@@ -357,12 +358,12 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
357358

358359
/// Meta data of layer
359360
class Layer {
360-
final Color color;
361-
final List<Color> gradient;
362-
final MaskFilter blur;
363-
final Path path;
364-
final double amplitude;
365-
final double phase;
361+
final Color? color;
362+
final List<Color>? gradient;
363+
final MaskFilter? blur;
364+
final Path? path;
365+
final double? amplitude;
366+
final double? phase;
366367

367368
Layer({
368369
this.color,
@@ -375,20 +376,20 @@ class Layer {
375376
}
376377

377378
class _CustomWavePainter extends CustomPainter {
378-
final ColorMode colorMode;
379-
final Color color;
380-
final List<Color> gradient;
381-
final Alignment gradientBegin;
382-
final Alignment gradientEnd;
383-
final MaskFilter blur;
379+
final ColorMode? colorMode;
380+
final Color? color;
381+
final List<Color>? gradient;
382+
final Alignment? gradientBegin;
383+
final Alignment? gradientEnd;
384+
final MaskFilter? blur;
384385

385-
double waveAmplitude;
386+
double? waveAmplitude;
386387

387-
Animation<double> wavePhaseValue;
388+
Animation<double>? wavePhaseValue;
388389

389-
double waveFrequency;
390+
double? waveFrequency;
390391

391-
double heightPercentange;
392+
double? heightPercentange;
392393

393394
double _tempA = 0.0;
394395
double _tempB = 0.0;
@@ -406,7 +407,7 @@ class _CustomWavePainter extends CustomPainter {
406407
this.waveFrequency,
407408
this.wavePhaseValue,
408409
this.waveAmplitude,
409-
Listenable repaint})
410+
Listenable? repaint})
410411
: super(repaint: repaint);
411412

412413
_setPaths(double viewCenterY, Size size, Canvas canvas) {
@@ -415,50 +416,50 @@ class _CustomWavePainter extends CustomPainter {
415416
color: color,
416417
gradient: gradient,
417418
blur: blur,
418-
amplitude: (-1.6 + 0.8) * waveAmplitude,
419-
phase: wavePhaseValue.value * 2 + 30,
419+
amplitude: (-1.6 + 0.8) * waveAmplitude!,
420+
phase: wavePhaseValue!.value * 2 + 30,
420421
);
421422

422-
_layer.path.reset();
423-
_layer.path.moveTo(
423+
_layer.path!.reset();
424+
_layer.path!.moveTo(
424425
0.0,
425426
viewCenterY +
426-
_layer.amplitude * _getSinY(_layer.phase, waveFrequency, -1));
427+
_layer.amplitude! * _getSinY(_layer.phase!, waveFrequency!, -1));
427428
for (int i = 1; i < size.width + 1; i++) {
428-
_layer.path.lineTo(
429+
_layer.path!.lineTo(
429430
i.toDouble(),
430431
viewCenterY +
431-
_layer.amplitude * _getSinY(_layer.phase, waveFrequency, i));
432+
_layer.amplitude! * _getSinY(_layer.phase!, waveFrequency!, i));
432433
}
433434

434-
_layer.path.lineTo(size.width, size.height);
435-
_layer.path.lineTo(0.0, size.height);
436-
_layer.path.close();
435+
_layer.path!.lineTo(size.width, size.height);
436+
_layer.path!.lineTo(0.0, size.height);
437+
_layer.path!.close();
437438
if (_layer.color != null) {
438-
_paint.color = _layer.color;
439+
_paint.color = _layer.color!;
439440
}
440441
if (_layer.gradient != null) {
441442
var rect = Offset.zero &
442-
Size(size.width, size.height - viewCenterY * heightPercentange);
443+
Size(size.width, size.height - viewCenterY * heightPercentange!);
443444
_paint.shader = LinearGradient(
444445
begin: gradientBegin == null
445446
? Alignment.bottomCenter
446-
: gradientBegin,
447-
end: gradientEnd == null ? Alignment.topCenter : gradientEnd,
448-
colors: _layer.gradient)
447+
: gradientBegin!,
448+
end: gradientEnd == null ? Alignment.topCenter : gradientEnd!,
449+
colors: _layer.gradient!)
449450
.createShader(rect);
450451
}
451452
if (_layer.blur != null) {
452453
_paint.maskFilter = _layer.blur;
453454
}
454455

455456
_paint.style = PaintingStyle.fill;
456-
canvas.drawPath(_layer.path, _paint);
457+
canvas.drawPath(_layer.path!, _paint);
457458
}
458459

459460
@override
460461
void paint(Canvas canvas, Size size) {
461-
double viewCenterY = size.height * (heightPercentange + 0.1);
462+
double viewCenterY = size.height * (heightPercentange! + 0.1);
462463
viewWidth = size.width;
463464
_setPaths(viewCenterY, size, canvas);
464465
}

0 commit comments

Comments
 (0)