搜索插件、代码仓与 APP 分类

插件详情v1.0.0+5

easy_debounce_throttle

一个易于使用的 Flutter 包,提供带有 Stream 和 WidgetBuilder 的防抖和节流功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
6
喜欢数
5
Pub 分数
160

安装插件

flutter pub add easy_debounce_throttle

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的Flutter包,可简化为流和组件实现去抖动和节流功能。它提供了直观的方法来延迟或限制回调的执行,从而在搜索输入或实时表单验证等场景中提升性能。该包与Stream和WidgetBuilder无缝集成,便于在Flutter的响应式UI框架中使用。专为寻求简洁、可重用逻辑而无需样板代码的开发者设计。

适合场景

  • 实时搜索输入处理
  • 带延迟的表单字段验证
  • 减少API调用频率
  • 优化用户输入处理
  • 基于流的事件节流
  • 增强UI响应能力

主要特点

  • 简单的防抖和节流 API
  • 流集成
  • WidgetBuilder 支持
  • 最小化样板代码
  • 跨平台兼容性
  • 简洁、可读性高的语法

适合对象

  • 开发交互式表单的开发者
  • 频繁用户输入的应用程序
  • 性能关键的UI组件
  • Flutter 初学者学习响应式模式
  • 注重可维护性代码的团队

使用建议

  • 不适用于高频、低延迟事件
  • 需要理解流(Streams)
  • 除了基本的防抖/节流之外,自定义选项有限
  • 对于简单用例,可能会增加轻微开销
  • 最好与异步操作或副作用一起使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Easy Debounce Throttle

Platform Pub Package Build Status License: MIT


An easy-to-use flutter package that provides debounce and throttle with Stream and WidgetBuilder.

Usage

If you want to subscribe to debounce, throttle events with Stream, use EasyDebounce, EasyThrottle

you should call the close() method to avoid memory leak

EasyDebounce
 final EasyDebounce _easyDebounce = EasyDebounce(delay: const Duration(milliseconds: 1000));
 final TextEditingController _textController = TextEditingController();
 
 @override
 void initState() {
 super.initState();
 _textController.addListener(() {
   _easyDebounce.debounce(_textController.text);
 });
 _easyDebounce.listen((data) {
   setState(() {
     _debounceText = '$data';
   });
 });
 }
 
 @override
 void dispose() {
 _easyDebounce.close();
 super.dispose();
}
EasyThrottle
 final EasyThrottle _easyThrottle = EasyThrottle(delay: const Duration(milliseconds: 1000));
 final TextEditingController _textController = TextEditingController();
 
 @override
 void initState() {
 super.initState();
 _textController.addListener(() {
   _easyThrottle.throttle(_textController.text);
 });
 _easyThrottle.listen((data) {
   setState(() {
     _throttleText = '$data';
   });
 });
 }
 
 @override
 void dispose() {
 _easyThrottle.close();
 super.dispose();
 }

If you want to receive debounce and throttle events from methods such as onPressed callbacks with WidgetBuilder, use EasyDebounceBuilder and EasyThrottleBuilder.

EasyDebounceBuilder and EasyThrottleBuilder internally disposes the stream, so there is no need to call the close() method.

EasyDebounceBuilder
EasyDebounceBuilder(
    delay: const Duration(milliseconds: 1000),
    builder: (context, debounce) {
      return TextButton(
          onPressed: () => debounce(() => _increaseDebounceCount()),
       // onPressed: () {
       //   debounce(() {
       //     _increaseDebounceCount();
       //   });
       // },
          child: Text(
            'increase debounce count',
            style: kNotoSansBold14.copyWith(color: Colors.white),
          ));
    }),
EasyThrottleBuilder
EasyThrottleBuilder(
    delay: const Duration(milliseconds: 1000),
    builder: (context, throttle) {
      return TextButton(
          onPressed: () => throttle(() => _increaseThrottleCount()),
       // onPressed: () {
       //   throttle(() {
       //     _increaseThrottleCount();
       //   });
       // },
          child: Text(
            'increase throttle count',
            style: kNotoSansBold14.copyWith(color: Colors.white),
          ));
    }),

Examples

An example can be found in the example directory of this repository.

A list of detailed examples can be found in this Examples Repository

EasyDebounce EasyThrottle
EasyDebounceBuilder EasyThrottleBuilder

Installing

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  easy_debounce_throttle: ^1.0.0

or

$ flutter pub add easy_debounce_throttle
2. Install it

You can install packages from the command line:

$ flutter pub get
3. Import it

Now in your Dart code, you can use:

import 'package:easy_debounce_throttle/easy_debounce_throttle.dart';

License

MIT License

Copyright (c) 2022 seosh817

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。