@@ -56,15 +56,14 @@ class SnakeBoardPainter extends CustomPainter {
5656}
5757
5858class SnakeState extends State <Snake > {
59- SnakeState (int rows, int columns, this .cellSize) {
60- state = GameState (rows, columns);
61- }
59+ SnakeState (int rows, int columns, this .cellSize)
60+ : state = GameState (rows, columns);
6261
6362 double cellSize;
6463 GameState state;
65- AccelerometerEvent acceleration;
66- StreamSubscription <AccelerometerEvent > _streamSubscription;
67- Timer _timer;
64+ AccelerometerEvent ? acceleration;
65+ late StreamSubscription <AccelerometerEvent > _streamSubscription;
66+ late Timer _timer;
6867
6968 @override
7069 Widget build (BuildContext context) {
@@ -96,21 +95,21 @@ class SnakeState extends State<Snake> {
9695 }
9796
9897 void _step () {
99- final math.Point <int > newDirection = acceleration == null
98+ final AccelerometerEvent ? currentAcceleration = acceleration;
99+ final math.Point <int >? newDirection = currentAcceleration == null
100100 ? null
101- : acceleration .x.abs () < 1.0 && acceleration .y.abs () < 1.0
101+ : currentAcceleration .x.abs () < 1.0 && currentAcceleration .y.abs () < 1.0
102102 ? null
103- : (acceleration .x.abs () < acceleration .y.abs ())
104- ? math.Point <int >(0 , acceleration .y.sign.toInt ())
105- : math.Point <int >(- acceleration .x.sign.toInt (), 0 );
103+ : (currentAcceleration .x.abs () < currentAcceleration .y.abs ())
104+ ? math.Point <int >(0 , currentAcceleration .y.sign.toInt ())
105+ : math.Point <int >(- currentAcceleration .x.sign.toInt (), 0 );
106106 state.step (newDirection);
107107 }
108108}
109109
110110class GameState {
111- GameState (this .rows, this .columns) {
112- snakeLength = math.min (rows, columns) - 5 ;
113- }
111+ GameState (this .rows, this .columns)
112+ : snakeLength = math.min (rows, columns) - 5 ;
114113
115114 int rows;
116115 int columns;
@@ -119,7 +118,7 @@ class GameState {
119118 List <math.Point <int >> body = < math.Point <int >> [const math.Point <int >(0 , 0 )];
120119 math.Point <int > direction = const math.Point <int >(1 , 0 );
121120
122- void step (math.Point <int > newDirection) {
121+ void step (math.Point <int >? newDirection) {
123122 math.Point <int > next = body.last + direction;
124123 next = math.Point <int >(next.x % columns, next.y % rows);
125124
0 commit comments