alert_dialog.dart 9.31 KB
Newer Older
1
import 'package:flutter/material.dart';
CLOUARD Regis's avatar
CLOUARD Regis committed
2
3
4
import 'package:vikazimut/theme.dart';
import 'package:vikazimut/utils/pattern_lock.dart';
import 'package:vikazimut/utils/translations.dart';
5

CLOUARD Regis's avatar
CLOUARD Regis committed
6
7
8
9
10
11
12
13
14
15
16
17
18
/// Credits: https://pub.dev/packages/commons

void showErrorDialog(BuildContext context, {required title, required String message}) {
  const color = const Color(0xffc0392b);
  final icon = const Icon(Icons.close, size: 64, color: Colors.white);

  _buildAndShowDialog(
    context: context,
    dialogIcon: icon,
    title: title,
    color: color,
    message: message,
    positiveButtonText: Translations.getString("ok"),
19
  );
CLOUARD Regis's avatar
CLOUARD Regis committed
20
21
22
23
24
25
26
27
28
29
30
}

Future<void> showConfirmationDialog(
  BuildContext context, {
  required String title,
  required String message,
  required String yesButtonText,
  required String noButtonText,
  VoidCallback? yesButtonAction,
  VoidCallback? noButtonAction,
}) async {
CLOUARD Regis's avatar
CLOUARD Regis committed
31
  const color = primaryColor;
CLOUARD Regis's avatar
CLOUARD Regis committed
32
33
34
35
36
37
38
39
40
41
42
43
  final icon = const Icon(Icons.help_outline, size: 64, color: Colors.white);

  await _buildAndShowDialog(
    context: context,
    dialogIcon: icon,
    title: title,
    color: color,
    message: message,
    positiveButtonText: yesButtonText,
    positiveButtonAction: yesButtonAction,
    negativeButtonText: noButtonText,
    negativeButtonAction: noButtonAction,
44
  );
CLOUARD Regis's avatar
CLOUARD Regis committed
45
}
46

CLOUARD Regis's avatar
CLOUARD Regis committed
47
48
49
50
51
52
53
54
55
56
Future<void> showPatternConfirmationDialog(
  BuildContext context, {
  required String title,
  required String message,
  VoidCallback? yesButtonAction,
}) async {
  var color = Theme.of(context).accentColor;
  final icon = const Icon(Icons.help_outline, size: 64, color: Colors.white);

  await _buildAndShowDialog(
57
    context: context,
CLOUARD Regis's avatar
CLOUARD Regis committed
58
59
60
61
62
63
64
65
    dialogIcon: icon,
    title: title,
    color: color,
    message: message,
    positiveButtonText: "",
    positiveButtonAction: yesButtonAction,
    usePattern: true,
    negativeButtonText: Translations.getString("cancel"),
66
67
68
  );
}

CLOUARD Regis's avatar
CLOUARD Regis committed
69
void showWarningDialog(
CLOUARD Regis's avatar
CLOUARD Regis committed
70
71
72
73
  BuildContext context, {
  required String title,
  required String message,
  required String yesButtonText,
CLOUARD Regis's avatar
CLOUARD Regis committed
74
  String? noButtonText,
CLOUARD Regis's avatar
CLOUARD Regis committed
75
76
  VoidCallback? yesAction,
  VoidCallback? noAction,
CLOUARD Regis's avatar
CLOUARD Regis committed
77
78
  String? checkboxMessage,
  VoidCallback? checkboxAction,
CLOUARD Regis's avatar
CLOUARD Regis committed
79
}) {
CLOUARD Regis's avatar
CLOUARD Regis committed
80
81
82
83
84
  const color = Color(0xffFF8C00);
  final icon = const Icon(
    Icons.warning,
    size: 64,
    color: Colors.white,
85
86
  );

CLOUARD Regis's avatar
CLOUARD Regis committed
87
  _buildAndShowDialog(
88
    context: context,
CLOUARD Regis's avatar
CLOUARD Regis committed
89
90
91
92
93
94
95
96
    dialogIcon: icon,
    title: title,
    color: color,
    message: message,
    positiveButtonText: yesButtonText,
    positiveButtonAction: yesAction,
    checkboxMessage: checkboxMessage,
    checkboxAction: checkboxAction,
97
98
99
  );
}

