80 lines
2.6 KiB
Dart
80 lines
2.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:satu/core/models/dialog_models.dart';
|
|
|
|
class DialogService {
|
|
final GlobalKey<NavigatorState> _dialogNavigationKey =
|
|
GlobalKey<NavigatorState>();
|
|
late Function(DialogRequest)? _showDialogListener;
|
|
late Function(DialogRequest)? _showDialogInputListener;
|
|
Completer<DialogResponse>? _dialogCompleter;
|
|
|
|
Completer<DialogResponse>? get completer => _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 = 'SATU',
|
|
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({
|
|
required String title,
|
|
required String requestPrice,
|
|
required String requestCount,
|
|
String? description,
|
|
String confirmationTitle = 'ПОДТВЕРДИТЬ',
|
|
String cancelTitle = 'Отмена',
|
|
}) {
|
|
_dialogCompleter = Completer<DialogResponse>();
|
|
_showDialogInputListener!(DialogRequest(
|
|
title: title,
|
|
description: description ?? '',
|
|
buttonTitle: confirmationTitle,
|
|
cancelTitle: cancelTitle,
|
|
requestPrice: requestPrice,
|
|
requestCount: requestCount));
|
|
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;
|
|
}
|
|
}
|