
[ad_1]
prevent data leakage
User inactivity detection is one of the most common features to avoid data leakage. They can be within private applications on your smartphone such as your bank account or applications running in physical stores on a kiosk device where you may need to enter some personal information such as your email address.
I’m assuming you know a bit about dependency injection and the BLOC pattern but keep in mind that none of this is mandatory for implementing your own solution. I’ll provide an alternative anyway. Enough talking, let’s dig into it!
add before injectable
, async
, flutter_bloc
And build_runner
, Those are some well known packages and used frequently.
Then we need an object to keep track of time, that is the main feature. To do this, let’s make a simple cubit
and are using RestartableTimer
, It does what it says and is provided by async
the packet. Exactly what we need.
As you can imagine, the idea is to run some sort of countdown during this 60 secondes
and once the time elapses, it emits a TimeoutExpired
state so that BlocListener
The layer in our presentation can capture this and act accordingly. For example, you may want to display a AlertDialog
To ensure that the user has gone or clears any personal information promptly.
On the other hand, whenever there is an interaction with the screen, there will be a countdown reseted
for its original price. This is the purpose of the code below and that’s why RestartableTimer
better than a regular Timer
,
then wrap your MaterialApp
within a simple Listener
, onPointerDown
Callbacks are the key to our implementation. It will turn on every time you interact with the screen.
don’t forget to call
runner
To generate everything you need.
Create a simple class with similar methods. The only difference is the callback function. This will give us the opportunity to handle the expected behavior when the timeout is triggered.
To prevent any multiple timeouts from running [if you need to access your instance for some extended usage]I get used factory
manufacturer to make a singleton
as i did with @lazySingleton
Annotation. and obviously callback
will be triggered like state
was emitted.
Then you need to create an instance of it and you are all set.
don’t forget to call reset
In onPointerDown
call back.
add WidgetsBindingObserver
mixins and overrides initState
, dispose
And didChangeAppLifecycleState
ways. Make sure you expand the app StatefulWidget
to do this ! If you don’t, didChangeAppLifecycleState
will not be called.
That’s it. With all of this, you should be able to handle user inactivity anywhere in your application.
As always, this is a proposal, if you know a better way to implement this kind of feature feel free to share it!
[ad_2]
Source link
#Detect #user #inactivity #flutter #applications #Julian #Duriebreaux #August