added frontend
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import 'dart:convert' show jsonDecode, jsonEncode;
|
||||
|
||||
import 'package:fotodocumentation/dto/base_dto.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/http_client_utils.dart';
|
||||
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
|
||||
import 'package:fotodocumentation/utils/url_utils.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/http.dart' show Response;
|
||||
|
||||
import 'package:fotodocumentation/main.dart' show logger;
|
||||
|
||||
abstract class BaseController {
|
||||
UrlUtils get uriUtils => DiContainer.get();
|
||||
JwtTokenStorage get _jwtTokenStorage => DiContainer.get();
|
||||
HttpClientUtils get httpClientUtils => DiContainer.get();
|
||||
|
||||
Future<Header> getAuthHeader() async {
|
||||
final accessToken = await _jwtTokenStorage.getAccessToken();
|
||||
|
||||
if (accessToken != null && accessToken.isNotEmpty) {
|
||||
// Use JWT Bearer token
|
||||
return Header('Authorization', 'Bearer $accessToken');
|
||||
} else {
|
||||
return const Header("Accept-Language", "en-US");
|
||||
}
|
||||
}
|
||||
|
||||
Exception getServerError(Response response) {
|
||||
return Exception("Error receiving data from server");
|
||||
}
|
||||
|
||||
Future<List<T>> runGetListWithAuth<T>(String uriStr, List<T> Function(dynamic) convert) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
try {
|
||||
Header cred = await getAuthHeader();
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.get(uri, headers: {cred.name: cred.value});
|
||||
if (response.statusCode == 200) {
|
||||
String text = response.body;
|
||||
var jsonArray = jsonDecode(text);
|
||||
return convert(jsonArray);
|
||||
} else {
|
||||
throw ServerError(response.statusCode); // Exception("Failed to get server data ${response.statusCode}");
|
||||
}
|
||||
} catch (e) {
|
||||
logger.e("exception $e");
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<T?> runGetWithAuth<T>(String uriStr, T Function(dynamic) convert) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
try {
|
||||
Header cred = await getAuthHeader();
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.get(uri, headers: {cred.name: cred.value});
|
||||
if (response.statusCode == 200) {
|
||||
String text = response.body;
|
||||
var jsonArray = jsonDecode(text);
|
||||
return convert(jsonArray);
|
||||
} else {
|
||||
throw ServerError(response.statusCode); // Exception("Failed to get server data ${response.statusCode}");
|
||||
}
|
||||
} catch (e) {
|
||||
logger.e("exception $e");
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> runDeleteWithAuth(String uriStr) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
Header cred = await getAuthHeader();
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.delete(uri, headers: {cred.name: cred.value});
|
||||
return response.statusCode == 200;
|
||||
}
|
||||
|
||||
Future<bool> runPutWithAuth(String uriStr) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
Header cred = await getAuthHeader();
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.put(uri, headers: {cred.name: cred.value});
|
||||
return response.statusCode == 200;
|
||||
}
|
||||
|
||||
Future<ServerReply<T>> runSaveNew<T extends DtoMapAble>(String uriStr, T dtoObj, Function(http.Response response, T dto) processReply) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
try {
|
||||
Header cred = await getAuthHeader();
|
||||
String body = jsonEncode(dtoObj.toMap());
|
||||
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.post(uri, headers: {cred.name: cred.value, "Accept": "application/json", "Content-Type": "application/json"}, body: body);
|
||||
return processReply(response, dtoObj);
|
||||
} catch (e) {
|
||||
logger.e("exception $e");
|
||||
}
|
||||
return ServerReply(ServerState.error, dtoObj);
|
||||
}
|
||||
|
||||
Future<ServerReply<T>> runSaveUpdate<T extends DtoMapAble>(String uriStr, T dtoObj, Function(http.Response response, T dto) processReply) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
try {
|
||||
Header cred = await getAuthHeader();
|
||||
String body = jsonEncode(dtoObj.toMap());
|
||||
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
var response = await client.put(uri, headers: {cred.name: cred.value, "Accept": "application/json", "Content-Type": "application/json"}, body: body);
|
||||
|
||||
return processReply(response, dtoObj);
|
||||
} catch (e) {
|
||||
logger.e("exception $e");
|
||||
}
|
||||
return ServerReply(ServerState.error, dtoObj);
|
||||
}
|
||||
|
||||
ServerReply<T> processServerResponse<T>(Response response, T dto, T Function(Map<String, dynamic> json) fromJson) {
|
||||
if (response.statusCode == 200) {
|
||||
String text = response.body;
|
||||
var json = jsonDecode(text);
|
||||
var dto = fromJson(json);
|
||||
return ServerReply(ServerState.ok, dto);
|
||||
} else if (response.statusCode == 400 || response.statusCode == 409) {
|
||||
String text = response.body;
|
||||
try {
|
||||
var json = jsonDecode(text);
|
||||
var error = ErrorDto.fromJson(json);
|
||||
|
||||
return ServerReply(ServerState.duplicate, dto, error: error);
|
||||
} catch (e) {
|
||||
return ServerReply(ServerState.error, dto, error: ErrorDto(response.statusCode, text));
|
||||
}
|
||||
} else if (response.statusCode == 403) {
|
||||
var error = ErrorDto(403, "Not allowed.");
|
||||
return ServerReply(ServerState.error, dto, error: error);
|
||||
}
|
||||
return ServerReply(ServerState.error, dto, error: ErrorDto(response.statusCode, "Internal server error"));
|
||||
}
|
||||
}
|
||||
|
||||
class Header {
|
||||
final String name;
|
||||
final String value;
|
||||
|
||||
const Header(this.name, this.value);
|
||||
|
||||
@override
|
||||
String toString() => '$runtimeType: $name, $value';
|
||||
}
|
||||
|
||||
class ServerReply<T> {
|
||||
ServerState state;
|
||||
T entity;
|
||||
ErrorDto? error;
|
||||
|
||||
ServerReply(this.state, this.entity, {this.error});
|
||||
|
||||
@override
|
||||
String toString() => '$runtimeType: $state, $entity, $error';
|
||||
}
|
||||
|
||||
class ServerError {
|
||||
int statusCode;
|
||||
|
||||
ServerError(this.statusCode);
|
||||
}
|
||||
|
||||
enum ServerState {
|
||||
ok,
|
||||
duplicate,
|
||||
error,
|
||||
;
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import 'dart:convert' show base64, utf8;
|
||||
import 'dart:convert' show jsonDecode, jsonEncode;
|
||||
|
||||
import 'package:fotodocumentation/controller/base_controller.dart';
|
||||
import 'package:fotodocumentation/dto/jwt_token_pair_dto.dart';
|
||||
import 'package:fotodocumentation/main.dart' show logger;
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
typedef AuthenticateReply = ({JwtTokenPairDto? jwtTokenPairDto});
|
||||
|
||||
abstract interface class LoginController {
|
||||
Future<AuthenticateReply> authenticate(String username, String password);
|
||||
Future<bool> refreshAccessToken();
|
||||
Future<bool> isUsingJwtAuth();
|
||||
}
|
||||
|
||||
class LoginControllerImpl extends BaseController implements LoginController {
|
||||
final String path = "login";
|
||||
|
||||
JwtTokenStorage get _jwtTokenStorage => DiContainer.get();
|
||||
|
||||
@override
|
||||
Future<AuthenticateReply> authenticate(String username, String password) async {
|
||||
http.Client client = httpClientUtils.client;
|
||||
try {
|
||||
Header cred = _getLoginHeader(username, password);
|
||||
String uriStr = '${uriUtils.getBaseUrl()}$path';
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
|
||||
var response = await client.get(uri, headers: {cred.name: cred.value});
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> data = Map.castFrom(jsonDecode(response.body));
|
||||
|
||||
final tokenPair = JwtTokenPairDto.fromJson(data);
|
||||
|
||||
// Store tokens securely
|
||||
await _jwtTokenStorage.saveTokens(tokenPair.accessToken, tokenPair.refreshToken);
|
||||
|
||||
// Load user data using the new token
|
||||
return (jwtTokenPairDto: tokenPair);
|
||||
} else {
|
||||
logger.e('Authentication failed: ${response.statusCode} ${response.body}');
|
||||
return (jwtTokenPairDto: null);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.e("Authentication error: $e");
|
||||
return (jwtTokenPairDto: null);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> refreshAccessToken() async {
|
||||
try {
|
||||
final refreshToken = await _jwtTokenStorage.getRefreshToken();
|
||||
if (refreshToken == null) {
|
||||
logger.i('No refresh token available');
|
||||
return false;
|
||||
}
|
||||
String uriStr = '${uriUtils.getBaseUrl()}$path/login/refresh';
|
||||
Uri uri = Uri.parse(uriStr);
|
||||
|
||||
final response = await http.post(
|
||||
uri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
'refreshToken': refreshToken,
|
||||
}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
final newAccessToken = data['accessToken'] as String;
|
||||
|
||||
// Update only the access token (keep same refresh token)
|
||||
await _jwtTokenStorage.updateAccessToken(newAccessToken);
|
||||
|
||||
logger.d('Access token refreshed successfully');
|
||||
return true;
|
||||
} else {
|
||||
logger.d('Token refresh failed: ${response.statusCode} ${response.body}');
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
logger.e('Token refresh error: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isUsingJwtAuth() async {
|
||||
return await _jwtTokenStorage.hasTokens();
|
||||
}
|
||||
|
||||
Header _getLoginHeader(String username, String password) {
|
||||
String combined = "$username:$password";
|
||||
final bytes = utf8.encode(combined);
|
||||
String asBase64 = base64.encode(bytes);
|
||||
return Header("Authorization", "Basic $asBase64");
|
||||
}
|
||||
}
|
||||
15
hartmann-foto-documentation-frontend/lib/dto/base_dto.dart
Normal file
15
hartmann-foto-documentation-frontend/lib/dto/base_dto.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
final class ErrorDto {
|
||||
int error;
|
||||
String message;
|
||||
|
||||
ErrorDto(this.error, this.message);
|
||||
|
||||
ErrorDto.fromJson(Map<String, dynamic> json)
|
||||
: error = json['error'] as int,
|
||||
message = json['message'];
|
||||
}
|
||||
|
||||
abstract interface class DtoMapAble {
|
||||
Map<String, dynamic> toMap();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/// DTO representing a failes login attempt request for 2fa token.
|
||||
class TokenRequiredDto {
|
||||
final bool? tokenRequired;
|
||||
final bool? tokenInValid;
|
||||
|
||||
TokenRequiredDto({
|
||||
required this.tokenRequired,
|
||||
required this.tokenInValid,
|
||||
});
|
||||
|
||||
/// Create from JSON response
|
||||
factory TokenRequiredDto.fromJson(Map<String, dynamic> json) {
|
||||
return TokenRequiredDto(
|
||||
tokenRequired: json['tokenRequired'] as bool,
|
||||
tokenInValid: json['tokenInValid'] as bool,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// DTO representing a pair of JWT tokens from the backend.
|
||||
class JwtTokenPairDto {
|
||||
final String accessToken;
|
||||
final String refreshToken;
|
||||
|
||||
JwtTokenPairDto({
|
||||
required this.accessToken,
|
||||
required this.refreshToken,
|
||||
});
|
||||
|
||||
/// Create from JSON response
|
||||
factory JwtTokenPairDto.fromJson(Map<String, dynamic> json) {
|
||||
return JwtTokenPairDto(
|
||||
accessToken: json['accessToken'] as String,
|
||||
refreshToken: json['refreshToken'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to JSON (for serialization if needed)
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'accessToken': accessToken,
|
||||
'refreshToken': refreshToken,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'JwtTokenPairDto{accessToken: [REDACTED], refreshToken: [REDACTED]}';
|
||||
}
|
||||
}
|
||||
75
hartmann-foto-documentation-frontend/lib/l10n/app_de.arb
Normal file
75
hartmann-foto-documentation-frontend/lib/l10n/app_de.arb
Normal file
@@ -0,0 +1,75 @@
|
||||
{
|
||||
"@@locale": "de",
|
||||
"searchTFHint": "Suchtext",
|
||||
"@searchTFHint": {
|
||||
"description": "Search hint TextField"
|
||||
},
|
||||
"searchButtonLabel": "Suchen",
|
||||
"@searchButtonLabel": {
|
||||
"description": "Search button label"
|
||||
},
|
||||
"loginUsernameTFLabel": "Benutzername",
|
||||
"@loginUsernameTFLabel": {
|
||||
"description": "Usernamt TextField Label"
|
||||
},
|
||||
"loginPasswordTFLabel": "Passwort",
|
||||
"@loginPasswordTFLabel": {
|
||||
"description": "Password TextField Label"
|
||||
},
|
||||
"loginLoginButtonLabel": "Anmelden",
|
||||
"@loginLoginButtonLabel": {
|
||||
"description": "Login Button Label"
|
||||
},
|
||||
"errorWidgetStatusCode": "Statuscode {statusCode}",
|
||||
"@errorWidgetStatusCode": {
|
||||
"description": "Error message showing server status code",
|
||||
"placeholders": {
|
||||
"statusCode": {
|
||||
"type": "int"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"errorWidget": "Fehler: {name}",
|
||||
"@errorWidget": {
|
||||
"description": "Error widget text",
|
||||
"placeholders": {
|
||||
"name": {
|
||||
"type": "String",
|
||||
"example": "Error text"
|
||||
}
|
||||
}
|
||||
},
|
||||
"errorWidgetRetryButton": "Wiederholen",
|
||||
"@errorWidgetRetryButton": {
|
||||
"description": "Retry button text for error widget"
|
||||
},
|
||||
"submitWidget": "Speichern",
|
||||
"@submitWidget": {
|
||||
"description": "Save Button text"
|
||||
},
|
||||
"textInputWidgetValidatorText": "Bitte geben Sie einen Text ein",
|
||||
"@textInputWidgetValidatorText": {
|
||||
"description": "Awaiting result info text"
|
||||
},
|
||||
"waitingWidget": "Warten auf Ergebnis …",
|
||||
"@waitingWidget": {
|
||||
"description": "Awaiting result info text"
|
||||
},
|
||||
"deleteDialogTitle": "Löschen",
|
||||
"@deleteDialogTitle": {
|
||||
"description": "Delete dialog title"
|
||||
},
|
||||
"deleteDialogText": "Sind Sie sicher, dass Sie diese Eintrag löschen möchten?",
|
||||
"@deleteDialogText": {
|
||||
"description": "Delete dialog text"
|
||||
},
|
||||
"deleteDialogButtonCancel": "Nein",
|
||||
"@deleteDialogButtonCancel": {
|
||||
"description": "Cancel Button text"
|
||||
},
|
||||
"deleteDialogButtonApprove": "Ja",
|
||||
"@deleteDialogButtonApprove": {
|
||||
"description": "Approve Button text"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:intl/intl.dart' as intl;
|
||||
|
||||
import 'app_localizations_de.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
|
||||
/// Callers can lookup localized strings with an instance of AppLocalizations
|
||||
/// returned by `AppLocalizations.of(context)`.
|
||||
///
|
||||
/// Applications need to include `AppLocalizations.delegate()` in their app's
|
||||
/// `localizationDelegates` list, and the locales they support in the app's
|
||||
/// `supportedLocales` list. For example:
|
||||
///
|
||||
/// ```dart
|
||||
/// import 'l10n/app_localizations.dart';
|
||||
///
|
||||
/// return MaterialApp(
|
||||
/// localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
/// supportedLocales: AppLocalizations.supportedLocales,
|
||||
/// home: MyApplicationHome(),
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// ## Update pubspec.yaml
|
||||
///
|
||||
/// Please make sure to update your pubspec.yaml to include the following
|
||||
/// packages:
|
||||
///
|
||||
/// ```yaml
|
||||
/// dependencies:
|
||||
/// # Internationalization support.
|
||||
/// flutter_localizations:
|
||||
/// sdk: flutter
|
||||
/// intl: any # Use the pinned version from flutter_localizations
|
||||
///
|
||||
/// # Rest of dependencies
|
||||
/// ```
|
||||
///
|
||||
/// ## iOS Applications
|
||||
///
|
||||
/// iOS applications define key application metadata, including supported
|
||||
/// locales, in an Info.plist file that is built into the application bundle.
|
||||
/// To configure the locales supported by your app, you’ll need to edit this
|
||||
/// file.
|
||||
///
|
||||
/// First, open your project’s ios/Runner.xcworkspace Xcode workspace file.
|
||||
/// Then, in the Project Navigator, open the Info.plist file under the Runner
|
||||
/// project’s Runner folder.
|
||||
///
|
||||
/// Next, select the Information Property List item, select Add Item from the
|
||||
/// Editor menu, then select Localizations from the pop-up menu.
|
||||
///
|
||||
/// Select and expand the newly-created Localizations item then, for each
|
||||
/// locale your application supports, add a new item and select the locale
|
||||
/// you wish to add from the pop-up menu in the Value field. This list should
|
||||
/// be consistent with the languages listed in the AppLocalizations.supportedLocales
|
||||
/// property.
|
||||
abstract class AppLocalizations {
|
||||
AppLocalizations(String locale)
|
||||
: localeName = intl.Intl.canonicalizedLocale(locale.toString());
|
||||
|
||||
final String localeName;
|
||||
|
||||
static AppLocalizations? of(BuildContext context) {
|
||||
return Localizations.of<AppLocalizations>(context, AppLocalizations);
|
||||
}
|
||||
|
||||
static const LocalizationsDelegate<AppLocalizations> delegate =
|
||||
_AppLocalizationsDelegate();
|
||||
|
||||
/// A list of this localizations delegate along with the default localizations
|
||||
/// delegates.
|
||||
///
|
||||
/// Returns a list of localizations delegates containing this delegate along with
|
||||
/// GlobalMaterialLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
|
||||
/// and GlobalWidgetsLocalizations.delegate.
|
||||
///
|
||||
/// Additional delegates can be added by appending to this list in
|
||||
/// MaterialApp. This list does not have to be used at all if a custom list
|
||||
/// of delegates is preferred or required.
|
||||
static const List<LocalizationsDelegate<dynamic>> localizationsDelegates =
|
||||
<LocalizationsDelegate<dynamic>>[
|
||||
delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
];
|
||||
|
||||
/// A list of this localizations delegate's supported locales.
|
||||
static const List<Locale> supportedLocales = <Locale>[Locale('de')];
|
||||
|
||||
/// Search hint TextField
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Suchtext'**
|
||||
String get searchTFHint;
|
||||
|
||||
/// Search button label
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Suchen'**
|
||||
String get searchButtonLabel;
|
||||
|
||||
/// Usernamt TextField Label
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Benutzername'**
|
||||
String get loginUsernameTFLabel;
|
||||
|
||||
/// Password TextField Label
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Passwort'**
|
||||
String get loginPasswordTFLabel;
|
||||
|
||||
/// Login Button Label
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Anmelden'**
|
||||
String get loginLoginButtonLabel;
|
||||
|
||||
/// Error message showing server status code
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Statuscode {statusCode}'**
|
||||
String errorWidgetStatusCode(int statusCode);
|
||||
|
||||
/// Error widget text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Fehler: {name}'**
|
||||
String errorWidget(String name);
|
||||
|
||||
/// Retry button text for error widget
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Wiederholen'**
|
||||
String get errorWidgetRetryButton;
|
||||
|
||||
/// Save Button text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Speichern'**
|
||||
String get submitWidget;
|
||||
|
||||
/// Awaiting result info text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Bitte geben Sie einen Text ein'**
|
||||
String get textInputWidgetValidatorText;
|
||||
|
||||
/// Awaiting result info text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Warten auf Ergebnis …'**
|
||||
String get waitingWidget;
|
||||
|
||||
/// Delete dialog title
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Löschen'**
|
||||
String get deleteDialogTitle;
|
||||
|
||||
/// Delete dialog text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Sind Sie sicher, dass Sie diese Eintrag löschen möchten?'**
|
||||
String get deleteDialogText;
|
||||
|
||||
/// Cancel Button text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Nein'**
|
||||
String get deleteDialogButtonCancel;
|
||||
|
||||
/// Approve Button text
|
||||
///
|
||||
/// In de, this message translates to:
|
||||
/// **'Ja'**
|
||||
String get deleteDialogButtonApprove;
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
extends LocalizationsDelegate<AppLocalizations> {
|
||||
const _AppLocalizationsDelegate();
|
||||
|
||||
@override
|
||||
Future<AppLocalizations> load(Locale locale) {
|
||||
return SynchronousFuture<AppLocalizations>(lookupAppLocalizations(locale));
|
||||
}
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) =>
|
||||
<String>['de'].contains(locale.languageCode);
|
||||
|
||||
@override
|
||||
bool shouldReload(_AppLocalizationsDelegate old) => false;
|
||||
}
|
||||
|
||||
AppLocalizations lookupAppLocalizations(Locale locale) {
|
||||
// Lookup logic when only language code is specified.
|
||||
switch (locale.languageCode) {
|
||||
case 'de':
|
||||
return AppLocalizationsDe();
|
||||
}
|
||||
|
||||
throw FlutterError(
|
||||
'AppLocalizations.delegate failed to load unsupported locale "$locale". This is likely '
|
||||
'an issue with the localizations generation tool. Please file an issue '
|
||||
'on GitHub with a reproducible sample app and the gen-l10n configuration '
|
||||
'that was used.');
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// ignore: unused_import
|
||||
import 'package:intl/intl.dart' as intl;
|
||||
import 'app_localizations.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
|
||||
/// The translations for German (`de`).
|
||||
class AppLocalizationsDe extends AppLocalizations {
|
||||
AppLocalizationsDe([String locale = 'de']) : super(locale);
|
||||
|
||||
@override
|
||||
String get searchTFHint => 'Suchtext';
|
||||
|
||||
@override
|
||||
String get searchButtonLabel => 'Suchen';
|
||||
|
||||
@override
|
||||
String get loginUsernameTFLabel => 'Benutzername';
|
||||
|
||||
@override
|
||||
String get loginPasswordTFLabel => 'Passwort';
|
||||
|
||||
@override
|
||||
String get loginLoginButtonLabel => 'Anmelden';
|
||||
|
||||
@override
|
||||
String errorWidgetStatusCode(int statusCode) {
|
||||
return 'Statuscode $statusCode';
|
||||
}
|
||||
|
||||
@override
|
||||
String errorWidget(String name) {
|
||||
return 'Fehler: $name';
|
||||
}
|
||||
|
||||
@override
|
||||
String get errorWidgetRetryButton => 'Wiederholen';
|
||||
|
||||
@override
|
||||
String get submitWidget => 'Speichern';
|
||||
|
||||
@override
|
||||
String get textInputWidgetValidatorText => 'Bitte geben Sie einen Text ein';
|
||||
|
||||
@override
|
||||
String get waitingWidget => 'Warten auf Ergebnis …';
|
||||
|
||||
@override
|
||||
String get deleteDialogTitle => 'Löschen';
|
||||
|
||||
@override
|
||||
String get deleteDialogText =>
|
||||
'Sind Sie sicher, dass Sie diese Eintrag löschen möchten?';
|
||||
|
||||
@override
|
||||
String get deleteDialogButtonCancel => 'Nein';
|
||||
|
||||
@override
|
||||
String get deleteDialogButtonApprove => 'Ja';
|
||||
}
|
||||
49
hartmann-foto-documentation-frontend/lib/main.dart
Normal file
49
hartmann-foto-documentation-frontend/lib/main.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:logger/web.dart' show DateTimeFormat, Logger, PrettyPrinter;
|
||||
import 'package:fotodocumentation/controller/login_controller.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/main_utils.dart';
|
||||
import 'package:fotodocumentation/utils/global_router.dart';
|
||||
|
||||
var logger = Logger(
|
||||
printer: PrettyPrinter(methodCount: 2, errorMethodCount: 8, colors: true, printEmojis: true, dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart),
|
||||
);
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
DiContainer.instance.initState();
|
||||
|
||||
final theme = await ThemeLoader.loadTheme();
|
||||
|
||||
LoginController loginController = DiContainer.get();
|
||||
//await loginController.isLoggedIn();
|
||||
runApp(FotoDocumentationApp(theme: theme));
|
||||
}
|
||||
|
||||
class FotoDocumentationApp extends StatelessWidget {
|
||||
final ThemeData theme;
|
||||
|
||||
const FotoDocumentationApp({super.key, required this.theme});
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
title: 'Hartmann Foto App',
|
||||
localizationsDelegates: [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: [
|
||||
Locale('de'),
|
||||
],
|
||||
scrollBehavior: MyCustomScrollBehavior(), // <== needed for web horizontal scroll behavior
|
||||
theme: theme,
|
||||
routerConfig: GlobalRouter.router);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CustomerWidget extends StatefulWidget {
|
||||
const CustomerWidget({super.key});
|
||||
|
||||
@override
|
||||
State<CustomerWidget> createState() => _CustomerWidgetState();
|
||||
}
|
||||
|
||||
class _CustomerWidgetState extends State<CustomerWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Placeholder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LandingPageWidget extends StatefulWidget {
|
||||
final Widget child;
|
||||
const LandingPageWidget({super.key, required this.child});
|
||||
|
||||
@override
|
||||
State<LandingPageWidget> createState() => _LandingPageWidgetState();
|
||||
}
|
||||
|
||||
class _LandingPageWidgetState extends State<LandingPageWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'package:fotodocumentation/controller/login_controller.dart';
|
||||
import 'package:fotodocumentation/dto/jwt_token_pair_dto.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/component/general_submit_widget.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/header_utils.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/modern_app_bar.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/login_credentials.dart';
|
||||
|
||||
class LoginWidget extends StatefulWidget {
|
||||
const LoginWidget({super.key});
|
||||
|
||||
@override
|
||||
State<LoginWidget> createState() => _LoginWidgetState();
|
||||
}
|
||||
|
||||
class _LoginWidgetState extends State<LoginWidget> {
|
||||
HeaderUtils get _headerUtils => DiContainer.get();
|
||||
LoginController get _loginController => DiContainer.get();
|
||||
LoginCredentials get _loginCredentials => DiContainer.get();
|
||||
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
|
||||
String? _error;
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_focusNode.dispose();
|
||||
_usernameController.dispose();
|
||||
_passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: ModernAppBar(
|
||||
title: _headerUtils.titleWidget("Login title"),
|
||||
actions: [],
|
||||
),
|
||||
body: _body(context),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.grey[50]!,
|
||||
Colors.white,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: _content(context),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _content(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: KeyboardListener(
|
||||
focusNode: _focusNode,
|
||||
onKeyEvent: (event) {
|
||||
if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.enter) {
|
||||
_actionSubmit(context);
|
||||
}
|
||||
},
|
||||
child: ListView(
|
||||
children: [
|
||||
Card(
|
||||
elevation: 4,
|
||||
margin: EdgeInsets.zero,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(30.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TextFormField(
|
||||
key: Key("username"),
|
||||
controller: _usernameController,
|
||||
decoration: InputDecoration(
|
||||
border: UnderlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)!.loginUsernameTFLabel,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
TextFormField(
|
||||
key: Key("password"),
|
||||
controller: _passwordController,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: UnderlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)!.loginPasswordTFLabel,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
if (_error != null) ...[
|
||||
Text(
|
||||
_error!,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
],
|
||||
GeneralSubmitWidget(
|
||||
key: const Key("submit"),
|
||||
onSelect: () async => await _actionSubmit(context),
|
||||
title: AppLocalizations.of(context)!.loginLoginButtonLabel,
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _actionSubmit(BuildContext context) async {
|
||||
String username = _usernameController.text;
|
||||
String password = _passwordController.text;
|
||||
|
||||
AuthenticateReply authenticateReply = await _loginController.authenticate(username, password);
|
||||
|
||||
JwtTokenPairDto? jwtTokenPairDto = authenticateReply.jwtTokenPairDto;
|
||||
if (jwtTokenPairDto == null) {
|
||||
setState(() => _error = "Error message");
|
||||
return;
|
||||
}
|
||||
|
||||
_loginCredentials.setLoggedIn(true);
|
||||
if (context.mounted) {
|
||||
context.go("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
import 'package:fotodocumentation/controller/base_controller.dart';
|
||||
|
||||
class GeneralErrorWidget extends StatelessWidget {
|
||||
final String error;
|
||||
final Function()? reload;
|
||||
final int? statusCode;
|
||||
|
||||
const GeneralErrorWidget({super.key, required this.error, this.reload, this.statusCode});
|
||||
|
||||
factory GeneralErrorWidget.fromServerError(ServerError serverError, {Function()? reload}) {
|
||||
return GeneralErrorWidget(error: "", reload: reload, statusCode: serverError.statusCode);
|
||||
}
|
||||
|
||||
factory GeneralErrorWidget.fromSnapshot(AsyncSnapshot snapshot, {Function()? reload}) {
|
||||
var error = snapshot.error;
|
||||
if (error is ServerError) {
|
||||
return GeneralErrorWidget.fromServerError(error, reload: () => reload);
|
||||
}
|
||||
return GeneralErrorWidget(error: snapshot.error.toString(), reload: () => reload);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localizations = AppLocalizations.of(context)!;
|
||||
final String errorMessage = statusCode != null
|
||||
? localizations.errorWidgetStatusCode(statusCode!)
|
||||
: localizations.errorWidget(error);
|
||||
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error_outline, size: 60, color: Colors.red[300]),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text(
|
||||
errorMessage,
|
||||
style: TextStyle(color: Colors.red[700]),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (reload != null) ...[
|
||||
ElevatedButton(
|
||||
onPressed: reload,
|
||||
child: Text(localizations.errorWidgetRetryButton),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/general_style.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
|
||||
typedef SubmitCallback = void Function();
|
||||
|
||||
class GeneralSubmitWidget extends StatelessWidget {
|
||||
GeneralStyle get _generalStyle => DiContainer.get();
|
||||
|
||||
final SubmitCallback onSelect;
|
||||
final String? title;
|
||||
|
||||
const GeneralSubmitWidget({super.key, required this.onSelect, this.title});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String text = title ?? AppLocalizations.of(context)!.submitWidget;
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
key: Key("SubmitWidgetButton"),
|
||||
style: _generalStyle.elevatedButtonStyle,
|
||||
onPressed: onSelect,
|
||||
child: Text(text),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PageHeaderWidget extends StatelessWidget {
|
||||
final IconData iconData;
|
||||
final String text;
|
||||
final String subText;
|
||||
final Color? iconColor;
|
||||
|
||||
const PageHeaderWidget({super.key, this.iconData = Icons.business, required this.text, this.subText = "", this.iconColor});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = iconColor ?? Theme.of(context).colorScheme.primary;
|
||||
return Card(
|
||||
elevation: 2,
|
||||
margin: EdgeInsets.zero,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: Colors.grey[300]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withAlpha(51),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
iconData,
|
||||
size: 32,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Text(
|
||||
text,
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (subText.isNotEmpty) ...[
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
subText,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
|
||||
class SearchBarCardWidget extends StatefulWidget {
|
||||
final TextEditingController searchController;
|
||||
final Function(String) onSearch;
|
||||
const SearchBarCardWidget({super.key, required this.searchController, required this.onSearch});
|
||||
|
||||
@override
|
||||
State<SearchBarCardWidget> createState() => _SearchBarCardWidgetState();
|
||||
}
|
||||
|
||||
class _SearchBarCardWidgetState extends State<SearchBarCardWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
elevation: 2,
|
||||
margin: EdgeInsets.zero,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: Colors.grey[300]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
key: Key("Search_text_field"),
|
||||
controller: widget.searchController,
|
||||
textAlignVertical: TextAlignVertical.center,
|
||||
decoration: InputDecoration(
|
||||
hintText: AppLocalizations.of(context)!.searchTFHint,
|
||||
border: InputBorder.none,
|
||||
prefixIcon: const Icon(Icons.search, size: 28),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
isDense: true,
|
||||
suffixIcon: InkWell(
|
||||
key: Key("Search_text_clear_button"),
|
||||
onTap: () => _actionClear(),
|
||||
child: const Icon(
|
||||
Icons.close,
|
||||
color: Colors.black,
|
||||
),
|
||||
)
|
||||
),
|
||||
onSubmitted: (_) => _actionSubmit(),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton.icon(
|
||||
key: Key("Search_text_button"),
|
||||
onPressed: _actionSubmit,
|
||||
icon: const Icon(Icons.search, size: 18),
|
||||
label: Text(AppLocalizations.of(context)!.searchButtonLabel),
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _actionSubmit() {
|
||||
widget.onSearch(widget.searchController.text);
|
||||
}
|
||||
|
||||
void _actionClear() {
|
||||
widget.searchController.text = "";
|
||||
widget.onSearch(widget.searchController.text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class TextInputWidget extends StatelessWidget {
|
||||
final String labelText;
|
||||
final bool required;
|
||||
final bool obscureText;
|
||||
final bool readOnly;
|
||||
final Function? onTap;
|
||||
const TextInputWidget({super.key, required this.labelText, this.required = false, this.obscureText = false, this.readOnly = false, this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<TextEditingController>(builder: (context, controller, child) {
|
||||
return TextFormField(
|
||||
readOnly: readOnly,
|
||||
obscureText: obscureText,
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
border: const UnderlineInputBorder(),
|
||||
labelText: labelText,
|
||||
),
|
||||
validator: (String? value) => required && (value == null || value.isEmpty) ? AppLocalizations.of(context)!.textInputWidgetValidatorText : null,
|
||||
onTap: () => onTap?.call(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class TextMultiInputWidget extends StatelessWidget {
|
||||
final String labelText;
|
||||
final bool required;
|
||||
final bool obscureText;
|
||||
final bool readOnly;
|
||||
final Function? onTap;
|
||||
final int maxLines;
|
||||
const TextMultiInputWidget({super.key, required this.labelText, this.required = false, this.obscureText = false, this.readOnly = false, this.maxLines = 6, this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<TextEditingController>(builder: (context, controller, child) {
|
||||
return TextFormField(
|
||||
readOnly: readOnly,
|
||||
minLines: 3, // Set this
|
||||
maxLines: maxLines, // and this
|
||||
keyboardType: TextInputType.multiline,
|
||||
obscureText: obscureText,
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
border: const UnderlineInputBorder(),
|
||||
labelText: labelText,
|
||||
),
|
||||
validator: (String? value) => required && (value == null || value.isEmpty) ? AppLocalizations.of(context)!.textInputWidgetValidatorText : null,
|
||||
onTap: () => onTap?.call(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
|
||||
class WaitingWidget extends StatelessWidget {
|
||||
const WaitingWidget({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const SizedBox(
|
||||
width: 60,
|
||||
height: 60,
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text(AppLocalizations.of(context)!.waitingWidget),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:fotodocumentation/main.dart' show logger;
|
||||
import 'package:fotodocumentation/l10n/app_localizations.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/dialog/snackbar_utils.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
|
||||
class DeleteDialog extends StatelessWidget {
|
||||
static SnackbarUtils get _snackbarUtils => DiContainer.get();
|
||||
|
||||
const DeleteDialog({super.key});
|
||||
|
||||
static Future<void> show(BuildContext context, Future<DeleteDialogResult> Function() doDelete) async {
|
||||
await _openDialog(context).then((value) async {
|
||||
if (value != null && value && context.mounted) {
|
||||
logger.d("Delete popup result $value");
|
||||
var result = await doDelete();
|
||||
if (context.mounted && result.msg.isNotEmpty) {
|
||||
_snackbarUtils.showSnackbar(context, result.msg, result.warning);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static Future<bool?> _openDialog(BuildContext context) async {
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
barrierDismissible: false, // user must tap button!
|
||||
builder: (BuildContext context) {
|
||||
return const DeleteDialog(
|
||||
key: Key("delete_dialog"),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final loc = AppLocalizations.of(context)!;
|
||||
|
||||
return AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
scrollable: false,
|
||||
titlePadding: const EdgeInsets.all(16.0),
|
||||
contentPadding: const EdgeInsets.fromLTRB(16.0, 0, 16.0, 16.0),
|
||||
title: _titleWidget(context, loc),
|
||||
content: _content(context, loc),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _titleWidget(BuildContext context, AppLocalizations loc) {
|
||||
return Card(
|
||||
elevation: 4,
|
||||
margin: EdgeInsets.zero,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: Colors.grey[300]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.warning_amber_rounded,
|
||||
size: 32,
|
||||
color: Colors.orange[700],
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Text(
|
||||
loc.deleteDialogTitle,
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
IconButton(
|
||||
key: const Key("close_button"),
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _content(BuildContext context, AppLocalizations loc) {
|
||||
return SizedBox(
|
||||
width: 400,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Card(
|
||||
elevation: 2,
|
||||
margin: EdgeInsets.zero,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: Colors.grey[300]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.warning_amber_rounded,
|
||||
size: 48,
|
||||
color: Colors.orange[700],
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Text(
|
||||
loc.deleteDialogText,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
key: const Key("delete_dialog:cancel"),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: Text(loc.deleteDialogButtonCancel),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton.icon(
|
||||
key: const Key("delete_dialog:approve"),
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||
backgroundColor: Colors.red[600],
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
icon: const Icon(Icons.delete, size: 20),
|
||||
label: Text(loc.deleteDialogButtonApprove),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteDialogResult {
|
||||
final String msg;
|
||||
final bool warning;
|
||||
|
||||
DeleteDialogResult({required this.msg, required this.warning});
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class DialogResult<T> {
|
||||
final DialogResultType type;
|
||||
final T? dto;
|
||||
|
||||
const DialogResult({required this.type, this.dto});
|
||||
}
|
||||
|
||||
enum DialogResultType {
|
||||
create,
|
||||
add;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract interface class SnackbarUtils {
|
||||
void showSnackbar(BuildContext context, String msg, bool warning);
|
||||
void showSnackbarPopup(BuildContext context, String msg, bool warning);
|
||||
}
|
||||
|
||||
class SnackbarUtilsImpl implements SnackbarUtils {
|
||||
@override
|
||||
void showSnackbar(BuildContext context, String msg, bool warning) {
|
||||
var snackBar = SnackBar(
|
||||
content: _contentFor(context, msg, warning),
|
||||
backgroundColor: Colors.white,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
showCloseIcon: true,
|
||||
closeIconColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
side: BorderSide(width: 2.0, style: BorderStyle.solid, color: Theme.of(context).colorScheme.inversePrimary),
|
||||
),
|
||||
margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.height - 130, left: MediaQuery.of(context).size.width - 400, right: 10),
|
||||
);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
||||
}
|
||||
|
||||
@override
|
||||
void showSnackbarPopup(BuildContext context, String msg, bool warning) {
|
||||
var snackBar = SnackBar(
|
||||
content: _contentFor(context, msg, warning),
|
||||
backgroundColor: Colors.white,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
showCloseIcon: true,
|
||||
closeIconColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
side: BorderSide(width: 2.0, style: BorderStyle.solid, color: Theme.of(context).colorScheme.inversePrimary),
|
||||
),
|
||||
width: 350,
|
||||
//margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.height - 100, left: MediaQuery.of(context).size.width - 350, right: 10),
|
||||
);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
||||
}
|
||||
|
||||
Widget _contentFor(BuildContext context, String msg, bool warning) {
|
||||
var icon = _iconFor(context, warning);
|
||||
|
||||
var style = _textStyleFor(context, warning);
|
||||
return Wrap(
|
||||
alignment: WrapAlignment.start,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
icon,
|
||||
Text(
|
||||
msg,
|
||||
style: style,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Icon _iconFor(BuildContext context, bool warning) {
|
||||
var color = _contentColor(context, warning);
|
||||
return warning ? Icon(Icons.error, color: color) : Icon(Icons.check_circle_outline, color: color);
|
||||
}
|
||||
|
||||
TextStyle _textStyleFor(BuildContext context, bool warning) {
|
||||
var color = _contentColor(context, warning);
|
||||
var bodyLarge = Theme.of(context).primaryTextTheme.bodyLarge!;
|
||||
var style = TextStyle(
|
||||
color: color,
|
||||
decoration: bodyLarge.decoration,
|
||||
fontFamily: bodyLarge.fontFamily,
|
||||
fontSize: bodyLarge.fontSize,
|
||||
fontWeight: bodyLarge.fontWeight,
|
||||
letterSpacing: bodyLarge.letterSpacing,
|
||||
textBaseline: bodyLarge.textBaseline);
|
||||
return style;
|
||||
}
|
||||
|
||||
Color _contentColor(BuildContext context, bool warning) {
|
||||
return warning ? Theme.of(context).colorScheme.error : Theme.of(context).colorScheme.inversePrimary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pinput/pinput.dart';
|
||||
|
||||
abstract interface class GeneralStyle {
|
||||
PinTheme get pinTheme;
|
||||
|
||||
ButtonStyle get elevatedButtonStyle;
|
||||
ButtonStyle get roundedButtonStyle;
|
||||
}
|
||||
|
||||
class GeneralStyleImpl implements GeneralStyle {
|
||||
static final ButtonStyle _elevatedButtonStyle = ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
|
||||
static final ButtonStyle _roundedButtonStyle = ElevatedButton.styleFrom(shape: const CircleBorder(), padding: const EdgeInsets.all(8));
|
||||
|
||||
@override
|
||||
PinTheme get pinTheme => _getPinTheme();
|
||||
|
||||
@override
|
||||
ButtonStyle get elevatedButtonStyle => _elevatedButtonStyle;
|
||||
|
||||
@override
|
||||
ButtonStyle get roundedButtonStyle => _roundedButtonStyle;
|
||||
|
||||
PinTheme _getPinTheme() {
|
||||
return PinTheme(
|
||||
width: 56,
|
||||
height: 56,
|
||||
textStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class HeaderButtonWrapper extends StatefulWidget {
|
||||
final IconData icon;
|
||||
final String tooltip;
|
||||
final VoidCallback onPressed;
|
||||
final Color? iconColor;
|
||||
final int? badgeCount;
|
||||
|
||||
const HeaderButtonWrapper({
|
||||
super.key,
|
||||
required this.icon,
|
||||
required this.tooltip,
|
||||
required this.onPressed,
|
||||
this.iconColor,
|
||||
this.badgeCount,
|
||||
});
|
||||
|
||||
@override
|
||||
State<HeaderButtonWrapper> createState() => _HeaderButtonWrapperState();
|
||||
}
|
||||
|
||||
class _HeaderButtonWrapperState extends State<HeaderButtonWrapper> {
|
||||
bool _isHovered = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MouseRegion(
|
||||
onEnter: (_) => setState(() => _isHovered = true),
|
||||
onExit: (_) => setState(() => _isHovered = false),
|
||||
child: Tooltip(
|
||||
message: widget.tooltip,
|
||||
child: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: _isHovered ? Colors.blue[50] : Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: _isHovered ? Colors.blue[200]! : Colors.transparent,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: IconButton(
|
||||
icon: Icon(
|
||||
widget.icon,
|
||||
size: 24,
|
||||
color: widget.iconColor ?? Colors.grey[700],
|
||||
),
|
||||
onPressed: widget.onPressed,
|
||||
splashRadius: 24,
|
||||
),
|
||||
),
|
||||
if (widget.badgeCount != null && widget.badgeCount! > 0)
|
||||
Positioned(
|
||||
right: 4,
|
||||
top: 4,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red[600],
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: Colors.white,
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 20,
|
||||
minHeight: 20,
|
||||
),
|
||||
child: Text(
|
||||
widget.badgeCount! > 99 ? '99+' : widget.badgeCount.toString(),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/login_credentials.dart';
|
||||
|
||||
abstract interface class HeaderUtils {
|
||||
Widget titleWidget(String text);
|
||||
}
|
||||
|
||||
class HeaderUtilsImpl extends HeaderUtils {
|
||||
LoginCredentials get _loginCredentials => DiContainer.get();
|
||||
|
||||
@override
|
||||
Widget titleWidget(String text) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.school,
|
||||
size: 28,
|
||||
color: Colors.blue[700],
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Text(
|
||||
text,
|
||||
style: const TextStyle(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
if (_loginCredentials.fullname.isNotEmpty) ...[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 40.0),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue[50],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Colors.blue[200]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.person_outline,
|
||||
size: 14,
|
||||
color: Colors.blue[700],
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
_loginCredentials.fullname,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.blue[900],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/header_button_wrapper.dart';
|
||||
|
||||
class ModernAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final Widget title;
|
||||
final List<Widget> actions;
|
||||
final Widget? leading;
|
||||
final bool automaticallyImplyLeading;
|
||||
|
||||
const ModernAppBar({
|
||||
super.key,
|
||||
required this.title,
|
||||
this.actions = const [],
|
||||
this.leading,
|
||||
this.automaticallyImplyLeading = true,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget? effectiveLeading = _effectiveLeading(context);
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[50],
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: Colors.grey[300]!,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
||||
child: Row(
|
||||
children: [
|
||||
if (effectiveLeading != null) ...[
|
||||
effectiveLeading,
|
||||
const SizedBox(width: 16),
|
||||
],
|
||||
Expanded(child: title),
|
||||
const SizedBox(width: 16),
|
||||
...actions.map((action) => Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: action,
|
||||
)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget? _effectiveLeading(BuildContext context) {
|
||||
// Determine if we should show a back button
|
||||
final ScaffoldState? scaffold = Scaffold.maybeOf(context);
|
||||
final ModalRoute<dynamic>? parentRoute = ModalRoute.of(context);
|
||||
final bool hasDrawer = scaffold?.hasDrawer ?? false;
|
||||
final bool canPop = parentRoute?.canPop ?? false;
|
||||
final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
|
||||
|
||||
Widget? effectiveLeading = leading;
|
||||
if (effectiveLeading == null && automaticallyImplyLeading) {
|
||||
if (hasDrawer) {
|
||||
effectiveLeading = HeaderButtonWrapper(
|
||||
icon: Icons.menu,
|
||||
onPressed: () {
|
||||
Scaffold.of(context).openDrawer();
|
||||
},
|
||||
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
|
||||
iconColor: Colors.grey[700],
|
||||
);
|
||||
} else if (canPop) {
|
||||
if (useCloseButton) {
|
||||
effectiveLeading = HeaderButtonWrapper(
|
||||
icon: Icons.close,
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
|
||||
iconColor: Colors.grey[700],
|
||||
);
|
||||
} else {
|
||||
effectiveLeading = HeaderButtonWrapper(
|
||||
icon: Icons.arrow_back,
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
tooltip: MaterialLocalizations.of(context).backButtonTooltip,
|
||||
iconColor: Colors.grey[700],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return effectiveLeading;
|
||||
}
|
||||
|
||||
@override
|
||||
Size get preferredSize => const Size.fromHeight(80);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
final class DateTimeUtils {
|
||||
static DateTime? toDateTime(dynamic element) {
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
String text = element.toString();
|
||||
int? time = int.tryParse(text);
|
||||
if (time == null) {
|
||||
return null;
|
||||
}
|
||||
return DateTime.fromMillisecondsSinceEpoch(time);
|
||||
}
|
||||
|
||||
static int? fromDateTime(DateTime? dt) {
|
||||
return dt?.millisecondsSinceEpoch;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:fotodocumentation/controller/login_controller.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/dialog/snackbar_utils.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/general_style.dart';
|
||||
import 'package:fotodocumentation/pages/ui_utils/header_utils.dart';
|
||||
import 'package:fotodocumentation/utils/http_client_utils.dart';
|
||||
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
|
||||
import 'package:fotodocumentation/utils/login_credentials.dart';
|
||||
import 'package:fotodocumentation/utils/url_utils.dart';
|
||||
|
||||
class DiContainer {
|
||||
static final DiContainer instance = DiContainer._privateConstructor();
|
||||
DiContainer._privateConstructor();
|
||||
|
||||
final _container = {};
|
||||
|
||||
static T get<T>() {
|
||||
return instance._container[T] as T;
|
||||
}
|
||||
|
||||
void initState() {
|
||||
DiContainer.instance.put(LoginCredentials, LoginCredentialsImpl());
|
||||
DiContainer.instance.put(GeneralStyle, GeneralStyleImpl());
|
||||
DiContainer.instance.put(JwtTokenStorage, JwtTokenStorageImpl());
|
||||
DiContainer.instance.put(HttpClientUtils, HttpCLientUtilsImpl());
|
||||
DiContainer.instance.put(HeaderUtils, HeaderUtilsImpl());
|
||||
DiContainer.instance.put(UrlUtils, UrlUtilsImpl());
|
||||
DiContainer.instance.put(SnackbarUtils, SnackbarUtilsImpl());
|
||||
DiContainer.instance.put(LoginController, LoginControllerImpl());
|
||||
}
|
||||
|
||||
void put<T>(Type key, T object) {
|
||||
_container[key] = object;
|
||||
}
|
||||
|
||||
T get2<T>() {
|
||||
return _container[T] as T;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart' show Colors, Color;
|
||||
|
||||
extension HexColor on Color {
|
||||
/// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
|
||||
static Color fromHex(String hexString) {
|
||||
final buffer = StringBuffer();
|
||||
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
|
||||
buffer.write(hexString.replaceFirst('#', ''));
|
||||
return Color(int.parse(buffer.toString(), radix: 16));
|
||||
}
|
||||
}
|
||||
|
||||
extension RiskColor on Color {
|
||||
static const Color noRisk = Colors.transparent;
|
||||
static final Color lowRisk = HexColor.fromHex("#FFFF00");
|
||||
static final Color mediumRisk = HexColor.fromHex("#FF9000");
|
||||
static final Color highRisk = HexColor.fromHex("#FF4000");
|
||||
|
||||
static Color colorForRisk(int value) {
|
||||
if (value == 1) {
|
||||
return lowRisk;
|
||||
} else if (value == 2) {
|
||||
return mediumRisk;
|
||||
} else if (value == 3) {
|
||||
return highRisk;
|
||||
}
|
||||
return noRisk;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// needed for web horizontal scroll behavior
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fotodocumentation/main.dart';
|
||||
import 'package:fotodocumentation/pages/customer/customer_widget.dart';
|
||||
import 'package:fotodocumentation/pages/login/login_widget.dart';
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/login_credentials.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class GlobalRouter {
|
||||
static final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'root');
|
||||
static final GlobalKey<NavigatorState> bottomBarNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'bottombar');
|
||||
static final GlobalKey<NavigatorState> adminNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'admin');
|
||||
static final GlobalKey<NavigatorState> skillEditorNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'skillEditor');
|
||||
|
||||
static final String pathHome = "/home";
|
||||
static final String pathLogin = "/login";
|
||||
|
||||
static final GoRouter router = createRouter(pathHome);
|
||||
|
||||
static GoRouter createRouter(String initialLocation) {
|
||||
return GoRouter(
|
||||
navigatorKey: rootNavigatorKey,
|
||||
initialLocation: initialLocation,
|
||||
routes: <RouteBase>[
|
||||
GoRoute(
|
||||
path: "/",
|
||||
redirect: (_, __) => pathHome,
|
||||
),
|
||||
GoRoute(
|
||||
path: pathLogin,
|
||||
builder: (BuildContext context, GoRouterState state) => const LoginWidget(),
|
||||
),
|
||||
GoRoute(
|
||||
path: pathHome,
|
||||
builder: (context, state) => CustomerWidget(),
|
||||
),
|
||||
],
|
||||
redirect: (context, state) {
|
||||
var uriStr = state.uri.toString();
|
||||
logger.t("uri $uriStr");
|
||||
LoginCredentials loginCredentials = DiContainer.get();
|
||||
|
||||
if (!loginCredentials.isLoggedIn) {
|
||||
return pathLogin;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class GlobalStack<T> {
|
||||
final _list = <T>[];
|
||||
|
||||
void push(T value) => _list.add(value);
|
||||
|
||||
T pop() => _list.removeLast();
|
||||
|
||||
T peek() => _list.last;
|
||||
|
||||
bool get isEmpty => _list.isEmpty;
|
||||
|
||||
bool get isNotEmpty => _list.isNotEmpty;
|
||||
|
||||
@override
|
||||
String toString() => _list.toString();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:fotodocumentation/utils/http_client_factory_stub.dart';
|
||||
|
||||
HttpCLientFactory getHttpClientFactory() => HttpClientFactoryApp();
|
||||
|
||||
class HttpClientFactoryApp extends HttpCLientFactory {
|
||||
@override
|
||||
http.Client createHttpClient() {
|
||||
return http.Client();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
HttpCLientFactory getHttpClientFactory() => throw UnsupportedError('Cannot create http client');
|
||||
|
||||
|
||||
class HttpCLientFactory {
|
||||
http.Client createHttpClient() {
|
||||
// Check if running on the Web
|
||||
/*if (kIsWeb) {
|
||||
var client = http.Client();
|
||||
(client as BrowserClient).withCredentials = true;
|
||||
return client;
|
||||
} else if (universal_io.Platform.isAndroid || universal_io.Platform.isIOS) {
|
||||
// Platform-specific logic for Android and iOS
|
||||
return http.Client();
|
||||
} else {
|
||||
throw UnsupportedError('Unsupported platform');
|
||||
}*/
|
||||
throw UnsupportedError('Cannot create http client');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:http/browser_client.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:fotodocumentation/utils/http_client_factory_stub.dart';
|
||||
|
||||
HttpCLientFactory getHttpClientFactory() => HttpClientFactoryWeb();
|
||||
|
||||
class HttpClientFactoryWeb extends HttpCLientFactory{
|
||||
@override
|
||||
http.Client createHttpClient() {
|
||||
var client = http.Client();
|
||||
(client as BrowserClient).withCredentials = true;
|
||||
return client;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:fotodocumentation/utils/di_container.dart';
|
||||
import 'package:fotodocumentation/utils/login_credentials.dart';
|
||||
import 'package:fotodocumentation/utils/global_router.dart';
|
||||
|
||||
/// HTTP client that intercepts all responses and handles 401 status codes
|
||||
/// by logging out the user and redirecting to the login page.
|
||||
class HttpClientInterceptor extends http.BaseClient {
|
||||
final http.Client _inner;
|
||||
|
||||
HttpClientInterceptor(this._inner);
|
||||
|
||||
@override
|
||||
Future<http.StreamedResponse> send(http.BaseRequest request) async {
|
||||
final response = await _inner.send(request);
|
||||
|
||||
// Check for 401 Unauthorized
|
||||
if (response.statusCode == 401) {
|
||||
_handle401Unauthorized();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
void _handle401Unauthorized() {
|
||||
// Clear login credentials
|
||||
final loginCredentials = DiContainer.get<LoginCredentials>();
|
||||
loginCredentials.logout();
|
||||
|
||||
// Navigate to login page using GoRouter
|
||||
final context = GlobalRouter.rootNavigatorKey.currentContext;
|
||||
if (context != null) {
|
||||
context.go(GlobalRouter.pathLogin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'http_client_factory_stub.dart' if (dart.library.io) 'http_client_factory_app.dart' if (dart.library.js) 'http_client_factory_web.dart';
|
||||
import 'http_client_interceptor.dart';
|
||||
|
||||
abstract class HttpClientUtils {
|
||||
http.Client get client;
|
||||
}
|
||||
|
||||
class HttpCLientUtilsImpl extends HttpClientUtils {
|
||||
http.Client? _client;
|
||||
|
||||
@override
|
||||
http.Client get client => _getClient();
|
||||
|
||||
http.Client _getClient() {
|
||||
_client ??= HttpClientInterceptor(getHttpClientFactory().createHttpClient());
|
||||
return _client!;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
abstract class JwtTokenStorage {
|
||||
/// Save both access and refresh tokens
|
||||
///
|
||||
/// @param accessToken The short-lived access token
|
||||
/// @param refreshToken The long-lived refresh token
|
||||
Future<void> saveTokens(String accessToken, String refreshToken);
|
||||
|
||||
/// Get the stored access token
|
||||
///
|
||||
/// @return Access token or null if not found
|
||||
Future<String?> getAccessToken();
|
||||
|
||||
/// Get the stored refresh token
|
||||
///
|
||||
/// @return Refresh token or null if not found
|
||||
Future<String?> getRefreshToken();
|
||||
|
||||
/// Clear all stored tokens (on logout)
|
||||
Future<void> clearTokens();
|
||||
|
||||
/// Check if tokens are stored
|
||||
///
|
||||
/// @return true if access token exists
|
||||
Future<bool> hasTokens();
|
||||
|
||||
/// Update only the access token (used after refresh)
|
||||
///
|
||||
/// @param accessToken New access token
|
||||
Future<void> updateAccessToken(String accessToken);
|
||||
}
|
||||
|
||||
class JwtTokenStorageImpl extends JwtTokenStorage {
|
||||
|
||||
// Storage keys
|
||||
String? _keyAccessToken;
|
||||
String? _keyRefreshToken;
|
||||
|
||||
@override
|
||||
Future<void> saveTokens(String accessToken, String refreshToken) async {
|
||||
_keyAccessToken = accessToken;
|
||||
_keyRefreshToken = refreshToken;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getAccessToken() async {
|
||||
return _keyAccessToken;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getRefreshToken() async {
|
||||
return _keyRefreshToken;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearTokens() async {
|
||||
_keyAccessToken = null;
|
||||
_keyRefreshToken = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> hasTokens() async {
|
||||
return _keyAccessToken != null && _keyAccessToken!.isNotEmpty;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateAccessToken(String accessToken) async {
|
||||
_keyAccessToken == accessToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract class LoginCredentials extends ChangeNotifier {
|
||||
String get fullname;
|
||||
bool get isLoggedIn;
|
||||
|
||||
void setLoggedIn(bool loggedIn);
|
||||
void logout();
|
||||
}
|
||||
|
||||
class LoginCredentialsImpl extends LoginCredentials {
|
||||
bool loggedIn = false;
|
||||
|
||||
@override
|
||||
bool get isLoggedIn => loggedIn;
|
||||
@override
|
||||
String get fullname => "";
|
||||
|
||||
@override
|
||||
void setLoggedIn(bool loggedIn) {
|
||||
this.loggedIn = loggedIn;
|
||||
}
|
||||
|
||||
@override
|
||||
void logout() {
|
||||
loggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// needed for web horizontal scroll behavior
|
||||
import 'dart:convert' show jsonDecode;
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:json_theme/json_theme.dart';
|
||||
import 'package:fotodocumentation/main.dart' show logger;
|
||||
|
||||
class MyCustomScrollBehavior extends MaterialScrollBehavior {
|
||||
// Override behavior methods and getters like dragDevices
|
||||
@override
|
||||
Set<PointerDeviceKind> get dragDevices => {
|
||||
PointerDeviceKind.touch,
|
||||
PointerDeviceKind.mouse,
|
||||
};
|
||||
}
|
||||
|
||||
class ThemeLoader {
|
||||
static Future<ThemeData> loadTheme() async {
|
||||
try {
|
||||
String prefix = kDebugMode && kIsWeb ? "" : "assets/";
|
||||
String url = "${prefix}theme/appainter_theme.json";
|
||||
final themeStr = await rootBundle.loadString(url);
|
||||
final themeJson = jsonDecode(themeStr);
|
||||
return ThemeDecoder.decodeThemeData(themeJson)!;
|
||||
} catch (e) {
|
||||
logger.e("Failed to load theme $e", error: e);
|
||||
return ThemeData.light();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:basic_utils/basic_utils.dart' show StringUtils;
|
||||
|
||||
abstract interface class PasswordUtils {
|
||||
String create();
|
||||
}
|
||||
|
||||
class PasswordUtilsImpl implements PasswordUtils {
|
||||
@override
|
||||
String create() {
|
||||
return StringUtils.generateRandomString(8, special: false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter/foundation.dart' show kReleaseMode;
|
||||
|
||||
abstract interface class UrlUtils {
|
||||
String getBaseUrl();
|
||||
}
|
||||
|
||||
class UrlUtilsImpl extends UrlUtils {
|
||||
@override
|
||||
String getBaseUrl() {
|
||||
if (kReleaseMode){
|
||||
return "${Uri.base.origin}/api/";
|
||||
}
|
||||
return "http://localhost:8080/api/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user