CLOUARD Regis's avatar
CLOUARD Regis committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Future<void> _buildAndShowDialog({
  required BuildContext context,
  required Icon dialogIcon,
  required String title,
  required Color color,
  required String message,
  required String positiveButtonText,
  String? negativeButtonText,
  VoidCallback? positiveButtonAction,
  VoidCallback? negativeButtonAction,
  String? checkboxMessage,
  VoidCallback? checkboxAction,
  bool usePattern = false,
}) async {
  Widget _dialogContent(
    BuildContext context, {
    required Icon dialogIcon,
CLOUARD Regis's avatar
CLOUARD Regis committed
117
    required String title,
CLOUARD Regis's avatar
CLOUARD Regis committed
118
119
120
121
122
123
124
125
    required Color color,
    required String message,
    required String positiveButtonText,
    String? negativeButtonText,
    VoidCallback? positiveButtonAction,
    VoidCallback? negativeButtonAction,
    String? checkboxMessage,
    VoidCallback? checkboxAction,
CLOUARD Regis's avatar
CLOUARD Regis committed
126
  }) {
CLOUARD Regis's avatar
CLOUARD Regis committed
127
128
129
    var screenWidth = MediaQuery.of(context).size.width;
    List<bool> remember = [false];
    return Dialog(
CLOUARD Regis's avatar
CLOUARD Regis committed
130
131
      insetPadding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 10.0),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16.0)),
CLOUARD Regis's avatar
CLOUARD Regis committed
132
133
      elevation: 0.0,
      backgroundColor: Colors.transparent,
Clouard Regis's avatar
Clouard Regis committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      child: Stack(
        children: <Widget>[
          Container(
            width: screenWidth >= 600 ? 500 : screenWidth,
            padding: const EdgeInsets.only(
              top: 45.0 + 16.0,
              bottom: 16.0,
              left: 16.0,
              right: 16.0,
            ),
            margin: const EdgeInsets.only(top: 55.0),
            decoration: BoxDecoration(
              color: Theme.of(context).dialogBackgroundColor,
              shape: BoxShape.rectangle,
              borderRadius: BorderRadius.circular(16.0),
              boxShadow: [
                const BoxShadow(
                  color: Colors.black26,
                  blurRadius: 10.0,
                  offset: const Offset(0.0, 10.0),
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                if (title.isNotEmpty)
                  Text(
                    title,
                    style: TextStyle(
                      color: color,
                      fontSize: 20.0,
                      fontWeight: FontWeight.w700,
Clouard Regis's avatar
Clouard Regis committed
167
                    ),
Clouard Regis's avatar
Clouard Regis committed
168
169
170
171
172
173
174
175
176
177
                  ),
                const SizedBox(height: 16.0),
                Flexible(
                  fit: FlexFit.loose,
                  child: SingleChildScrollView(
                    child: Text(
                      message,
                      textAlign: TextAlign.center,
                      style: const TextStyle(
                        fontSize: 16.0,
CLOUARD Regis's avatar
CLOUARD Regis committed
178
179
180
                      ),
                    ),
                  ),
Clouard Regis's avatar
Clouard Regis committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                ),
                if (usePattern)
                  PatternLock(
                    secretCode: [0, 1, 2, 3],
                    onSuccess: () {
                      Navigator.of(context).pop();
                      if (positiveButtonAction != null) {
                        positiveButtonAction();
                      }
                    },
                  ),
                const SizedBox(height: 16.0),
                if (checkboxMessage != null && checkboxMessage.isNotEmpty)
                  CheckBoxContainer(
                    checkboxValue: remember,
                    checkboxMessage: checkboxMessage,
                    color: color,
                  ),
                Align(
                  alignment: Alignment.bottomRight,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      if (!usePattern)
                        TextButton(
                          child: Text(
                            positiveButtonText,
                            style: TextStyle(color: color),
CLOUARD Regis's avatar
CLOUARD Regis committed
209
                          ),
Clouard Regis's avatar
Clouard Regis committed
210
211
212
213
214
215
216
217
218
                          onPressed: () {
                            if (remember[0] && checkboxAction != null) {
                              checkboxAction();
                            }
                            Navigator.of(context).pop();
                            if (positiveButtonAction != null) {
                              positiveButtonAction();
                            }
                          },
CLOUARD Regis's avatar
CLOUARD Regis committed
219
                        ),
Clouard Regis's avatar
Clouard Regis committed
220
221
222
223
224
225
                      const SizedBox(width: 40.0,),
                      if (negativeButtonText != null)
                        TextButton(
                          child: Text(
                            negativeButtonText,
                            style: TextStyle(color: color),
CLOUARD Regis's avatar
CLOUARD Regis committed
226
                          ),
Clouard Regis's avatar
Clouard Regis committed
227
228
229
230
231
232
233
234
                          onPressed: () {
                            Navigator.of(context).pop();
                            if (negativeButtonAction != null) {
                              negativeButtonAction();
                            }
                          },
                        ),
                    ],
CLOUARD Regis's avatar
CLOUARD Regis committed
235
                  ),
Clouard Regis's avatar
Clouard Regis committed
236
237
                ),
              ],
CLOUARD Regis's avatar
CLOUARD Regis committed
238
            ),
Clouard Regis's avatar
Clouard Regis committed
239
240
241
242
243
244
245
246
          ),
          Positioned(
            left: 16.0,
            right: 16.0,
            child: CircleAvatar(
              backgroundColor: color,
              radius: 45.0,
              child: dialogIcon,
CLOUARD Regis's avatar
CLOUARD Regis committed
247
            ),
Clouard Regis's avatar
Clouard Regis committed
248
249
          ),
        ],
