[英]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.