Skip to content

Commit

Permalink
Merge pull request #656 from hpi-studyu/fix/app/redundant-click-preve…
Browse files Browse the repository at this point in the history
…ntion

fix: redundant click prevention bug
  • Loading branch information
johannesvedder committed Jul 12, 2024
2 parents 372f8dd + 73649cc commit c537426
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CheckmarkTaskWidget extends StatefulWidget {
}

class _CheckmarkTaskWidgetState extends State<CheckmarkTaskWidget> {
DateTime _lastClickTime = DateTime.now();
DateTime? _lastClickTime;
bool _isLoading = false;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QuestionnaireTaskWidget extends StatefulWidget {
class _QuestionnaireTaskWidgetState extends State<QuestionnaireTaskWidget> {
dynamic response;
late bool responseValidator;
DateTime _lastClickTime = DateTime.now();
DateTime? _lastClickTime;
bool _isLoading = false;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();

Expand Down Expand Up @@ -86,12 +86,8 @@ class _QuestionnaireTaskWidgetState extends State<QuestionnaireTaskWidget> {
backgroundColor: WidgetStateProperty.all<Color>(Colors.green),
),
onPressed: () async {
if (isRedundantClick(_lastClickTime)) {
return;
}
if (!formKey.currentState!.validate()) {
return;
}
if (isRedundantClick(_lastClickTime)) return;
if (!formKey.currentState!.validate()) return;
setState(() {
_isLoading = true;
_lastClickTime = DateTime.now();
Expand Down
5 changes: 3 additions & 2 deletions app/lib/util/misc.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
bool isRedundantClick(
DateTime lastClickTime, {
Duration interval = const Duration(seconds: 2),
DateTime? lastClickTime, {
Duration interval = const Duration(seconds: 1),
}) {
if (lastClickTime == null) return false;
final now = DateTime.now();
if (now.difference(lastClickTime) > interval) {
return false;
Expand Down

0 comments on commit c537426

Please sign in to comment.