CLOUARD Regis's avatar
CLOUARD Regis committed
250
      ),
CLOUARD Regis's avatar
CLOUARD Regis committed
251
252
253
    );
  }

CLOUARD Regis's avatar
CLOUARD Regis committed
254
  await showDialog(
CLOUARD Regis's avatar
CLOUARD Regis committed
255
      context: context,
CLOUARD Regis's avatar
CLOUARD Regis committed
256
      barrierDismissible: false,
CLOUARD Regis's avatar
CLOUARD Regis committed
257
      builder: (BuildContext context) {
CLOUARD Regis's avatar
CLOUARD Regis committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        return _dialogContent(
          context,
          dialogIcon: dialogIcon,
          title: title,
          color: color,
          message: message,
          positiveButtonText: positiveButtonText,
          positiveButtonAction: positiveButtonAction,
          negativeButtonText: negativeButtonText,
          negativeButtonAction: negativeButtonAction,
          checkboxMessage: checkboxMessage,
          checkboxAction: checkboxAction,
        );
      });
}

class CheckBoxContainer extends StatefulWidget {
  final List<bool> checkboxValue;
  final String checkboxMessage;
  final Color color;

  const CheckBoxContainer({
    Key? key,
    required this.checkboxValue,
    required this.color,
    required this.checkboxMessage,
  }) : super(key: key);
CLOUARD Regis's avatar
CLOUARD Regis committed
285

CLOUARD Regis's avatar
CLOUARD Regis committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  @override
  _CheckBoxContainerState createState() => _CheckBoxContainerState();
}

class _CheckBoxContainerState extends State<CheckBoxContainer> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Theme(
          data: ThemeData(primarySwatch: Colors.yellow, unselectedWidgetColor: widget.color),
          child: Checkbox(
            value: widget.checkboxValue[0],
            checkColor: Colors.white,
            activeColor: widget.color,
            onChanged: (value) {
              setState(() {
                widget.checkboxValue[0] = value!;
              });
            },
          ),
        ),
        Text(widget.checkboxMessage),
      ],
    );
CLOUARD Regis's avatar
CLOUARD Regis committed
311
  }
CLOUARD Regis's avatar
CLOUARD Regis committed
312
}