如果某人没有在 timefrate 内将文本输入第一个对话框,则显示第二个对话框?
show second Dialog if someone does not input text into the first Dialog within a timefrate?
我制作了一个对话框,如果有人正确输入了某个单词,该对话框可以关闭。我想知道如果有人在特定时间范围内没有在第一个文本框中输入任何内容,是否有可能出现第二个对话框。例如,如果此人在接下来的 5 分钟内没有输入文本,是否有任何方法可以触发第二个对话框显示?
Future.delayed(Duration(seconds: 5), () {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text('Your time finished'),
content: Text('Type "OKAY" if you want to go back to the homepage'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: TextFormField(
controller: _textEditingController,
validator: (String? word){
if (word != OKAY){
_NoDialog();
}
else{
Navigator.pop(context);
}
},
),
),
],
);
});
});
如果此人提供的文本不正确,我将调用函数 NoDialog
,但如果此人未在文本框中输入任何内容,我不确定如何进行 'if' 声明一段时间。
我想我可以做一个 'if' 声明,如果这个人将文本留空,然后调用一个附加了 Future.delayed 的新函数,但我找不到一种方法来为 'if' 声明没有输入文本的情况。同样在这种情况下,我相信该人必须按下 'enter' 键,但我希望显示第二个对话框,因为用户没有触摸屏幕上的任何内容。这可能吗?
在简单的情况下,您可以通过传递相同的构建上下文来显示嵌套重叠对话框。
第一个对话框将是
_userInputCheckerDialog({
required BuildContext context,
required String correctText,
Duration maxTime = const Duration(seconds: 2),
}) async {
bool _noInteraction = true;
/// apear if user doesnt type anything within
Future.delayed(maxTime).then((t) async {
if (_noInteraction) await _showSecondDialog(context);
});
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return StatefulBuilder(
///may need in future to change variable inside dialog
builder: (context, setStateSB) {
return AlertDialog(
content: TextField(
onChanged: (value) async {
_noInteraction = false;
if (value == correctText) {
Navigator.of(context)
.pop(); // close after getting currect text
}
},
),
);
},
);
},
);
}
然后内部对话框具有相同的上下文
_showSecondDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Text("Hey start typing"),
),
);
}
this is only checking 1st time of user interaction.
如果要使用timeframe,我们需要用async package取消(不是真的取消)未来。
你可能会觉得它比第一个复杂一点。
使用异步
_showSecondDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Text("Hey start typing"),
),
);
}
CancelableOperation _restartTimer({
CancelableOperation? cancelableOperation,
Duration duration = const Duration(seconds: 4),
required BuildContext context,
}) {
if (cancelableOperation != null) {
cancelableOperation.cancel();
cancelableOperation = null;
}
cancelableOperation = CancelableOperation.fromFuture(
Future.delayed(
duration,
),
).then((p0) async {
await _showSecondDialog(context);
});
return cancelableOperation;
}
_userInputCheckerDialog({
required BuildContext context,
required String correctText,
Duration maxTime = const Duration(seconds: 2),
}) async {
CancelableOperation? _cancelableOperation;
///start timer at startUp
_cancelableOperation = _restartTimer(
context: context,
cancelableOperation: _cancelableOperation,
duration: maxTime,
);
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return StatefulBuilder(
///may need in future to change variable inside dialog
builder: (context, setStateSB) {
return AlertDialog(
content: TextField(
onChanged: (value) async {
_cancelableOperation = _restartTimer(
context: context,
cancelableOperation: _cancelableOperation);
if (value == correctText) {
_cancelableOperation!.cancel();
Navigator.of(context)
.pop(); // close after getting currect text
}
},
),
);
},
);
},
);
}
我制作了一个对话框,如果有人正确输入了某个单词,该对话框可以关闭。我想知道如果有人在特定时间范围内没有在第一个文本框中输入任何内容,是否有可能出现第二个对话框。例如,如果此人在接下来的 5 分钟内没有输入文本,是否有任何方法可以触发第二个对话框显示?
Future.delayed(Duration(seconds: 5), () {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text('Your time finished'),
content: Text('Type "OKAY" if you want to go back to the homepage'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: TextFormField(
controller: _textEditingController,
validator: (String? word){
if (word != OKAY){
_NoDialog();
}
else{
Navigator.pop(context);
}
},
),
),
],
);
});
});
如果此人提供的文本不正确,我将调用函数 NoDialog
,但如果此人未在文本框中输入任何内容,我不确定如何进行 'if' 声明一段时间。
我想我可以做一个 'if' 声明,如果这个人将文本留空,然后调用一个附加了 Future.delayed 的新函数,但我找不到一种方法来为 'if' 声明没有输入文本的情况。同样在这种情况下,我相信该人必须按下 'enter' 键,但我希望显示第二个对话框,因为用户没有触摸屏幕上的任何内容。这可能吗?
在简单的情况下,您可以通过传递相同的构建上下文来显示嵌套重叠对话框。
第一个对话框将是
_userInputCheckerDialog({
required BuildContext context,
required String correctText,
Duration maxTime = const Duration(seconds: 2),
}) async {
bool _noInteraction = true;
/// apear if user doesnt type anything within
Future.delayed(maxTime).then((t) async {
if (_noInteraction) await _showSecondDialog(context);
});
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return StatefulBuilder(
///may need in future to change variable inside dialog
builder: (context, setStateSB) {
return AlertDialog(
content: TextField(
onChanged: (value) async {
_noInteraction = false;
if (value == correctText) {
Navigator.of(context)
.pop(); // close after getting currect text
}
},
),
);
},
);
},
);
}
然后内部对话框具有相同的上下文
_showSecondDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Text("Hey start typing"),
),
);
}
this is only checking 1st time of user interaction.
如果要使用timeframe,我们需要用async package取消(不是真的取消)未来。 你可能会觉得它比第一个复杂一点。
使用异步
_showSecondDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Text("Hey start typing"),
),
);
}
CancelableOperation _restartTimer({
CancelableOperation? cancelableOperation,
Duration duration = const Duration(seconds: 4),
required BuildContext context,
}) {
if (cancelableOperation != null) {
cancelableOperation.cancel();
cancelableOperation = null;
}
cancelableOperation = CancelableOperation.fromFuture(
Future.delayed(
duration,
),
).then((p0) async {
await _showSecondDialog(context);
});
return cancelableOperation;
}
_userInputCheckerDialog({
required BuildContext context,
required String correctText,
Duration maxTime = const Duration(seconds: 2),
}) async {
CancelableOperation? _cancelableOperation;
///start timer at startUp
_cancelableOperation = _restartTimer(
context: context,
cancelableOperation: _cancelableOperation,
duration: maxTime,
);
await showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return StatefulBuilder(
///may need in future to change variable inside dialog
builder: (context, setStateSB) {
return AlertDialog(
content: TextField(
onChanged: (value) async {
_cancelableOperation = _restartTimer(
context: context,
cancelableOperation: _cancelableOperation);
if (value == correctText) {
_cancelableOperation!.cancel();
Navigator.of(context)
.pop(); // close after getting currect text
}
},
),
);
},
);
},
);
}