Frontend first draft
This commit is contained in:
29
frontend/pweb/lib/widgets/error/content.dart
Normal file
29
frontend/pweb/lib/widgets/error/content.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pweb/widgets/vspacer.dart';
|
||||
|
||||
|
||||
class ErrorSnackBarContent extends StatelessWidget {
|
||||
final String situation;
|
||||
final String localizedError;
|
||||
|
||||
const ErrorSnackBarContent({
|
||||
super.key,
|
||||
required this.situation,
|
||||
required this.localizedError,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Column(
|
||||
mainAxisSize: MainAxisSize.min, // wrap to content
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
situation,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const VSpacer(multiplier: 0.25),
|
||||
Text(localizedError),
|
||||
],
|
||||
);
|
||||
}
|
||||
132
frontend/pweb/lib/widgets/error/snackbar.dart
Normal file
132
frontend/pweb/lib/widgets/error/snackbar.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pweb/utils/error_handler.dart';
|
||||
import 'package:pweb/widgets/error/content.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
|
||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUserOfErrorX({
|
||||
required ScaffoldMessengerState scaffoldMessenger,
|
||||
required String errorSituation,
|
||||
required Object exception,
|
||||
required AppLocalizations appLocalizations,
|
||||
int delaySeconds = 3,
|
||||
}) {
|
||||
// A. Localized user-friendly error message
|
||||
final String localizedError = ErrorHandler.handleErrorLocs(appLocalizations, exception);
|
||||
|
||||
// B. Technical details for advanced reference
|
||||
final String technicalDetails = exception.toString();
|
||||
|
||||
// C. Build the snack bar
|
||||
final snackBar = _buildMainErrorSnackBar(
|
||||
errorSituation: errorSituation,
|
||||
localizedError: localizedError,
|
||||
technicalDetails: technicalDetails,
|
||||
loc: appLocalizations,
|
||||
scaffoldMessenger: scaffoldMessenger,
|
||||
delaySeconds: delaySeconds,
|
||||
);
|
||||
|
||||
// D. Show it
|
||||
return scaffoldMessenger.showSnackBar(snackBar);
|
||||
}
|
||||
|
||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUserOfError({
|
||||
required BuildContext context,
|
||||
required String errorSituation,
|
||||
required Object exception,
|
||||
int delaySeconds = 3,
|
||||
}) => notifyUserOfErrorX(
|
||||
scaffoldMessenger: ScaffoldMessenger.of(context),
|
||||
errorSituation: errorSituation,
|
||||
exception: exception,
|
||||
appLocalizations: AppLocalizations.of(context)!,
|
||||
delaySeconds: delaySeconds,
|
||||
);
|
||||
|
||||
Future<T?> executeActionWithNotification<T>({
|
||||
required BuildContext context,
|
||||
required Future<T> Function() action,
|
||||
required String errorMessage,
|
||||
int delaySeconds = 3,
|
||||
}) async {
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
final localizations = AppLocalizations.of(context)!;
|
||||
|
||||
try {
|
||||
return await action();
|
||||
} catch (e) {
|
||||
// Report the error using your existing notifier.
|
||||
notifyUserOfErrorX(
|
||||
scaffoldMessenger: scaffoldMessenger,
|
||||
errorSituation: errorMessage,
|
||||
exception: e,
|
||||
appLocalizations: localizations,
|
||||
delaySeconds: delaySeconds,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> postNotifyUserOfError({
|
||||
required ScaffoldMessengerState scaffoldMessenger,
|
||||
required String errorSituation,
|
||||
required Object exception,
|
||||
required AppLocalizations appLocalizations,
|
||||
int delaySeconds = 3,
|
||||
}) {
|
||||
|
||||
final completer = Completer<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>>();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => completer.complete(notifyUserOfErrorX(
|
||||
scaffoldMessenger: scaffoldMessenger,
|
||||
errorSituation: errorSituation,
|
||||
exception: exception,
|
||||
appLocalizations: appLocalizations,
|
||||
delaySeconds: delaySeconds,
|
||||
)),
|
||||
);
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
Future<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> postNotifyUserOfErrorX({
|
||||
required BuildContext context,
|
||||
required String errorSituation,
|
||||
required Object exception,
|
||||
int delaySeconds = 3,
|
||||
}) => postNotifyUserOfError(
|
||||
scaffoldMessenger: ScaffoldMessenger.of(context),
|
||||
errorSituation: errorSituation,
|
||||
exception: exception,
|
||||
appLocalizations: AppLocalizations.of(context)!,
|
||||
delaySeconds: delaySeconds,
|
||||
);
|
||||
|
||||
|
||||
/// 2) A helper function that returns the main SnackBar widget
|
||||
SnackBar _buildMainErrorSnackBar({
|
||||
required String errorSituation,
|
||||
required String localizedError,
|
||||
required String technicalDetails,
|
||||
required AppLocalizations loc,
|
||||
required ScaffoldMessengerState scaffoldMessenger,
|
||||
int delaySeconds = 3,
|
||||
}) => SnackBar(
|
||||
duration: Duration(seconds: delaySeconds),
|
||||
content: ErrorSnackBarContent(
|
||||
situation: errorSituation,
|
||||
localizedError: localizedError,
|
||||
),
|
||||
action: SnackBarAction(
|
||||
label: loc.showDetailsAction,
|
||||
onPressed: () => scaffoldMessenger.showSnackBar(SnackBar(
|
||||
content: Text(technicalDetails),
|
||||
duration: const Duration(seconds: 6),
|
||||
)),
|
||||
),
|
||||
);
|
||||
Reference in New Issue
Block a user