Skip to content

Commit

Permalink
feat: Added PeriodicScope.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jul 12, 2023
1 parent ae88160 commit 2b2dce2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/katana_ui/lib/katana_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ part 'ui/indent.dart';
part 'ui/card_tile.dart';
part 'ui/label.dart';
part 'ui/line_tile.dart';
part 'ui/periodic_scope.dart';
61 changes: 61 additions & 0 deletions packages/katana_ui/lib/ui/periodic_scope.dart
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);
}
}
2 changes: 1 addition & 1 deletion packages/katana_ui/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ packages:
path: "../katana"
relative: true
source: path
version: "2.3.2"
version: "2.3.3"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit 2b2dce2

Please sign in to comment.