Giter Site home page Giter Site logo

locker's Introduction

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

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'),
      ),
    );
  }
}

Then, use LockerKey to create a unique key for each action you want to lock:

class _MyHomePageState extends State<MyHomePage> {
  final _countKey = LockerKey.unique();
  // ...
}

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);
  // ...
}

Lock your function execution by calling lock on the context with the locker key:

floatingActionButton: FloatingActionButton(
  onPressed: () async {
    await context.locker(_countKey).lock(
      () => _incrementCounter(),
    );
  },
  // ...
),

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.

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.

locker's People

Contributors

soraef avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.