简体   繁体   English

当一个区域在 10 秒内被触摸 5 次时,如何在 Dart/Flutter 中编码以启用我的调试模式

[英]How can I code in Dart / Flutter to enable my debug mode when a region is touched 5 times in 10 seconds

I am using Flutter and the Bloc Pattern with rxdart and would like to have a debug mode in my app similar like you enable the developer mode in Android the user should touch 5 times a logo in the UI.我正在将 Flutter 和 Bloc 模式与 rxdart 一起使用,并希望在我的应用程序中有一个调试模式,就像您在 Android 中启用开发人员模式一样,用户应该在 UI 中触摸 5 次徽标。 The UI part is pretty simple with: UI 部分非常简单:

Column _renderLogo(BuildContext context) => new Column(
        children: <Widget>[
          GestureDetector(
            onTap: () => BlocProvider.of(context).debugEnabledSink.add(true), 
            child: Container( ...more logo rendering...

With that in mind, I am looking for an easy elegant way to enable the detection of 5 events in 10 seconds.考虑到这一点,我正在寻找一种简单优雅的方法来在 10 秒内检测 5 个事件。 The whole detection should reset when not enough events are detected in any 10 second time window.当在任何 10 秒时间窗口中没有检测到足够的事件时,整个检测应重置。

You can use a pseudo-timer to achieve this:您可以使用伪计时器来实现这一点:

const maxDebugTimerSeconds = 10;
const maxTapCount = 5;

DateTime firstTap;
int tapCount;

void doGestureOnTap() {
  final now = DateTime.now();
  if (firstTap != null && now.difference(firstTap).inSeconds < maxDebugTimerSeconds) {
    tapCount++;
    if (tapCount >= maxTapCount) {
      BlocProvider.of(context).debugEnabledSink.add(true);
    }
  } else {
    firstTap = now;
    tapCount = 0;
  }
}

...

GestureDetector(
  onTap: doGestureOnTap,
  ...
),

This answer is based on the comment of @pskink above.这个答案基于上面@pskink 的评论。 I just post it here because it seems like a very elegant way of solving my problem.我只是把它贴在这里,因为它似乎是解决我的问题的一种非常优雅的方式。 Thank you @pskink谢谢@pskink

DebugSwitcher might not be the best class Name it's more TimeWindowEventDetector but you get the idea. DebugSwitcher 可能不是最好的类名称,它更像是 TimeWindowEventDetector,但您明白了。


class DebugSwitcher {
  final Duration window;
  final int cnt;
  bool value = false;
  var timeStamps = [];

  DebugSwitcher({this.window = const Duration(seconds: 10), this.cnt = 5});

  call(data, sink) {
    var now = DateTime.now();
    timeStamps.removeWhere((ts) => now.difference(ts) >= window);
    timeStamps.add(now);
    if (timeStamps.length >= cnt) {
      timeStamps = [];
      sink.add(value = !value);
    }
  }
}

Than u listen to the Sink Events this way:比你这样听接收器事件:

 _debugEnabledController.stream
        .transform(StreamTransformer.fromHandlers(handleData: DebugSwitcher()))
        .listen(
      (_) {
        _isDebugModeOn.value = true;
        infoInternal.value = 'Enabled Debug Mode';
      },
    );

The Sink is defined like this: Sink 定义如下:

  final StreamController<bool> _debugEnabledController =
      StreamController<bool>();

  Sink<bool> get debugEnabledSink => _debugEnabledController.sink;

In the UI the code looks like this:在 UI 中,代码如下所示:

 Column _renderLogo(BuildContext context) => new Column(
        children: <Widget>[
          GestureDetector(
            onTap: () => BlocProvider.of(context).debugEnabledSink.add(true),
            child: Container(
              margin: const EdgeInsets.only(top: 10.0, right: 10.0),
              height: 80.0,
              child: Theme.of(context).primaryColorBrightness ==
                      Brightness.light
                  ? new Image.asset('assets/app_icon_light.png', fit: BoxFit.contain)
                  : new Image.asset('assets/app_icon.png', fit: BoxFit.contain),
            ),
          ),
        ],
      );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在VS Code中的调试中以dart v2模式运行我的flutter应用程序(--preview-dart-2选项) - How do I run my flutter app in dart v2 mode in debug in VS Code (--preview-dart-2 option) 我怎样才能让我的搜索历史不会在颤振/飞镖中重复多次相同的元素? - How can I make my search history not have the same elements repeated many times in flutter/dart? 如何启用 Flutter/Dart 语言实验? - How can I enable Flutter/Dart language experiments? 当我再次切换回选项卡时,如何阻止我未来的构建者重新加载? - 颤振/飞镖 - How can I stop my future builders to reload when I switch back to the tab again? - Flutter/dart 如何在 flutter 的后台运行 dart 代码? - How can I run dart code in the background in flutter? 使用flutter时如何“完成”我的dart SDK而不是安装单独的版本? - How can i 'complete' my dart SDK rather than installing a separate version when using flutter? 安装Flutter后如何使用dart - How can I use the dart when I installed the Flutter 如何开始我的 Dart &amp; Flutter 之旅? - How can I start my journey in Dart & Flutter? 我如何在 dart-flutter 上获得代码安全性 - How can i get code safety on dart-flutter 我如何(为了调试目标)写入我在调试模式下在颤振中运行应用程序的 PC 上的文件? - How can i (for debug goals) write to files on the pc which i run the app in debug mode on in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM