Skip to content

A widget to create lock screen like functionality for Flutter apps.

License

Notifications You must be signed in to change notification settings

nico-famedly/flutter_app_lock

 
 

Repository files navigation

flutter_app_lock

A Flutter package for showing a lock screen on app open and app pause.

If the app is launching, the lock screen is shown first and then the rest of the app is instantiated once a successful login has occured.

If the user is returning to the app after it has already launched, the login screen is shown on top of your app and can't be dismissed until another successful login.

Getting Started

In your flutter project add the dependency:

dependencies:
  ...
  flutter_app_lock: ^2.0.0

For help getting started with Flutter, view the online documentation.

Usage example

void main() {
  runApp(AppLock(
    builder: (args) => MyApp(data: args),
    lockScreen: LockScreen(),
  ));
}

Simply wrap the initialization of MyApp (or your equivalent) in a function and pass it to the builder property of an AppLock widget.

LockScreen is your own widget implementing your own login logic which should call the following once a successful login has occured.

AppLock.of(context).didUnlock();

This will instantiate your MyApp (or your equivalent) if it's an app launch or simply returns to the current running instance of your app if it's resuming.

Theme

At present, AppLock uses a MaterialApp internally to push the lockScreen when locking the app. You can override the theme of this MaterialApp by passing through your ThemeData to AppLock using the theme property.

void main() {
  runApp(AppLock(
    builder: (args) => MyApp(data: args),
    lockScreen: LockScreen(),
    theme: ThemeData(
      ...
    ),
  ));
}

Enabling and disabling

It is possible to enable and disable the lockScreen on app launch and on-demand.

runApp(AppLock(
  builder: (args) => MyApp(data: args),
  lockScreen: LockScreen(),
  enabled: false,
));

The above will cause MyApp to be built instantly and lockScreen will never be shown. The default for enabled is true.

You can then enable lockScreen later on by doing:

AppLock.of(context).enable();

This will now cause the lockScreen to be shown on app pauses.

If you wanted to disable the lockScreen again you can simply do:

AppLock.of(context).disable();

There is also a convenience method:

AppLock.of(context).setEnabled(true);
AppLock.of(context).setEnabled(false);

Passing arguments

In some scenarios, it might be appropriate to unlock a database or create some other objects from the lockScreen and then inject them in to your MyApp or equivalent, so you can better guarantee that services are instantiated or databases are opened/unlocked.

You can do this by passing in an argument to the didUnlock method on AppLock:

var database = await openDatabase(...);

AppLock.of(context).didUnlock(database);

This object is then available as part of the AppLock builder method, builder:

...
runApp(AppLock(
  builder: (args) => MyApp(database: args), // args is the `database` object passed in to `didUnlock`
  lockScreen: LockScreen(),
));

Manually showing the lock screen

In some scenarios, you might want to manually trigger the lock screen to show.

You can do this by calling:

AppLock.of(context).showLockScreen();

If you want to wait until the user has successfully unlocked again, showLockScreen returns a Future so you can await this method call.

await AppLock.of(context).showLockScreen();

print('Did unlock!');

Background lock latency

It might be useful for apps to not require the lock screen to be shown immediately after entering the background state. You can now specify how long the app is allowed to be in the background before requiring the lock screen to be shown:

void main() {
  runApp(AppLock(
    ...,
    backgroundLockLatency: const Duration(seconds: 30),
  ));
}

The above example allows the app to be in the background for up to 30 seconds without requiring the lock screen to be shown.

Tests

Integration tests have been introduced in the example project and were used to confirm the behaviour hasn't changed since the move to null-safety.

They can be run by running flutter test integration_test/integration_tests.dart in a terminal.

About

A widget to create lock screen like functionality for Flutter apps.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 89.5%
  • Ruby 7.3%
  • Swift 2.2%
  • Other 1.0%