diff --git a/lib/idle_detector_wrapper.dart b/lib/idle_detector_wrapper.dart index ce1c08b..075440f 100644 --- a/lib/idle_detector_wrapper.dart +++ b/lib/idle_detector_wrapper.dart @@ -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. diff --git a/lib/src/idle_detector_controller.dart b/lib/src/idle_detector_controller.dart new file mode 100644 index 0000000..a7892bb --- /dev/null +++ b/lib/src/idle_detector_controller.dart @@ -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(); + } +} diff --git a/lib/src/idle_detector_wrapper_base.dart b/lib/src/idle_detector_wrapper_base.dart index 5f8aabb..47c7f3a 100644 --- a/lib/src/idle_detector_wrapper_base.dart +++ b/lib/src/idle_detector_wrapper_base.dart @@ -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 @@ -22,7 +27,12 @@ class _IdleDetectorState extends State { @override void initState() { super.initState(); - _resetTimer(); + if (widget.autoStart) { + _resetTimer(); + } + if (widget.controller != null) { + widget.controller!.setTimerFunctions(_resetTimer, _stopTimer); + } } @override @@ -31,6 +41,10 @@ class _IdleDetectorState extends State { super.dispose(); } + void _stopTimer() { + _timer?.cancel(); + } + void _resetTimer() { _timer?.cancel(); _timer = Timer(widget.idleTime, () {