fbmobile/lib/core/services/dialog_service.dart

62 lines
1.9 KiB
Dart
Raw Normal View History

2021-02-02 14:33:23 +00:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.dart';
import '../datamodels/dialog_request.dart';
import '../datamodels/dialog_response.dart';
class DialogService {
2023-01-16 00:44:34 +00:00
final GlobalKey<NavigatorState> _dialogNavigationKey =
GlobalKey<NavigatorState>();
late Function(DialogRequest) _showDialogListener;
Completer<DialogResponse>? _dialogCompleter;
2021-02-02 14:33:23 +00:00
GlobalKey<NavigatorState> get dialogNavigationKey => _dialogNavigationKey;
void registerDialogListener(Function(DialogRequest) showDialogListener) {
_showDialogListener = showDialogListener;
}
Future<DialogResponse> showDialog({
String? title,
String? description,
String? buttonTitleAccept,
2021-02-02 14:33:23 +00:00
}) {
_dialogCompleter = Completer<DialogResponse>();
_showDialogListener(DialogRequest(
title: title,
description: description,
buttonTitleAccept:
2023-01-16 00:44:34 +00:00
buttonTitleAccept == null || buttonTitleAccept.isEmpty
? translate('dialog.confirm')
: buttonTitleAccept));
return _dialogCompleter!.future;
2021-02-02 14:33:23 +00:00
}
Future<DialogResponse> showConfirmationDialog(
2023-01-16 00:44:34 +00:00
{String? title,
String? description,
String? buttonTitleAccept,
String? buttonTitleDeny}) {
2021-02-02 14:33:23 +00:00
_dialogCompleter = Completer<DialogResponse>();
_showDialogListener(DialogRequest(
title: title,
description: description,
buttonTitleAccept:
2023-01-16 00:44:34 +00:00
buttonTitleAccept == null || buttonTitleAccept.isEmpty
? translate('dialog.confirm')
: buttonTitleAccept,
buttonTitleDeny: buttonTitleDeny == null || buttonTitleDeny.isEmpty
? translate('dialog.cancel')
: buttonTitleDeny));
return _dialogCompleter!.future;
2021-02-02 14:33:23 +00:00
}
void dialogComplete(DialogResponse response) {
_dialogNavigationKey.currentState!.pop();
_dialogCompleter!.complete(response);
2021-02-02 14:33:23 +00:00
_dialogCompleter = null;
}
}