Line data Source code
1 : // ignore_for_file: avoid_dynamic_calls
2 :
3 : import 'package:basf_flutter_components/basf_flutter_components.dart';
4 : import 'package:flutter/material.dart';
5 :
6 : /// {@template basf_alert_dialog}
7 : /// A BASF-styled customized alert dialog
8 : /// {@endtemplate}
9 : class BasfAlertDialog extends StatelessWidget {
10 : /// {@macro basf_alert_dialog}
11 1 : const BasfAlertDialog({
12 : super.key,
13 : required this.description,
14 : this.title = 'Warning',
15 : this.dismissText = 'Cancel',
16 : this.confirmText = 'Confirm',
17 : this.onConfirmed,
18 : this.onDismissed,
19 : this.onlyConfirm = false,
20 : });
21 :
22 : /// Alert title
23 : final String title;
24 :
25 : /// Alert description
26 : final String description;
27 :
28 : /// Dismiss text action
29 : final String dismissText;
30 :
31 : /// Confirm text action
32 : final String confirmText;
33 :
34 : /// Action to be performed when its confirmed
35 : final Function? onConfirmed;
36 :
37 : /// Action to be performed when its dismissed
38 : final Function? onDismissed;
39 :
40 : /// Hides dismiss option
41 : final bool onlyConfirm;
42 :
43 1 : @override
44 : Widget build(BuildContext context) {
45 1 : return AlertDialog(
46 : shape:
47 2 : RoundedRectangleBorder(borderRadius: BasfThemes.defaultBorderRadius),
48 1 : title: Text(
49 1 : title,
50 : textAlign: TextAlign.center,
51 2 : style: BasfThemes.mainTextTheme.headline6,
52 : ),
53 1 : content: Text(
54 1 : description,
55 : textAlign: TextAlign.center,
56 2 : style: BasfThemes.mainTextTheme.subtitle1,
57 : ),
58 1 : actions: <Widget>[
59 2 : if (!onlyConfirm) _dismissButton(),
60 2 : if (!onlyConfirm) VerticalSpacer.normal(),
61 1 : _confirmButton(),
62 : ],
63 : );
64 : }
65 :
66 1 : Widget _dismissButton() {
67 1 : return Builder(
68 1 : builder: (context) {
69 1 : return Padding(
70 : padding: const EdgeInsets.only(left: Dimens.paddingMedium),
71 1 : child: BasfOutlinedButton(
72 : expanded: true,
73 1 : text: dismissText,
74 1 : style: OutlinedButton.styleFrom(
75 : primary: BasfColors.red,
76 : side: const BorderSide(color: BasfColors.red),
77 : ),
78 0 : onPressed: () {
79 0 : final dynamic result = onDismissed?.call();
80 0 : Navigator.of(context).pop(result);
81 : },
82 : ),
83 : );
84 : },
85 : );
86 : }
87 :
88 1 : Widget _confirmButton() {
89 1 : return Builder(
90 1 : builder: (context) {
91 1 : return Padding(
92 : padding: const EdgeInsets.only(left: Dimens.paddingMedium),
93 1 : child: BasfTextButton.contained(
94 1 : text: confirmText,
95 : expanded: true,
96 0 : onPressed: () {
97 0 : final dynamic result = onConfirmed?.call();
98 0 : Navigator.of(context).pop(result);
99 : },
100 : ),
101 : );
102 : },
103 : );
104 : }
105 : }
|