-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathdialog.dart
101 lines (95 loc) · 3.52 KB
/
dialog.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../generated/l10n/zulip_localizations.dart';
import 'actions.dart';
/// A platform-appropriate action for [AlertDialog.adaptive]'s [actions] param.
Widget _adaptiveAction({required VoidCallback onPressed, required String text}) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return TextButton(
onPressed: onPressed,
child: Text(
text,
// As suggested by
// https://api.flutter.dev/flutter/material/AlertDialog/actions.html :
// > It is recommended to set the Text.textAlign to TextAlign.end
// > for the Text within the TextButton, so that buttons whose
// > labels wrap to an extra line align with the overall
// > OverflowBar's alignment within the dialog.
textAlign: TextAlign.end));
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return CupertinoDialogAction(onPressed: onPressed, child: Text(text));
}
}
/// Tracks the status of a dialog, in being still open or already closed.
///
/// See also:
/// * [showDialog], whose return value this class is intended to wrap.
class DialogStatus {
const DialogStatus(this.closed);
/// Resolves when the dialog is closed.
final Future<void> closed;
}
/// Displays an [AlertDialog] with a dismiss button
/// and optional "Learn more" button.
///
/// The [DialogStatus.closed] field of the return value can be used
/// for waiting for the dialog to be closed.
// This API is inspired by [ScaffoldManager.showSnackBar]. We wrap
// [showDialog]'s return value, a [Future], inside [DialogStatus]
// whose documentation can be accessed. This helps avoid confusion when
// intepreting the meaning of the [Future].
DialogStatus showErrorDialog({
required BuildContext context,
required String title,
String? message,
Uri? learnMoreButtonUrl,
}) {
final zulipLocalizations = ZulipLocalizations.of(context);
final future = showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog.adaptive(
title: Text(title),
content: message != null ? SingleChildScrollView(child: Text(message)) : null,
actions: [
if (learnMoreButtonUrl != null)
_adaptiveAction(
onPressed: () => PlatformActions.launchUrl(context, learnMoreButtonUrl),
text: zulipLocalizations.errorDialogLearnMore,
),
_adaptiveAction(
onPressed: () => Navigator.pop(context),
text: zulipLocalizations.errorDialogContinue),
]));
return DialogStatus(future);
}
void showSuggestedActionDialog({
required BuildContext context,
required String title,
required String message,
required String? actionButtonText,
required VoidCallback onActionButtonPress,
}) {
final zulipLocalizations = ZulipLocalizations.of(context);
showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog.adaptive(
title: Text(title),
content: SingleChildScrollView(child: Text(message)),
actions: [
_adaptiveAction(
onPressed: () => Navigator.pop(context),
text: zulipLocalizations.dialogCancel),
_adaptiveAction(
onPressed: () {
onActionButtonPress();
Navigator.pop(context);
},
text: actionButtonText ?? zulipLocalizations.dialogContinue),
]));
}