fbmobile/lib/core/manager/dialog_manager.dart

67 lines
1.6 KiB
Dart
Raw Permalink Normal View History

2021-02-02 14:33:23 +00:00
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;
2021-02-02 14:33:23 +00:00
const DialogManager({super.key, this.child});
2021-02-02 14:33:23 +00:00
2023-01-04 20:17:54 +00:00
@override
2021-02-02 14:33:23 +00:00
_DialogManagerState createState() => _DialogManagerState();
}
class _DialogManagerState extends State<DialogManager> {
2023-01-04 20:17:54 +00:00
final DialogService _dialogService = locator<DialogService>();
2021-02-02 14:33:23 +00:00
@override
void initState() {
super.initState();
_dialogService.registerDialogListener(_showDialog);
}
@override
Widget build(BuildContext context) {
return widget.child!;
2021-02-02 14:33:23 +00:00
}
void _showDialog(DialogRequest request) {
List<Widget> actions = <Widget>[];
2023-01-16 00:44:34 +00:00
if (request.buttonTitleDeny != null &&
request.buttonTitleDeny!.isNotEmpty) {
Widget denyBtn = TextButton(
child: Text(request.buttonTitleDeny!),
2021-02-02 14:33:23 +00:00
onPressed: () {
_dialogService.dialogComplete(DialogResponse(confirmed: false));
},
);
actions.add(denyBtn);
}
Widget confirmBtn = TextButton(
child: Text(request.buttonTitleAccept!),
2021-02-02 14:33:23 +00:00
onPressed: () {
_dialogService.dialogComplete(DialogResponse(confirmed: true));
},
);
actions.add(confirmBtn);
AlertDialog alert = AlertDialog(
title: Text(request.title!),
content: Text(request.description!),
2021-02-02 14:33:23 +00:00
actions: actions,
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}