Skip to content

Commit ed8abb7

Browse files
committed
use alertDialog.adaptive and _adaptiveWidget to user Cupertino flavour for ios
1 parent 26c2aa0 commit ed8abb7

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

lib/widgets/dialog.dart

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import 'package:flutter/cupertino.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
4+
import 'package:flutter/foundation.dart';
35

4-
Widget _dialogActionText(String text) {
6+
Widget _materialDialogActionText(String text) {
57
return Text(
68
text,
79

@@ -15,6 +17,20 @@ Widget _dialogActionText(String text) {
1517
);
1618
}
1719

20+
/// A platform-appropriate action for [AlertDialog.adaptive]'s [actions] param.
21+
Widget _adaptiveAction({required VoidCallback onPressed, required String text}) {
22+
switch (defaultTargetPlatform) {
23+
case TargetPlatform.android:
24+
case TargetPlatform.fuchsia:
25+
case TargetPlatform.linux:
26+
case TargetPlatform.windows:
27+
return TextButton(onPressed: onPressed, child: _materialDialogActionText(text));
28+
case TargetPlatform.iOS:
29+
case TargetPlatform.macOS:
30+
return CupertinoDialogAction(onPressed: onPressed, child: Text(text));
31+
}
32+
}
33+
1834
/// Tracks the status of a dialog, in being still open or already closed.
1935
///
2036
/// See also:
@@ -42,13 +58,13 @@ DialogStatus showErrorDialog({
4258
final zulipLocalizations = ZulipLocalizations.of(context);
4359
final future = showDialog<void>(
4460
context: context,
45-
builder: (BuildContext context) => AlertDialog(
61+
builder: (BuildContext context) => AlertDialog.adaptive(
4662
title: Text(title),
4763
content: message != null ? SingleChildScrollView(child: Text(message)) : null,
4864
actions: [
49-
TextButton(
65+
_adaptiveAction(
5066
onPressed: () => Navigator.pop(context),
51-
child: _dialogActionText(zulipLocalizations.errorDialogContinue)),
67+
text: zulipLocalizations.errorDialogContinue),
5268
]));
5369
return DialogStatus(future);
5470
}
@@ -63,15 +79,15 @@ void showSuggestedActionDialog({
6379
final zulipLocalizations = ZulipLocalizations.of(context);
6480
showDialog<void>(
6581
context: context,
66-
builder: (BuildContext context) => AlertDialog(
82+
builder: (BuildContext context) => AlertDialog.adaptive(
6783
title: Text(title),
6884
content: SingleChildScrollView(child: Text(message)),
6985
actions: [
70-
TextButton(
86+
_adaptiveAction(
7187
onPressed: () => Navigator.pop(context),
72-
child: _dialogActionText(zulipLocalizations.dialogCancel)),
73-
TextButton(
88+
text: zulipLocalizations.dialogCancel),
89+
_adaptiveAction(
7490
onPressed: onActionButtonPress,
75-
child: _dialogActionText(actionButtonText ?? zulipLocalizations.dialogContinue)),
91+
text: actionButtonText ?? zulipLocalizations.dialogContinue),
7692
]));
7793
}

test/widgets/dialog_checks.dart

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/foundation.dart';
13
import 'package:flutter/material.dart';
24
import 'package:flutter_test/flutter_test.dart';
35

@@ -6,21 +8,40 @@ import 'package:flutter_test/flutter_test.dart';
68
/// Checks for an error dialog matching an expected title
79
/// and, optionally, matching an expected message. Fails if none is found.
810
///
9-
/// On success, returns the widget's "OK" button.
11+
/// On success, returns the widget's "OK" button
12+
///
1013
/// Dismiss the dialog by calling `tester.tap(find.byWidget(okButton))`.
1114
Widget checkErrorDialog(WidgetTester tester, {
1215
required String expectedTitle,
1316
String? expectedMessage,
1417
}) {
15-
final dialog = tester.widget<AlertDialog>(find.byType(AlertDialog));
16-
tester.widget(find.descendant(matchRoot: true,
17-
of: find.byWidget(dialog.title!), matching: find.text(expectedTitle)));
18-
if (expectedMessage != null) {
19-
tester.widget(find.descendant(matchRoot: true,
20-
of: find.byWidget(dialog.content!), matching: find.text(expectedMessage)));
18+
switch (defaultTargetPlatform) {
19+
case TargetPlatform.android:
20+
case TargetPlatform.fuchsia:
21+
case TargetPlatform.linux:
22+
case TargetPlatform.windows: {
23+
final dialog = tester.widget<Dialog>(find.byType(Dialog));
24+
tester.widget(find.widgetWithText(Dialog, expectedTitle));
25+
if (expectedMessage != null) {
26+
tester.widget(find.widgetWithText(Dialog, expectedMessage));
27+
}
28+
return tester.widget(
29+
find.descendant(of: find.byWidget(dialog),
30+
matching: find.widgetWithText(TextButton, 'OK')));
31+
}
32+
case TargetPlatform.iOS:
33+
case TargetPlatform.macOS: {
34+
final dialog = tester.widget<CupertinoAlertDialog>(
35+
find.byType(CupertinoAlertDialog));
36+
tester.widget(find.descendant(matchRoot: true,
37+
of: find.byWidget(dialog.title!), matching: find.text(expectedTitle)));
38+
if (expectedMessage != null) {
39+
tester.widget(find.descendant(matchRoot: true,
40+
of: find.byWidget(dialog.content!), matching: find.text(expectedMessage)));
41+
}
42+
return tester.widget(find.descendant(of: find.byWidget(dialog),
43+
matching: find.widgetWithText(CupertinoDialogAction, 'OK')));
44+
}
2145
}
22-
23-
return tester.widget(
24-
find.descendant(of: find.byWidget(dialog),
25-
matching: find.widgetWithText(TextButton, 'OK')));
46+
2647
}

0 commit comments

Comments
 (0)