flutter_app_lock

Creator: coderz1093

Last updated:

Add to Cart

Description:

flutter app lock

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: ^4.1.1
copied to clipboard
For help getting started with Flutter, view the online documentation.
Usage example #
void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
...

@override
Widget build(BuildContext context) {
return MaterialApp(
...,
builder: (context, child) => AppLock(
builder: (context, arg) => child!,
lockScreenBuilder: (context) => LockScreen(),
),
);
}
}
copied to clipboard
Simply use your MaterialApp's builder property to return an AppLock widget, passing child to its own builder property.
Note: While I mention MaterialApp throughout this README, this is also compatible with CupertinoApp and WidgetsApp widgets.
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();
copied to clipboard
This will now make the MaterialApp's Navigator or Router the top most widget.
Theme #
Because AppLock is expected to be returned within your MaterialApp's builder method, your existing theme and theme variants will style the widget returned from lockScreenBuilder automatically.
MaterialApp(
...,
theme: ThemeData(
...,
),
builder: (context, child) => AppLock(
builder: (context, arg) => child!,
lockScreenBuilder: (context) => LockScreen(),
),
);
copied to clipboard
Enabling and disabling #
It is possible to enable and disable the showing of the widget returned from lockScreenBuilder on app launch and on-demand.
MaterialApp(
...,
builder: (context, child) => AppLock(
builder: (context, arg) => child!,
lockScreenBuilder: (context) => LockScreen(),
enabled: false,
),
);
copied to clipboard
The above will cause child (your MaterialApp's Navigator or Router) to be built instantly and the widget returned from lockScreenBuilder will never be shown. The default for enabled is true.
You can then enable the showing of the widget returned from lockScreenBuilder later on by doing:
AppLock.of(context)!.enable();
copied to clipboard
This will now cause the widget returned from lockScreenBuilder to be shown on app pauses.
If you wanted to disable the showing of the widget returned from lockScreenBuilder again you can simply do:
AppLock.of(context)!.disable();
copied to clipboard
There is also a convenience method:
AppLock.of(context)!.setEnabled(true);
AppLock.of(context)!.setEnabled(false);
copied to clipboard
Passing arguments #
In some scenarios, it might be appropriate to unlock a database or create some other objects from the widget returned from lockScreenBuilder and then expose them to your app further down the tree, 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);
copied to clipboard
This object is then available as part of the AppLock builder method, builder:
MaterialApp(
...,
builder: (context, child) => AppLock(
builder: (context, arg) => child!, // arg is the `database` object passed in to `didUnlock`
lockScreenBuilder: (context) => LockScreen(),
),
);
copied to clipboard
It is also available by calling AppLock.of(context)!.launchArg.
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();
copied to clipboard
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!');
copied to clipboard
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:
MaterialApp(
...,
builder: (context, child) => AppLock(
...,
backgroundLockLatency: const Duration(seconds: 30),
),
);
copied to clipboard
The above example allows the app to be in the background for up to 30 seconds without requiring the lock screen to be shown.
Inactive statuses (e.g. app switcher) #
When the app becomes "inactive", for example by launching the device app switcher or device notification center, you can now configure a widget to be shown when this occurs:
MaterialApp(
...,
builder: (context, child) => AppLock(
...,
inactiveBuilder: (context) => InactiveScreen(),
),
);
copied to clipboard
InactiveScreen is your own widget implementing your own requirements for a screen shown while the app is inactive.
There are limitations to this as noted on issue #6 on GitHub.
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.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.