Skip to content

Commit 2775952

Browse files
committed
feat: v2.0.1 support null safety
support null safety also known as void safety
1 parent 2d288eb commit 2775952

40 files changed

+529
-533
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ example/.history
2323
example/.svn/
2424
example/pubspec.lock
2525
example/.vscode/
26+
.vscode/

example/lib/main.dart

+27-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MyApp extends StatelessWidget {
1818
}
1919

2020
class MyHomePage extends StatefulWidget {
21-
MyHomePage({Key key, this.title}) : super(key: key);
21+
MyHomePage({Key? key, required this.title}) : super(key: key);
2222

2323
final String title;
2424

@@ -34,17 +34,22 @@ class _MyHomePageState extends State<MyHomePage> {
3434
title: Text(widget.title),
3535
),
3636
body: Center(
37-
child: Zerker(app: MyZKApp(), clip: true, interactive: true, width: 350, height: 350),
37+
child: Zerker(
38+
app: MyZKApp(),
39+
clip: true,
40+
interactive: true,
41+
width: 350,
42+
height: 350),
3843
));
3944
}
4045
}
4146

4247
class MyZKApp extends ZKApp {
4348
bool _loaded = false;
4449
bool _jumping = false;
45-
ZKSprite boy;
46-
ZKText title;
47-
ZKScrollBg bg;
50+
ZKSprite? boy;
51+
ZKText? title;
52+
ZKScrollBg? bg;
4853

4954
@override
5055
init() {
@@ -75,8 +80,11 @@ class MyZKApp extends ZKApp {
7580
..position.x = appWidth / 2
7681
..position.y = 20
7782
..text = "Please click anywhere"
78-
..setStyle(color: Colors.blueGrey, backgroundColor: Colors.greenAccent, textAlign: TextAlign.center);
79-
stage.addChild(title);
83+
..setStyle(
84+
color: Colors.blueGrey,
85+
backgroundColor: Colors.greenAccent,
86+
textAlign: TextAlign.center);
87+
stage.addChild(title!);
8088

8189
// add boy
8290
boy = ZKSprite(key: "boy")
@@ -88,37 +96,40 @@ class MyZKApp extends ZKApp {
8896
..animator.make("jump", ["Jump ({1-15}).png"])
8997
..animator.make("dead", ["Dead ({1-15}).png"])
9098
..animator.play("run", 25, true);
91-
stage.addChild(boy);
99+
stage.addChild(boy!);
92100

93101
// add bg
94102
bg = ZKScrollBg(key: "bg", time: 4 * 1000)
95103
..setScale(0.5)
96104
..position.y = appHeight
97105
..anchor.y = 1;
98-
stage.addChildAt(bg, 0);
106+
stage.addChildAt(bg!, 0);
99107

100108
_addAction();
101109
}
102110

103111
_addAction() {
104-
boy.onTapDown = (event) {
105-
bg.stop();
112+
boy?.onTapDown = (event) {
113+
bg?.stop();
106114
_jumping = false;
107-
boy.animator.play("dead", 20);
115+
boy?.animator.play("dead", 20);
108116
};
109117

110118
stage.onTapDown = (event) {
111119
if (event.target == boy) return;
112120
if (_jumping) return;
113121

114-
bg.play();
122+
bg?.play();
115123
_jumping = true;
116-
boy.animator.play("jump", 20);
124+
boy?.animator.play("jump", 20);
117125
ZKTween(boy)
118126
.to({"y": appHeight - 120}, 500)
119127
.easing(Ease.circ.easeOut)
120-
.chain(ZKTween(boy).to({"y": appHeight - 16}, 500).easing(Ease.circ.easeIn).onComplete((obj) {
121-
boy.animator.play("run", 25, true);
128+
.chain(ZKTween(boy)
129+
.to({"y": appHeight - 16}, 500)
130+
.easing(Ease.circ.easeIn)
131+
.onComplete((obj) {
132+
boy?.animator.play("run", 25, true);
122133
_jumping = false;
123134
}))
124135
.start();

example/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: A new Flutter project.
1414
version: 1.0.0+1
1515

1616
environment:
17-
sdk: ">=2.1.0 <3.0.0"
17+
sdk: ">=2.12.0 <3.0.0"
1818

1919
dependencies:
2020
flutter:
@@ -23,7 +23,8 @@ dependencies:
2323
# The following adds the Cupertino Icons font to your application.
2424
# Use with the CupertinoIcons class for iOS style icons.
2525
cupertino_icons: ^0.1.2
26-
zerker: ^1.4.1
26+
zerker:
27+
path: ../
2728

2829
dev_dependencies:
2930
flutter_test:

example/test/widget_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import 'package:flutter/material.dart';
99
import 'package:flutter_test/flutter_test.dart';
1010

11-
import '../main.dart';
11+
import '../lib/main.dart';
1212

1313
void main() {
1414
testWidgets('Counter increments smoke test', (WidgetTester tester) async {

lib/src/animate/animation.dart

+10-15
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import '../utils/util.dart';
44
List defaultList = Util.convertConsecutiveList(['0-42']);
55

66
class Animation {
7-
Function onComplete;
7+
Function? onComplete;
88

9-
String name;
10-
String framesType;
9+
String name = '';
10+
String framesType = '';
1111
bool loop = false;
1212

1313
int index = 0;
1414
int elapsed = 0;
1515
double delay = 0;
1616

17-
List _frames;
17+
List _frames = [];
1818

1919
////////////////////////////////////////////////////////////
2020
///
@@ -48,9 +48,9 @@ class Animation {
4848
int _rate = Constant.RATE;
4949

5050
Animation(String name,
51-
[List frames, int rate = Constant.RATE, bool loop = false]) {
51+
[List? frames, int rate = Constant.RATE, bool loop = false]) {
5252
this.name = name;
53-
this.rate = rate ?? Constant.RATE;
53+
this.rate = rate;
5454
this.loop = loop;
5555

5656
if (frames == null) {
@@ -63,12 +63,12 @@ class Animation {
6363
}
6464

6565
dynamic getCurrentFrameKey() {
66-
if (this.frames == null) return 0;
66+
if (this.frames.length == 0) return 0;
6767
return this.frames[this.index];
6868
}
6969

7070
void update(int seconds) {
71-
if (this.frames == null) return;
71+
if (this.frames.length == 0) return;
7272

7373
this.elapsed += seconds;
7474
if (this.elapsed >= this.delay) {
@@ -84,7 +84,7 @@ class Animation {
8484
}
8585

8686
if (this.loop != true && onComplete != null) {
87-
onComplete();
87+
onComplete!();
8888
}
8989
}
9090
}
@@ -95,14 +95,9 @@ class Animation {
9595
}
9696

9797
void dispose() {
98-
this.name = null;
9998
this.onComplete = null;
10099
this.elapsed = 0;
101100
this.index = 0;
102-
103-
if (this.frames != null) {
104-
this.frames.clear();
105-
this.frames = null;
106-
}
101+
this.frames.clear();
107102
}
108103
}

lib/src/animate/animator.dart

+21-18
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import '../core/constant.dart';
1111
///
1212
////////////////////////////////////////////////////////////
1313
class Animator {
14-
Function onComplete;
14+
Function? onComplete;
1515
String status = "stop";
1616

1717
ListMap _aniList = new ListMap();
18-
Animation _currentAni;
18+
Animation? _currentAni;
1919
int _framesLength = -1;
2020

2121
bool get isEnabled {
@@ -32,27 +32,28 @@ class Animator {
3232
}
3333

3434
dynamic getCurrentFrameKey() {
35-
return _currentAni != null ? _currentAni.getCurrentFrameKey() : 0;
35+
return _currentAni != null ? _currentAni?.getCurrentFrameKey() : 0;
3636
}
3737

3838
////////////////////////////////////////////////////////////
3939
///
4040
/// Play and Stop
4141
///
4242
////////////////////////////////////////////////////////////
43-
void play(String name, [int rate, bool loop = false]) {
44-
Animation ani = _aniList.getItem(name);
45-
if (ani == null)
43+
void play(String name, [int? rate, bool loop = false]) {
44+
Animation? ani = _aniList.getItem(name);
45+
if (ani == null) {
4646
throw ("Zerker:: Sorry, there is no corresponding animation information.");
47-
48-
ani.rate = rate ?? ani.rate;
49-
ani.loop = loop ?? ani.loop;
50-
_currentAni = ani;
51-
_currentAni.reset();
52-
this.status = "play";
47+
} else {
48+
ani.rate = rate ?? ani.rate;
49+
ani.loop = loop;
50+
_currentAni = ani;
51+
_currentAni?.reset();
52+
this.status = "play";
53+
}
5354
}
5455

55-
void stop([String name]) {
56+
void stop([String? name]) {
5657
if (name != null) {
5758
this.play(name, 9999);
5859
}
@@ -66,7 +67,7 @@ class Animator {
6667
///
6768
////////////////////////////////////////////////////////////
6869
void make(String name,
69-
[List frames, int rate = Constant.RATE, bool loop = false]) {
70+
[List? frames, int rate = Constant.RATE, bool loop = false]) {
7071
Animation ani = new Animation(name, frames, rate, loop);
7172
ani.framesLength = framesLength;
7273
ani.onComplete = onComplete;
@@ -75,12 +76,15 @@ class Animator {
7576
}
7677

7778
void add(
78-
{String name, List frames, int rate = Constant.RATE, bool loop = false}) {
79+
{String name = "",
80+
List? frames,
81+
int rate = Constant.RATE,
82+
bool loop = false}) {
7983
return make(name, frames, rate, loop);
8084
}
8185

8286
void remove(String name, [bool destroy = false]) {
83-
Animation ani = _aniList.getItem(name);
87+
Animation? ani = _aniList.getItem(name);
8488
if (ani != null && destroy) {
8589
ani.dispose();
8690
}
@@ -96,7 +100,7 @@ class Animator {
96100
void update(int time) {
97101
if (this.status == "stop") return;
98102

99-
_currentAni.update(time);
103+
_currentAni?.update(time);
100104
}
101105

102106
////////////////////////////////////////////////////////////
@@ -110,7 +114,6 @@ class Animator {
110114

111115
dispose() {
112116
this.clear();
113-
114117
onComplete = null;
115118
}
116119
}

lib/src/animate/frame.dart

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
import "dart:ui";
2-
import "dart:math";
32
import '../math/point.dart';
43
import '../math/mathutil.dart';
54
import '../core/constant.dart';
65

76
class Frame {
8-
Image image;
9-
String type;
10-
String name;
7+
Image? image;
8+
String type = "";
9+
String name = "";
1110

12-
Rect srcRect;
13-
Rect dstRect;
14-
bool trimmed;
15-
bool hasClip;
16-
Point anchor;
11+
Rect? srcRect;
12+
Rect? dstRect;
13+
bool trimmed = false;
14+
bool hasClip = false;
15+
Point? anchor;
1716

1817
bool rotated = false;
1918
bool _setted = false;
2019

2120
dynamic info;
22-
Rect get rect => srcRect;
21+
Rect? get rect => srcRect;
2322

24-
Frame([String type, Image img]) {
23+
Frame([String type = "", Image? img]) {
2524
this.type = type;
2625
this.image = img;
2726
}
2827

2928
double get width {
30-
return this.dstRect != null ? this.dstRect.width : 0;
29+
return this.dstRect != null ? this.dstRect!.width : 0;
3130
}
3231

3332
double get height {
34-
return this.dstRect != null ? this.dstRect.height : 0;
33+
return this.dstRect != null ? this.dstRect!.height : 0;
3534
}
3635

3736
bool get isEnabled {
@@ -73,10 +72,10 @@ class Frame {
7372

7473
void rotateDstRectOnce(x, y) {
7574
if (this._setted != true) {
76-
var l = this.dstRect.left;
77-
var t = this.dstRect.top;
78-
var w = this.dstRect.width;
79-
var h = this.dstRect.height;
75+
var l = this.dstRect!.left;
76+
var t = this.dstRect!.top;
77+
var w = this.dstRect!.width;
78+
var h = this.dstRect!.height;
8079

8180
List pos = this.coordinateRotate(x, y);
8281
var x1 = pos[0];
@@ -88,10 +87,10 @@ class Frame {
8887
}
8988

9089
List coordinateRotate(x, y) {
91-
var cx = x;
92-
var cy = y;
93-
var x0 = y;
94-
var y0 = (1 - x);
90+
double cx = x;
91+
double cy = y;
92+
double x0 = y;
93+
double y0 = (1.0 - x);
9594

9695
var point = MathUtil.coordinateRotate(-pi / 2, x0, y0, cx, cy);
9796
var x1 = MathUtil.round(point[0], 4);

0 commit comments

Comments
 (0)