import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_styled_toast/flutter_styled_toast.dart'; import 'package:invoiceninja_flutter/constants.dart'; import 'package:invoiceninja_flutter/main_app.dart'; import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart'; Completer snackBarCompleter(BuildContext context, String message, {bool shouldPop = false}) { final Completer completer = Completer(); completer.future.then((_) { if (shouldPop && Navigator.of(context).canPop()) { Navigator.of(context).pop(); } showToast(message); }).catchError((Object error) { if (shouldPop && Navigator.of(context).canPop()) { Navigator.of(context).pop(); } showDialog( context: navigatorKey.currentContext, builder: (BuildContext context) { return ErrorDialog(error); }); }); return completer; } Completer popCompleter(BuildContext context, dynamic result) { final Completer completer = Completer(); completer.future.then((_) { Navigator.of(context).pop(result); }).catchError((Object error) { showDialog( context: navigatorKey.currentContext, builder: (BuildContext context) { return ErrorDialog(error); }); }); return completer; } Completer errorCompleter(BuildContext context) { final Completer completer = Completer(); completer.future.catchError((Object error) { showDialog( context: navigatorKey.currentContext, builder: (BuildContext context) { return ErrorDialog(error); }); }); return completer; } // https://stackoverflow.com/a/55119208/497368 class Debouncer { Debouncer({this.milliseconds = kMillisecondsToDebounceUpdate}); final int milliseconds; static VoidCallback action; static Timer timer; void run(VoidCallback action) { if (milliseconds == null) { action(); return; } if (timer == null) { action(); } else { timer.cancel(); Debouncer.action = action; } timer = Timer(Duration(milliseconds: milliseconds), () { if (action != null) { action(); } Debouncer.action = null; Debouncer.timer = null; }); } static void complete() { if (action != null) { action(); } } static void runOnComplete(Function callback) { complete(); callback(); } }