fix snackbar showing also showing on other screens (#155)
This commit is contained in:
@@ -83,12 +83,13 @@ class _GroupDetailViewState extends State<GroupDetailView> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
).then((confirmed) async {
|
).then((confirmed) async {
|
||||||
if (confirmed == true && context.mounted) {
|
if (confirmed == true && mounted) {
|
||||||
bool success = await db.groupDao.deleteGroup(groupId: widget.groupToEdit!.id);
|
bool success = await db.groupDao.deleteGroup(groupId: widget.groupToEdit!.id);
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
if (success) {
|
if (success) {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
} else {
|
} else {
|
||||||
|
if (!mounted) return;
|
||||||
showSnackbar(message: loc.error_deleting_group);
|
showSnackbar(message: loc.error_deleting_group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class SettingsView extends StatefulWidget {
|
|||||||
|
|
||||||
class _SettingsViewState extends State<SettingsView> {
|
class _SettingsViewState extends State<SettingsView> {
|
||||||
|
|
||||||
/// GlobalKey for ScaffoldMessenger to show snackbars
|
/// GlobalKey for ScaffoldMessenger to show snackbars only on this screen
|
||||||
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
||||||
|
|
||||||
PackageInfo _packageInfo = PackageInfo(
|
PackageInfo _packageInfo = PackageInfo(
|
||||||
@@ -84,8 +84,8 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
json,
|
json,
|
||||||
'game_tracker-data',
|
'game_tracker-data',
|
||||||
);
|
);
|
||||||
if (!context.mounted) return;
|
if (!mounted) return;
|
||||||
showExportSnackBar(context: context, result: result);
|
showExportSnackBar(result: result);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SettingsListTile(
|
SettingsListTile(
|
||||||
@@ -94,8 +94,8 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final result = await DataTransferService.importData(context);
|
final result = await DataTransferService.importData(context);
|
||||||
if (!context.mounted) return;
|
if (!mounted) return;
|
||||||
showImportSnackBar(context: context, result: result);
|
showImportSnackBar(result: result);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SettingsListTile(
|
SettingsListTile(
|
||||||
@@ -123,7 +123,6 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
if (confirmed == true && context.mounted) {
|
if (confirmed == true && context.mounted) {
|
||||||
DataTransferService.deleteAllData(context);
|
DataTransferService.deleteAllData(context);
|
||||||
showSnackbar(
|
showSnackbar(
|
||||||
context: context,
|
|
||||||
message: AppLocalizations.of(
|
message: AppLocalizations.of(
|
||||||
context,
|
context,
|
||||||
).data_successfully_deleted,
|
).data_successfully_deleted,
|
||||||
@@ -237,62 +236,56 @@ class _SettingsViewState extends State<SettingsView> {
|
|||||||
|
|
||||||
/// Displays a snackbar based on the import result.
|
/// Displays a snackbar based on the import result.
|
||||||
///
|
///
|
||||||
/// [context] The BuildContext to show the snackbar in.
|
|
||||||
/// [result] The result of the import operation.
|
/// [result] The result of the import operation.
|
||||||
void showImportSnackBar({
|
void showImportSnackBar({
|
||||||
required BuildContext context,
|
|
||||||
required ImportResult result,
|
required ImportResult result,
|
||||||
}) {
|
}) {
|
||||||
final loc = AppLocalizations.of(context);
|
final loc = AppLocalizations.of(context);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case ImportResult.success:
|
case ImportResult.success:
|
||||||
showSnackbar(context: context, message: loc.data_successfully_imported);
|
showSnackbar(message: loc.data_successfully_imported);
|
||||||
case ImportResult.invalidSchema:
|
case ImportResult.invalidSchema:
|
||||||
showSnackbar(context: context, message: loc.invalid_schema);
|
showSnackbar(message: loc.invalid_schema);
|
||||||
case ImportResult.fileReadError:
|
case ImportResult.fileReadError:
|
||||||
showSnackbar(context: context, message: loc.error_reading_file);
|
showSnackbar(message: loc.error_reading_file);
|
||||||
case ImportResult.canceled:
|
case ImportResult.canceled:
|
||||||
showSnackbar(context: context, message: loc.import_canceled);
|
showSnackbar(message: loc.import_canceled);
|
||||||
case ImportResult.formatException:
|
case ImportResult.formatException:
|
||||||
showSnackbar(context: context, message: loc.format_exception);
|
showSnackbar(message: loc.format_exception);
|
||||||
case ImportResult.unknownException:
|
case ImportResult.unknownException:
|
||||||
showSnackbar(context: context, message: loc.unknown_exception);
|
showSnackbar(message: loc.unknown_exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Displays a snackbar based on the export result.
|
/// Displays a snackbar based on the export result.
|
||||||
///
|
///
|
||||||
/// [context] The BuildContext to show the snackbar in.
|
|
||||||
/// [result] The result of the export operation.
|
/// [result] The result of the export operation.
|
||||||
void showExportSnackBar({
|
void showExportSnackBar({
|
||||||
required BuildContext context,
|
|
||||||
required ExportResult result,
|
required ExportResult result,
|
||||||
}) {
|
}) {
|
||||||
final loc = AppLocalizations.of(context);
|
final loc = AppLocalizations.of(context);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case ExportResult.success:
|
case ExportResult.success:
|
||||||
showSnackbar(context: context, message: loc.data_successfully_exported);
|
showSnackbar(message: loc.data_successfully_exported);
|
||||||
case ExportResult.canceled:
|
case ExportResult.canceled:
|
||||||
showSnackbar(context: context, message: loc.export_canceled);
|
showSnackbar(message: loc.export_canceled);
|
||||||
case ExportResult.unknownException:
|
case ExportResult.unknownException:
|
||||||
showSnackbar(context: context, message: loc.unknown_exception);
|
showSnackbar(message: loc.unknown_exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Displays a snackbar with the given message and optional action.
|
/// Displays a snackbar with the given message and optional action.
|
||||||
///
|
///
|
||||||
/// [context] The BuildContext to show the snackbar in.
|
|
||||||
/// [message] The message to display in the snackbar.
|
/// [message] The message to display in the snackbar.
|
||||||
/// [duration] The duration for which the snackbar is displayed.
|
/// [duration] The duration for which the snackbar is displayed.
|
||||||
/// [action] An optional callback function to execute when the action button is pressed.
|
/// [action] An optional callback function to execute when the action button is pressed.
|
||||||
void showSnackbar({
|
void showSnackbar({
|
||||||
required BuildContext context,
|
|
||||||
required String message,
|
required String message,
|
||||||
Duration duration = const Duration(seconds: 3),
|
Duration duration = const Duration(seconds: 3),
|
||||||
VoidCallback? action,
|
VoidCallback? action,
|
||||||
}) {
|
}) {
|
||||||
final loc = AppLocalizations.of(context);
|
final loc = AppLocalizations.of(context);
|
||||||
final messenger = ScaffoldMessenger.of(context);
|
final messenger = _scaffoldMessengerKey.currentState!;
|
||||||
messenger.hideCurrentSnackBar();
|
messenger.hideCurrentSnackBar();
|
||||||
messenger.showSnackBar(
|
messenger.showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
|
|||||||
Reference in New Issue
Block a user