Last updated:
0 purchases
locker
Locker #
Locker is a Flutter library designed to prevent duplicate actions, such as multiple taps on a button by a user. This simple yet effective solution ensures that actions are not performed multiple times, even if the UI element (like a button) is triggered multiple times in quick succession. Locker is perfect for preventing repeated form submissions, API calls, or any other actions that should not be executed more than once at the same time.
Features #
Prevents multiple function executions when UI elements are activated multiple times.
Easy to integrate with any Flutter application.
Utilizes LockerScope and LockerKey to manage lock states seamlessly.
Installation #
To use Locker in your Flutter app, add locker to your pubspec.yaml file:
dependencies:
locker: ^1.0.0
copied to clipboard
Then run flutter packages get to install the new dependency.
Usage #
To integrate Locker into your Flutter application, wrap your root widget with LockerScope:
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return LockerScope(
child: MaterialApp(
// ...
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
copied to clipboard
Then, use LockerKey to create a unique key for each action you want to lock:
class _MyHomePageState extends State<MyHomePage> {
final _countKey = LockerKey.unique();
// ...
}
copied to clipboard
Use watchLocked to listen for the lock status and provide a visual indicator like CircularProgressIndicator when the action is locked:
@override
Widget build(BuildContext context) {
final isLocked = context.watchLocked(_countKey);
// ...
}
copied to clipboard
Lock your function execution by calling lock on the context with the locker key:
floatingActionButton: FloatingActionButton(
onPressed: () async {
await context.locker(_countKey).lock(
() => _incrementCounter(),
);
},
// ...
),
copied to clipboard
or you can use LockerButtonBuilder.
floatingActionButton: LockerButtonBuilder(
lockerKey: _countKey,
onPressed: () => _incrementCounter(),
builder: (context, isLocked, onPressed) {
return FloatingActionButton(
onPressed: isLocked ? null : onPressed,
tooltip: 'Increment',
child: const Icon(Icons.add),
);
},
), // This trailing comma makes auto-formatting nicer for build methods.
copied to clipboard
Contributing #
Contributions to Locker are welcome! Feel free to report issues, open a pull request, or suggest improvements to the documentation.
License #
Locker is released under the MIT License. See the LICENSE file for more information.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.