Flutter 当一个区域在10秒内被触摸5次时,如何在Dart/FLIFT中编码以启用调试模式
我在rxdart中使用颤振和Bloc模式,希望在我的应用程序中有一个类似于您在Android中启用开发者模式的调试模式。用户应在UI中触摸5次徽标。 UI部分非常简单,包括:Flutter 当一个区域在10秒内被触摸5次时,如何在Dart/FLIFT中编码以启用调试模式,flutter,dart,Flutter,Dart,我在rxdart中使用颤振和Bloc模式,希望在我的应用程序中有一个类似于您在Android中启用开发者模式的调试模式。用户应在UI中触摸5次徽标。 UI部分非常简单,包括: Column _renderLogo(BuildContext context) => new Column( children: <Widget>[ GestureDetector( onTap: () => BlocProvider.o
Column _renderLogo(BuildContext context) => new Column(
children: <Widget>[
GestureDetector(
onTap: () => BlocProvider.of(context).debugEnabledSink.add(true),
child: Container( ...more logo rendering...
考虑到这一点,我正在寻找一种简单、优雅的方法,以便能够在10秒内检测到5个事件。当在任何10秒的时间窗口中未检测到足够的事件时,应重置整个检测。您可以使用伪计时器来实现此目的: 常量maxDebugTimerSeconds=10; 常量maxTapCount=5; 日期时间第一次点击; int-tapCount; 虚空doGestureOnTap{ final now=DateTime.now; 如果firstTap!=null&&now.differencefirstTap.insectonds
这个答案基于上面@pskink的评论。我只是把它贴在这里,因为它似乎是解决我问题的一种非常优雅的方式。谢谢你@pskink 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);
}
}
}
然后,通过以下方式侦听接收器事件:
_debugEnabledController.stream
.transform(StreamTransformer.fromHandlers(handleData: DebugSwitcher()))
.listen(
(_) {
_isDebugModeOn.value = true;
infoInternal.value = 'Enabled Debug Mode';
},
);
水槽的定义如下:
final StreamController<bool> _debugEnabledController =
StreamController<bool>();
Sink<bool> get debugEnabledSink => _debugEnabledController.sink;
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),
),
),
],
);
在UI中,代码如下所示:
final StreamController<bool> _debugEnabledController =
StreamController<bool>();
Sink<bool> get debugEnabledSink => _debugEnabledController.sink;
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),
),
),
],
);
例如,使用StreamTransformer.fromHandlers转换流您可以举出一个示例了解更多详细信息吗?类DebugSwitcher{final Duration window;final int cnt;bool value=false;var timestaps=[];DebugSwitcher this.window,this.cnt;calldata,sink{var now=DateTime.now;timeStamps.removeherets=>now.differencets>=window;timeStamps.addnow;if timeStamps.length>=cnt{timeStamps=[];sink.addvalue=!value;}//printtimeStamps;}}-现在您可以将它与:sc.stream.transformStreamTransformer.fromHandlershandleData:DebuggerSwitcherDurationsSeconds:10,5.listenprint;当然,您不需要单独的类:您可以在一些自定义状态类中实现handleData回调当然,谢谢。我今天将尝试并研究一下。您为什么不将其发布为回答。我看起来很优雅。