Skip to content

Added option of a controller to control the time state #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/idle_detector_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
library;

export 'src/idle_detector_wrapper_base.dart';
export 'src/idle_detector_controller.dart';

// TODO: Export any libraries intended for clients of this package.
17 changes: 17 additions & 0 deletions lib/src/idle_detector_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class IdleDetectorController {
late Function _resetTimer;
late Function _stopTimer;

void setTimerFunctions(Function resetTimer, Function stopTimer) {
_resetTimer = resetTimer;
_stopTimer = stopTimer;
}

void resetTimer() {
_resetTimer();
}

void stopTimer() {
_stopTimer();
}
}
16 changes: 15 additions & 1 deletion lib/src/idle_detector_wrapper_base.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:idle_detector_wrapper/src/idle_detector_controller.dart';

class IdleDetector extends StatefulWidget {
final Duration idleTime;
final Widget child;
final Function? onIdle;
final IdleDetectorController? controller;
final bool autoStart;

IdleDetector({
required this.idleTime,
required this.child,
this.onIdle,
this.controller,
this.autoStart = true,
});

@override
Expand All @@ -22,7 +27,12 @@ class _IdleDetectorState extends State<IdleDetector> {
@override
void initState() {
super.initState();
_resetTimer();
if (widget.autoStart) {
_resetTimer();
}
if (widget.controller != null) {
widget.controller!.setTimerFunctions(_resetTimer, _stopTimer);
}
}

@override
Expand All @@ -31,6 +41,10 @@ class _IdleDetectorState extends State<IdleDetector> {
super.dispose();
}

void _stopTimer() {
_timer?.cancel();
}

void _resetTimer() {
_timer?.cancel();
_timer = Timer(widget.idleTime, () {
Expand Down