Flutter节流与防抖(本篇是防抖)

参考地址https://blog.csdn.net/sinat_17775997/article/details/107108687?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161707050116780357293444%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161707050116780357293444&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-1-107108687.first_rank_v2_pc_rank_v29&utm_term=Flutter%E5%86%99%E9%98%B2%E6%8A%96

  Future _unfollowSchool() async {
    var params = {
      'phoneNum':phoneNum,
      'schoolName':widget.arguments['title']
    };
    print('params:${params}');
    await request('unfollowSchool','delete',params).then((val){
      if(val['msg']=='成功'){
        setState(() {
          _hasStar = false;
        });
        _toast('取消收藏成功',1);
      }else{
        _toast(val['msg'],0);
      }
    });
    return _hasStar;
  }

// debounce.dart

import 'dart:async';

/// 函数防抖
///
/// [func]: 要执行的方法
/// [delay]: 要迟延的时长
Function debounce(
    Function func, [
      Duration delay = const Duration(milliseconds: 500),
    ]) {
  Timer timer;
  Function target = () {
    if (timer?.isActive ?? false) {
      timer?.cancel();
    }
    timer = Timer(delay, () {
      func?.call();
    });
  };
  return target;
}

这里发现,先toast弹框要后弹出,先改变按钮颜色,会让视觉效果好很多,toast先运行的话会等几秒,感觉上像按钮故障或者反应迟钝

防抖的效果不如节流流畅,记得时间要设置的小一点

你可能感兴趣的:(flutter,flutter,dart)