Skip to content

Commit 5d20281

Browse files
committed
Reviewed changes: code and code style ammended to comply with best practices
switch statements tightened, appropriate text widget for adative action, use defaultTargetPlatform instead of context, comments fixed
1 parent af8d513 commit 5d20281

File tree

3 files changed

+42
-55
lines changed

3 files changed

+42
-55
lines changed

lib/widgets/dialog.dart

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'package:flutter/cupertino.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
4+
import 'package:flutter/foundation.dart';
45

5-
Widget _dialogActionText(String text) {
6+
Widget _materialDialogActionText(String text) {
67
return Text(
78
text,
89

@@ -16,29 +17,19 @@ Widget _dialogActionText(String text) {
1617
);
1718
}
1819

19-
/// Sets the dialog action to be platform appropriate
20-
/// by displaying a [CupertinoDialogAction] for IOS platforms
21-
/// and a regular [TextButton] otherwise.
22-
Widget _adaptiveAction(
23-
{required BuildContext context,
24-
required VoidCallback onPressed,
25-
required Widget child}) {
26-
final ThemeData theme = Theme.of(context);
27-
switch (theme.platform) {
28-
case TargetPlatform.android:
29-
return TextButton(onPressed: onPressed, child: child);
30-
case TargetPlatform.fuchsia:
31-
return TextButton(onPressed: onPressed, child: child);
32-
case TargetPlatform.linux:
33-
return TextButton(onPressed: onPressed, child: child);
34-
case TargetPlatform.windows:
35-
return TextButton(onPressed: onPressed, child: child);
36-
case TargetPlatform.iOS:
37-
return CupertinoDialogAction(onPressed: onPressed, child: child);
38-
case TargetPlatform.macOS:
39-
return CupertinoDialogAction(onPressed: onPressed, child: child);
40-
}
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));
4131
}
32+
}
4233

4334
/// Tracks the status of a dialog, in being still open or already closed.
4435
///
@@ -72,9 +63,8 @@ DialogStatus showErrorDialog({
7263
content: message != null ? SingleChildScrollView(child: Text(message)) : null,
7364
actions: [
7465
_adaptiveAction(
75-
context: context,
7666
onPressed: () => Navigator.pop(context),
77-
child: _dialogActionText(zulipLocalizations.errorDialogContinue)),
67+
text: zulipLocalizations.errorDialogContinue),
7868
]));
7969
return DialogStatus(future);
8070
}
@@ -94,12 +84,10 @@ void showSuggestedActionDialog({
9484
content: SingleChildScrollView(child: Text(message)),
9585
actions: [
9686
_adaptiveAction(
97-
context: context,
9887
onPressed: () => Navigator.pop(context),
99-
child: _dialogActionText(zulipLocalizations.dialogCancel)),
88+
text: zulipLocalizations.dialogCancel),
10089
_adaptiveAction(
101-
context: context,
10290
onPressed: onActionButtonPress,
103-
child: _dialogActionText(actionButtonText ?? zulipLocalizations.dialogContinue)),
91+
text: actionButtonText ?? zulipLocalizations.dialogContinue),
10492
]));
10593
}

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ packages:
315315
dependency: transitive
316316
description:
317317
name: file
318-
sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
318+
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
319319
url: "https://pub.dev"
320320
source: hosted
321321
version: "7.0.1"

test/widgets/dialog_checks.dart

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,39 @@ import 'package:flutter_test/flutter_test.dart';
99
/// and, optionally, matching an expected message. Fails if none is found.
1010
///
1111
/// On success, returns the widget's "OK" button
12-
/// (which is a [CupertinoDialogAction] for OS platforms).
1312
///
1413
/// Dismiss the dialog by calling `tester.tap(find.byWidget(okButton))`.
1514
Widget checkErrorDialog(WidgetTester tester, {
1615
required String expectedTitle,
1716
String? expectedMessage,
1817
}) {
19-
if (defaultTargetPlatform == TargetPlatform.iOS
20-
|| defaultTargetPlatform == TargetPlatform.macOS) {
21-
22-
final dialog = tester.widget<CupertinoAlertDialog>(find.byType(CupertinoAlertDialog));
23-
24-
tester.widget(find.descendant(matchRoot: true,
25-
of: find.byWidget(dialog.title!), matching: find.text(expectedTitle)));
26-
27-
if (expectedMessage != null) {
28-
tester.widget(find.descendant(matchRoot: true,
29-
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')));
3031
}
31-
32-
return tester.widget(
33-
find.descendant(of: find.byWidget(dialog),
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),
3443
matching: find.widgetWithText(CupertinoDialogAction, 'OK')));
35-
36-
}
37-
else {
38-
final dialog = tester.widget<Dialog>(find.byType(Dialog));
39-
tester.widget(find.widgetWithText(Dialog, expectedTitle));
40-
if (expectedMessage != null) {
41-
tester.widget(find.widgetWithText(Dialog, expectedMessage));
4244
}
43-
return tester.widget(
44-
find.descendant(of: find.byWidget(dialog),
45-
matching: find.widgetWithText(TextButton, 'OK')));
4645
}
4746

4847
}

0 commit comments

Comments
 (0)