/
word_pair_timer.dart
122 lines (100 loc) · 2.98 KB
/
word_pair_timer.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
//
import 'dart:async';
import '/src/view.dart';
import 'package:english_words/english_words.dart';
///
class WordPairs extends StatelessWidget {
/// Only one instance of the class is necessary and desired.
WordPairs({
super.key,
int? seconds,
required InheritedWidgetStateMixin state,
}) : _timer = _WordPairsTimer(
seconds: seconds,
// ignore: INVALID_USE_OF_PROTECTED_MEMBER
callback: () => state.setState(() {}));
final _WordPairsTimer _timer;
/// Turn on the timer when necessary.
void activate() => _timer.activate();
/// Turn off the timer if the Stat object is closed.
void deactivate() => _timer.deactivate();
@override
Widget build(BuildContext context) {
// Find the 'latest' InheritedWidgetStateMixin object in the Widget tree
final _state = context.findAncestorStateOfType<InheritedWidgetStateMixin>();
// This widget is rebuilt if the InheritedWidget is called again and again
_state?.dependOnInheritedWidget(context);
return Text(
_timer.wordPair,
style: TextStyle(
color: Colors.red,
fontSize: Theme.of(context).textTheme.headlineMedium!.fontSize,
),
);
}
}
class _WordPairsTimer {
_WordPairsTimer({
this.seconds,
this.callback,
}) {
// Initialize the Timer
_initTimer();
// Uncomment if you don't want the Timer working.
// timer?.cancel();
}
/// Number of seconds between intervals set by the timer
final int? seconds;
/// Called at every interval set by the timer
final void Function()? callback;
/// The built-in timer
Timer? timer;
// Contains the word-pairs
final suggestions = <WordPair>[];
// Retrieve word pair from List
int index = 0;
/// Supply the word pair
String get wordPair => _wordPair;
String _wordPair = '';
/// Supply a word pair
void _getWordPair() {
//
try {
//
final WordPair twoWords = _generateWordPair();
/// Alternate approach uses inheritWidget() and setStatesInherited() functions.
_wordPair = twoWords.asString;
} catch (ex) {
/// Stop the timer.
/// Something is not working. Don't have the timer repeat it over and over.
timer?.cancel();
// Rethrow the error so to get processed by the App's error handler.
rethrow;
}
}
/// Supply a set of word pairs
/// Generate a certain number of word pairs
/// and when those are used, generate another set.
WordPair _generateWordPair() {
index++;
if (index >= suggestions.length) {
index = 0;
suggestions.clear();
suggestions.addAll(generateWordPairs().take(100));
}
return suggestions[index];
}
// initialize the Timer
void _initTimer() =>
timer = Timer.periodic(Duration(seconds: seconds ?? 5), (timer) {
callback?.call();
_getWordPair();
});
/// May need to initialize again
void activate() => _initTimer();
/// Cancel the timer.
void deactivate() {
timer?.cancel();
timer = null;
}
}