platform/packages/jael/jael_web/example/stateful.dart

33 lines
651 B
Dart
Raw Normal View History

2019-03-23 18:49:37 +00:00
import 'dart:async';
import 'package:jael_web/jael_web.dart';
part 'stateful.g.dart';
void main() {}
class _AppState {
2021-06-20 12:37:20 +00:00
final int? ticks;
2019-03-23 18:49:37 +00:00
_AppState({this.ticks});
2021-06-20 12:37:20 +00:00
_AppState copyWith({int? ticks}) {
2019-03-23 18:49:37 +00:00
return _AppState(ticks: ticks ?? this.ticks);
}
}
2019-03-23 22:02:57 +00:00
@Jael(template: '<div>Tick count: {{state.ticks}}</div>')
2019-03-23 18:49:37 +00:00
class StatefulApp extends Component<_AppState> with _StatefulAppJaelTemplate {
2021-06-20 12:37:20 +00:00
Timer? _timer;
2019-03-23 18:49:37 +00:00
StatefulApp() {
2019-07-29 22:12:52 +00:00
state = _AppState(ticks: 0);
2019-03-23 18:49:37 +00:00
_timer = Timer.periodic(Duration(seconds: 1), (t) {
setState(state.copyWith(ticks: t.tick));
});
}
@override
void beforeDestroy() {
2021-06-20 12:37:20 +00:00
_timer!.cancel();
2019-03-23 18:49:37 +00:00
}
}