|
| 1 | +import 'dart:developer'; |
1 | 2 | import 'dart:io'; |
2 | 3 |
|
3 | 4 | import 'package:cloud_firestore/cloud_firestore.dart'; |
@@ -88,25 +89,61 @@ class ClearDataSettingsEntryWidget extends ConsumerWidget { |
88 | 89 | if (!context.mounted) { |
89 | 90 | return; |
90 | 91 | } |
91 | | - await showDialog( |
92 | | - context: context, |
93 | | - builder: (context) => AlertDialog( |
94 | | - title: Text(translations.settings.dangerZone.clearData.doneDialog.title), |
95 | | - content: Text(translations.settings.dangerZone.clearData.doneDialog.message), |
96 | | - scrollable: true, |
97 | | - actions: [ |
| 92 | + await _showCloseDialog(context); |
| 93 | + if (_canExitWithConfirmDialog) { |
| 94 | + await _closeApp(); |
| 95 | + } |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + /// Shows the dialog that indicates the user he has to restart the app. |
| 100 | + Future<void> _showCloseDialog(BuildContext context) async { |
| 101 | + bool canExitWithConfirmDialog = _canExitWithConfirmDialog; |
| 102 | + await showDialog( |
| 103 | + context: context, |
| 104 | + barrierDismissible: false, |
| 105 | + builder: (context) => AlertDialog( |
| 106 | + title: Text(translations.settings.dangerZone.clearData.doneDialog.title), |
| 107 | + content: Text( |
| 108 | + canExitWithConfirmDialog ? translations.settings.dangerZone.clearData.doneDialog.message.appWillClose : translations.settings.dangerZone.clearData.doneDialog.message.closeAppManually, |
| 109 | + ), |
| 110 | + scrollable: true, |
| 111 | + actions: canExitWithConfirmDialog |
| 112 | + ? [ |
98 | 113 | TextButton( |
99 | 114 | onPressed: () => Navigator.pop(context), |
100 | 115 | child: Text(MaterialLocalizations.of(context).continueButtonLabel), |
101 | 116 | ), |
102 | | - ], |
103 | | - ), |
104 | | - ); |
105 | | - if (currentPlatform.isMobile) { |
106 | | - SystemChannels.platform.invokeMethod('SystemNavigator.pop', true); |
107 | | - } else { |
108 | | - exit(0); |
109 | | - } |
110 | | - }, |
111 | | - ); |
| 117 | + ] |
| 118 | + : null, |
| 119 | + ), |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /// Whether we can exit following the [_showCloseDialog] method. |
| 124 | + bool get _canExitWithConfirmDialog { |
| 125 | + switch (currentPlatform) { |
| 126 | + case Platform.android: |
| 127 | + case Platform.macOS: |
| 128 | + case Platform.linux: |
| 129 | + case Platform.windows: |
| 130 | + return true; |
| 131 | + default: |
| 132 | + return false; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// Closes the app programmatically. |
| 137 | + Future<void> _closeApp() async { |
| 138 | + switch (currentPlatform) { |
| 139 | + case Platform.android: |
| 140 | + case Platform.macOS: |
| 141 | + case Platform.linux: |
| 142 | + await SystemNavigator.pop(animated: true); |
| 143 | + break; |
| 144 | + default: |
| 145 | + debugger(); |
| 146 | + exit(0); |
| 147 | + } |
| 148 | + } |
112 | 149 | } |
0 commit comments