fbmobile/lib/core/manager/dialog_manager.dart
2023-01-16 01:44:51 +01:00

66 lines
1.6 KiB
Dart

import 'package:flutter/material.dart';
import '../../locator.dart';
import '../datamodels/dialog_request.dart';
import '../datamodels/dialog_response.dart';
import '../services/dialog_service.dart';
class DialogManager extends StatefulWidget {
final Widget? child;
const DialogManager({Key? key, this.child}) : super(key: key);
@override
_DialogManagerState createState() => _DialogManagerState();
}
class _DialogManagerState extends State<DialogManager> {
final DialogService _dialogService = locator<DialogService>();
@override
void initState() {
super.initState();
_dialogService.registerDialogListener(_showDialog);
}
@override
Widget build(BuildContext context) {
return widget.child!;
}
void _showDialog(DialogRequest request) {
List<Widget> actions = <Widget>[];
if (request.buttonTitleDeny != null &&
request.buttonTitleDeny!.isNotEmpty) {
Widget denyBtn = TextButton(
child: Text(request.buttonTitleDeny!),
onPressed: () {
_dialogService.dialogComplete(DialogResponse(confirmed: false));
},
);
actions.add(denyBtn);
}
Widget confirmBtn = TextButton(
child: Text(request.buttonTitleAccept!),
onPressed: () {
_dialogService.dialogComplete(DialogResponse(confirmed: true));
},
);
actions.add(confirmBtn);
AlertDialog alert = AlertDialog(
title: Text(request.title!),
content: Text(request.description!),
actions: actions,
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}