You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
Dart
94 lines
2.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
// import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:mohem_flutter_app/exceptions/api_exception.dart';
|
|
import 'package:mohem_flutter_app/widgets/dialogs/confirm_dialog.dart';
|
|
import 'package:mohem_flutter_app/widgets/loading_dialog.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class Utils {
|
|
static bool _isLoadingVisible = false;
|
|
|
|
static bool get isLoading => _isLoadingVisible;
|
|
|
|
static void showToast(String message) {
|
|
Fluttertoast.showToast(
|
|
msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.black54, textColor: Colors.white, fontSize: 16.0);
|
|
}
|
|
|
|
static dynamic getNotNullValue(List<dynamic> list, int index) {
|
|
try {
|
|
return list[index];
|
|
} catch (ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static int stringToHex(String colorCode) {
|
|
try {
|
|
return int.parse(colorCode.replaceAll("#", "0xff"));
|
|
} catch (ex) {
|
|
return (0xff000000);
|
|
}
|
|
}
|
|
|
|
static void showLoading(BuildContext context) {
|
|
WidgetsBinding.instance?.addPostFrameCallback((_) {
|
|
_isLoadingVisible = true;
|
|
showDialog(
|
|
context: context,
|
|
barrierColor: Colors.black.withOpacity(0.5),
|
|
builder: (BuildContext context) => LoadingDialog(),
|
|
).then((value) {
|
|
_isLoadingVisible = false;
|
|
});
|
|
});
|
|
}
|
|
|
|
static void hideLoading(BuildContext context) {
|
|
if (_isLoadingVisible) {
|
|
_isLoadingVisible = false;
|
|
Navigator.of(context).pop();
|
|
}
|
|
_isLoadingVisible = false;
|
|
}
|
|
|
|
static Future<String> getStringFromPrefs(String key) async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString(key) ?? "";
|
|
}
|
|
|
|
static Future<bool> saveStringFromPrefs(String key, String value) async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
return await prefs.setString(key, value);
|
|
}
|
|
|
|
static void handleException(dynamic exception, Function(String)? onErrorMessage) {
|
|
String errorMessage;
|
|
if (exception is APIException) {
|
|
if (exception.message == APIException.UNAUTHORIZED) {
|
|
return;
|
|
} else {
|
|
errorMessage = exception.error?.errorMessage ?? exception.message;
|
|
}
|
|
} else {
|
|
errorMessage = APIException.UNKNOWN;
|
|
}
|
|
if (onErrorMessage != null) {
|
|
onErrorMessage(errorMessage);
|
|
} else {
|
|
showToast(errorMessage);
|
|
}
|
|
}
|
|
|
|
static void confirmDialog(cxt, String message) {
|
|
showDialog(
|
|
context: cxt,
|
|
builder: (cxt) => ConfirmDialog(
|
|
message: message,
|
|
),
|
|
);
|
|
}
|
|
}
|