@@ -20,32 +20,33 @@ class MainScreen extends StatefulHookWidget {
20
20
21
21
class _MainScreenState extends State <MainScreen > {
22
22
final TypeTestBox _typeTestBox = TypeTestBox ();
23
- FocusNode _typeTestFocusNode;
24
- ResultsSideBar _resultsSideBar;
23
+ FocusNode ? _typeTestFocusNode;
24
+ ResultsSideBar ? _resultsSideBar;
25
25
String _typeTestText = '' ;
26
- List <List > _charactersData = [];
26
+ List <List < String > > _charactersData = [];
27
27
Map <String , int > _mistakes = {};
28
28
int _cursorIndex = 0 ;
29
29
int _accuracy = 0 ;
30
- DateTime _startTime;
30
+ DateTime ? _startTime;
31
31
bool _isRestarting = false ;
32
32
33
33
//String _debugText = '';
34
34
35
35
/// Gets the 5 most frequent mistakes from [_mistakes] .
36
36
List <String > _getCommonMistakes () {
37
37
List _sortedMistakes = _mistakes.keys.toList (growable: false )
38
- ..sort ((k1, k2) => _mistakes[k2].compareTo (_mistakes[k1]));
38
+ ..sort ((k1, k2) => _mistakes[k2]! .compareTo (_mistakes[k1]! ));
39
39
return _sortedMistakes.sublist (
40
- 0 , _sortedMistakes.length < 5 ? _sortedMistakes.length : 5 );
40
+ 0 , _sortedMistakes.length < 5 ? _sortedMistakes.length : 5 )
41
+ as List <String >;
41
42
}
42
43
43
44
/// Calculates the WPM, accuracy and common mistakes and updates the [_resultsSideBar] .
44
45
void _updateResultsSideBar () {
45
46
final double wpm = (_cursorIndex / 5 ) /
46
- (DateTime .now ().difference (_startTime).inMilliseconds / 60000 );
47
+ (DateTime .now ().difference (_startTime! ).inMilliseconds / 60000 );
47
48
final double accuracy = _accuracy / _cursorIndex;
48
- _resultsSideBar.update (
49
+ _resultsSideBar! .update (
49
50
wpm: wpm,
50
51
accuracy: accuracy,
51
52
commonMistakes: _getCommonMistakes (),
@@ -67,12 +68,13 @@ class _MainScreenState extends State<MainScreen> {
67
68
// Create a new list of characters from newText.
68
69
newText.replaceAll ('\n ' , Consts .returnSymbol).split ('' ).forEach (
69
70
// first element is the actual character string, second element is the state of the character.
70
- (character) => _charactersData.add ([character, Consts .correctState]),
71
+ (String character) =>
72
+ _charactersData.add ([character, Consts .correctState]),
71
73
);
72
74
73
75
// Resets the ResultsSideBar.
74
- _resultsSideBar. update (
75
- wpm: 0 , accuracy: 0 , commonMistakes: _getCommonMistakes ());
76
+ _resultsSideBar!
77
+ . update ( wpm: 0 , accuracy: 0 , commonMistakes: _getCommonMistakes ());
76
78
77
79
// Rebuilds the TypeTestBoxCharacters in the TypeTestBox.
78
80
_typeTestBox.generateCharacters (_charactersData);
@@ -82,7 +84,7 @@ class _MainScreenState extends State<MainScreen> {
82
84
//_typeTestFocusNode.requestFocus(); // <-- this doesn't work on release app for some reason??!!
83
85
84
86
// Once all the widgets are built we can allow key presses again.
85
- WidgetsBinding .instance.addPostFrameCallback (
87
+ WidgetsBinding .instance! .addPostFrameCallback (
86
88
(_) => _isRestarting = false ,
87
89
);
88
90
}
@@ -109,7 +111,7 @@ class _MainScreenState extends State<MainScreen> {
109
111
}
110
112
111
113
/// Validates the key press and updates the test data and widgets accordingly.
112
- void _validateInput (String input) {
114
+ void _validateInput (String ? input) {
113
115
// True if backspace is pressed and the cursor isn't at the start of the test.
114
116
if (input == "Backspace" ) {
115
117
if (_cursorIndex > 0 ) {
@@ -143,7 +145,9 @@ class _MainScreenState extends State<MainScreen> {
143
145
_charactersData[_cursorIndex][1 ] = Consts .incorrectState;
144
146
// Increases the number of incorrect types for the current character in _mistakes.
145
147
if (_mistakes.containsKey (_charactersData[_cursorIndex][0 ]))
146
- _mistakes[_charactersData[_cursorIndex][0 ]]++ ;
148
+ _mistakes[_charactersData[_cursorIndex][0 ]] =
149
+ _mistakes[_charactersData[_cursorIndex][0 ]]! +
150
+ 1 ; // <-- Any other way of doing this with null-safty??
147
151
else
148
152
_mistakes[_charactersData[_cursorIndex][0 ]] = 1 ;
149
153
}
@@ -164,7 +168,7 @@ class _MainScreenState extends State<MainScreen> {
164
168
index: _cursorIndex, state: Consts .neutralState, isAtCursor: true );
165
169
// Prevents listening to key inputs if the cursor is at the end of the test.
166
170
else {
167
- _typeTestFocusNode.unfocus ();
171
+ _typeTestFocusNode! .unfocus ();
168
172
FocusScope .of (context).requestFocus (FocusNode ());
169
173
}
170
174
// Updates the ResultSideBar with the new results.
@@ -180,7 +184,7 @@ class _MainScreenState extends State<MainScreen> {
180
184
_resultsSideBar = ResultsSideBar (onRestart: () => _restart (_typeTestText));
181
185
182
186
// Starts a test with the initial type text once everything has been built.
183
- WidgetsBinding .instance.addPostFrameCallback (
187
+ WidgetsBinding .instance! .addPostFrameCallback (
184
188
(_) => _restart (Config .initialTypeTestText),
185
189
);
186
190
}
@@ -194,7 +198,7 @@ class _MainScreenState extends State<MainScreen> {
194
198
final FocusNode textInputFocusNode = useFocusNode ();
195
199
196
200
// Gets the controller for the TextInputBox.
197
- final TextEditingController textInputController =
201
+ final TextEditingController ? textInputController =
198
202
useTextEditingController ();
199
203
200
204
return Scaffold (
@@ -210,7 +214,7 @@ class _MainScreenState extends State<MainScreen> {
210
214
),*/
211
215
Expanded (
212
216
child: RawKeyboardListener (
213
- focusNode: _typeTestFocusNode,
217
+ focusNode: _typeTestFocusNode! ,
214
218
onKey: _onKey,
215
219
child: _typeTestBox,
216
220
),
@@ -223,7 +227,7 @@ class _MainScreenState extends State<MainScreen> {
223
227
],
224
228
),
225
229
),
226
- _resultsSideBar,
230
+ _resultsSideBar! ,
227
231
],
228
232
),
229
233
);
0 commit comments