aman-kassa-flutter/lib/core/services/dialog_service.dart

76 lines
2.5 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:async';
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
import 'package:flutter/cupertino.dart';
class DialogService {
GlobalKey<NavigatorState> _dialogNavigationKey = GlobalKey<NavigatorState>();
Function(DialogRequest) _showDialogListener;
Function(DialogRequest) _showDialogInputListener;
Completer<DialogResponse> _dialogCompleter;
Completer<DialogResponse> get completer => this._dialogCompleter;
GlobalKey<NavigatorState> get dialogNavigationKey => _dialogNavigationKey;
/// Registers a callback function. Typically to show the dialog
void registerDialogListener(Function(DialogRequest) showDialogListener,
Function(DialogRequest) showDialogInputListener) {
_showDialogListener = showDialogListener;
_showDialogInputListener = showDialogInputListener;
}
/// Calls the dialog listener and returns a Future that will wait for dialogComplete.
Future<DialogResponse> showDialog({
String title = 'Aman Касса',
String description,
String buttonTitle = 'Ok',
}) {
_dialogCompleter = Completer<DialogResponse>();
_showDialogListener(DialogRequest(
title: title,
description: description,
buttonTitle: buttonTitle,
));
return _dialogCompleter.future;
}
/// Shows a confirmation dialog
Future<DialogResponse> showConfirmationDialog(
{String title,
String description,
String confirmationTitle = 'Ok',
String cancelTitle = 'Cancel'}) {
_dialogCompleter = Completer<DialogResponse>();
_showDialogListener(DialogRequest(
title: title,
description: description,
buttonTitle: confirmationTitle,
cancelTitle: cancelTitle));
return _dialogCompleter.future;
}
Future<DialogResponse> showConfirmationDialogInput(
{String title = ' Aman Касса',
String description,
String confirmationTitle = 'Ok',
String cancelTitle = 'Cancel',
String formatType}) {
_dialogCompleter = Completer<DialogResponse>();
_showDialogInputListener(DialogRequest(
title: title,
description: description,
buttonTitle: confirmationTitle,
cancelTitle: cancelTitle,
formatType: formatType));
return _dialogCompleter.future;
}
/// Completes the _dialogCompleter to resume the Future's execution call
void dialogComplete(DialogResponse response) {
_dialogNavigationKey.currentState.pop();
_dialogCompleter.complete(response);
_dialogCompleter = null;
}
}