Skip to content

Commit

Permalink
refactor(#646): use adaptive dialog wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Dec 20, 2023
1 parent 773703a commit 87fc076
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/lib/common/utilities/drug_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ Future<void> updateCachedDrugs() async {
// ignore: use_build_context_synchronously
await showAdaptiveDialog(
context: context,
builder: (context) => AlertDialog.adaptive(
title: Text(context.l10n.update_warning_title),
builder: (context) => AdaptiveDialogWrapper(
title: context.l10n.update_warning_title,
content: Text(context.l10n.update_warning_body),
actions: [
TextButton(
DialogAction(
onPressed: () => Navigator.pop(context),
child: Text(context.l10n.action_continue),
text: context.l10n.action_continue,
),
],
),
Expand Down
66 changes: 66 additions & 0 deletions app/lib/common/widgets/adaptive_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/cupertino.dart';

import '../module.dart';

class AdaptiveDialogWrapper extends StatelessWidget {
const AdaptiveDialogWrapper({
super.key,
required this.title,
required this.content,
required this.actions,
});

final String title;
final Widget? content;
final List<DialogAction> actions;

@override
Widget build(BuildContext context) {
final materialContent = getPlatform() == SupportedPlatform.ios
? Card(
color: Colors.transparent,
elevation: 0,
child: content,
)
: content;
return AlertDialog.adaptive(
title: Text(title),
content: materialContent,
actions: actions,
);
}
}

class DialogAction extends StatelessWidget {
const DialogAction({
super.key,
this.isDestructive = false,
this.onPressed,
required this.text,
});

final bool isDestructive;
final void Function()? onPressed;
final String text;

@override
Widget build(BuildContext context) {
switch (getPlatform()) {
case SupportedPlatform.ios:
return CupertinoDialogAction(
isDestructiveAction: isDestructive,
onPressed: onPressed,
child: Text(text),
);
default:
return TextButton(
onPressed: onPressed,
child: Text(text, style: onPressed != null
? isDestructive
? TextStyle(color: PharMeTheme.errorColor)
: TextStyle(color: PharMeTheme.primaryColor)
: TextStyle(color: PharMeTheme.onSurfaceColor)),
);
}
}
}
1 change: 1 addition & 0 deletions app/lib/common/widgets/module.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'adaptive_dialog.dart';
export 'context_menu.dart';
export 'drug_list/builder.dart';
export 'drug_list/cubit.dart';
Expand Down
16 changes: 7 additions & 9 deletions app/lib/drug/widgets/annotation_cards/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,21 @@ class DrugAnnotationCard extends StatelessWidget {
if (isInhibitor(drug.name)) {
showAdaptiveDialog(
context: context,
builder: (context) => AlertDialog.adaptive(
title: Text(context.l10n.drugs_page_active_warn_header),
builder: (context) => AdaptiveDialogWrapper(
title: context.l10n.drugs_page_active_warn_header,
content: Text(context.l10n.drugs_page_active_warn),
actions: [
TextButton(
DialogAction(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: Text(context.l10n.action_cancel),
text: context.l10n.action_cancel,
),
TextButton(
DialogAction(
onPressed: () {
Navigator.pop(context, 'OK');
setActivity(value: newValue);
},
child: Text(
context.l10n.action_continue,
style: TextStyle(color: PharMeTheme.errorColor),
),
text: context.l10n.action_continue,
isDestructive: true,
),
],
),
Expand Down
19 changes: 9 additions & 10 deletions app/lib/settings/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class DeleteDataDialog extends HookWidget {
Widget build(BuildContext context) {
final agreedToDeletion = useState(false);

return AlertDialog.adaptive(
title: Text(context.l10n.settings_page_delete_data),
return AdaptiveDialogWrapper(
title: context.l10n.settings_page_delete_data,
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Expand All @@ -91,24 +91,23 @@ class DeleteDataDialog extends HookWidget {
contentPadding: EdgeInsets.zero,
activeColor: PharMeTheme.primaryColor,
),
]),
],
),
actions: [
TextButton(
DialogAction(
onPressed: context.router.root.pop,
child: Text(context.l10n.action_cancel),
text: context.l10n.action_cancel,
),
TextButton(
DialogAction(
onPressed: agreedToDeletion.value
? () async {
await deleteAllAppData();
// ignore: use_build_context_synchronously
await overwriteRoutes(context, nextPage: LoginRoute());
}
: null,
child: Text(
context.l10n.action_continue,
style: TextStyle(color: PharMeTheme.errorColor),
),
text: context.l10n.action_continue,
isDestructive: true,
),
],
);
Expand Down

0 comments on commit 87fc076

Please sign in to comment.