-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
part of katana_ui; | ||
|
||
/// This widget repeatedly redraws at regular intervals. | ||
/// | ||
/// Draw [builder] at every interval of [duration]. | ||
/// | ||
/// 一定時間ごとに繰り返して再描画するウィジェットです。 | ||
/// | ||
/// [duration]の間隔ごとに[builder]を描画します。 | ||
class PeriodicScope extends StatefulWidget { | ||
/// This widget repeatedly redraws at regular intervals. | ||
/// | ||
/// Draw [builder] at every interval of [duration]. | ||
/// | ||
/// 一定時間ごとに繰り返して再描画するウィジェットです。 | ||
/// | ||
/// [duration]の間隔ごとに[builder]を描画します。 | ||
const PeriodicScope({ | ||
super.key, | ||
required this.duration, | ||
required this.builder, | ||
}); | ||
|
||
/// Interval between drawings. | ||
/// | ||
/// 描画を行う間隔。 | ||
final Duration duration; | ||
|
||
/// Builder for drawing. | ||
/// | ||
/// 描画を行うためのビルダー。 | ||
final Widget Function(BuildContext context, DateTime now) builder; | ||
|
||
@override | ||
State<StatefulWidget> createState() => _PeriodicScopeState(); | ||
} | ||
|
||
class _PeriodicScopeState extends State<PeriodicScope> { | ||
Timer? _timer; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_timer = Timer.periodic(widget.duration, (timer) { | ||
setState(() {}); | ||
}); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_timer?.cancel(); | ||
_timer = null; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final now = DateTime.now(); | ||
return widget.builder.call(context, now); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters