import ' dart:convert ' ;
import ' dart:typed_data ' ;
import ' package:flutter/material.dart ' ;
import ' package:flutter_svg/flutter_svg.dart ' ;
import ' package:fluttertoast/fluttertoast.dart ' ;
import ' package:mohem_flutter_app/app_state/app_state.dart ' ;
import ' package:mohem_flutter_app/config/routes.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 , { bool longDuration = false } ) {
Fluttertoast . showToast (
msg: message ,
toastLength: longDuration ? Toast . LENGTH_LONG : 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 Future delay ( int millis ) async {
return await Future . delayed ( Duration ( milliseconds: millis ) ) ;
}
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 , cxt , 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 {
if ( ! AppState ( ) . isAuthenticated ) {
showDialog (
context: cxt ,
builder: ( cxt ) = > ConfirmDialog (
message: errorMessage ,
onTap: ( ) {
Navigator . pushNamedAndRemoveUntil ( cxt , AppRoutes . login , ( Route < dynamic > route ) = > false ) ;
} ,
) ,
) ;
} else {
showToast ( errorMessage ) ;
}
}
}
static void confirmDialog ( cxt , String message ) {
showDialog (
context: cxt ,
builder: ( cxt ) = > ConfirmDialog (
message: message ,
) ,
) ;
}
static Widget getNoDataWidget ( BuildContext context ) {
return Container (
child: Center (
child: Column (
mainAxisAlignment: MainAxisAlignment . center ,
children: [
SvgPicture . asset ( ' assets/images/not_found.svg ' , width: 110.0 , height: 110.0 ) ,
Container ( margin: const EdgeInsets . only ( top: 15.0 ) , child: const Text ( " No Result Found " , style: TextStyle ( fontSize: 14 , fontWeight: FontWeight . w600 , color: Color ( 0xFFBABABA ) ) ) ) ,
] ,
) ,
) ,
) ;
}
static getPostBytes ( img ) {
try {
var b64 = img . replaceFirst ( ' data:image/png;base64, ' , ' ' ) ;
if ( img ! = null & & Utils . isBase64 ( b64 ) ) return Utils . dataFromBase64String ( b64 ) ;
} catch ( e ) { }
return null ;
}
static bool isBase64 ( String str ) {
RegExp _base64 = RegExp ( r'^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$' ) ;
return _base64 . hasMatch ( str ) ;
}
static Uint8List dataFromBase64String ( String base64String ) {
return base64Decode ( base64String ) ;
}
}