Flutter

Toast

class PmsToast {

  PmsToast._();


  static show(String message) => Fluttertoast.showToast(msg: message);


  static showShortTop(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.TOP);


  static showShortCenter(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER);


  static showShortBottom(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM);


  static showLongTop(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.TOP);


  static showLongCenter(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.CENTER);


  static showLongBottom(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM);


  static showSuccessST(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.TOP, backgroundColor: Colors.green);


  static showSuccessSC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, backgroundColor: Colors.green);


  static showSuccessSB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.green);


  static showSuccessLT(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.TOP, backgroundColor: Colors.green);


  static showSuccessLC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.CENTER, backgroundColor: Colors.green);


  static showSuccessLB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.green);


  static showErrorST(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.TOP, backgroundColor: Colors.red);


  static showErrorSC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, backgroundColor: Colors.red);


  static showErrorSB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.red);


  static showErrorLT(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.TOP, backgroundColor: Colors.red);


  static showErrorLC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.CENTER, backgroundColor: Colors.red);


  static showErrorLB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.red);


  static showInfoST(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.TOP, backgroundColor: Colors.blue);


  static showInfoSC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, backgroundColor: Colors.blue);


  static showInfoSB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.blue);


  static showInfoLT(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.TOP, backgroundColor: Colors.blue);


  static showInfoLC(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.CENTER, backgroundColor: Colors.blue);


  static showInfoLB(String message) => Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.blue);


  static showCustomST(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.TOP, textColor: textColor, backgroundColor: backgroundColor);


  static showCustomSC(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, textColor: textColor, backgroundColor: backgroundColor);


  static showCustomSB(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, textColor: textColor, backgroundColor: backgroundColor);


  static showCustomLT(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.TOP, textColor: textColor, backgroundColor: backgroundColor);


  static showCustomLC(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.CENTER, textColor: textColor, backgroundColor: backgroundColor);


  static showCustomLB(String message, Color textColor, Color backgroundColor) =>

      Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, textColor: textColor, backgroundColor: backgroundColor);

}




MapConfig

class MapConfig {

  mapper(dynamic data, dClass) {

    List<dynamic> keys = dClass.toJson().keys.toList();

    var newModel = dClass.toJson();

    for (var key in keys) {

      newModel[key] = data.toJson()[key];

    }

    return json.encode(newModel);

  }


  mapperList(List<dynamic> data, dClass) {

    List<dynamic>? keys = dClass.toJson().keys.toList();

    List<dynamic> result = [];

    for (var model in List<dynamic>.from(data.map((x) => x.toJson()))) {

      var newModel = dClass.toJson();

      for (var key in keys!) {

        newModel[key] = model[key];

      }

      result.add(newModel);

    }

    return json.encode(List<dynamic>.from(result.map((x) => x)));

  }

}



CheckNet

class CheckNet {

  factory CheckNet() {

    if (_checkNet == null) {

      _checkNet = CheckNet._internal();

      return _checkNet!;

    } else {

      return _checkNet!;

    }

  }


  CheckNet._internal();


  static StreamController<bool> connectController = BehaviorSubject();

  static bool isOnline = false;


  static CheckNet? _checkNet;


  final Connectivity _connectivity = Connectivity();

  late StreamSubscription<ConnectivityResult> _connectivitySubscription;


  void startListen() {

    initConnectivity();

    _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);

  }


  void cancelListen() {

    connectController.close();

    _connectivitySubscription.cancel();

  }


  Future<void> initConnectivity() async {

    ConnectivityResult result;

    try {

      result = await _connectivity.checkConnectivity();

    } on PlatformException {

      return;

    }


    return _updateConnectionStatus(result);

  }


  Future<bool> hasNetwork() async {

    try {

      final result = await InternetAddress.lookup('google.com');

      return result.isNotEmpty && result[0].rawAddress.isNotEmpty;

    } on SocketException catch (_) {

      return false;

    }

  }


  Future<bool> isMobile() async => await _connectivity.checkConnectivity().then((value) => value == ConnectivityResult.mobile);


  Future<bool> isWifi() async => await _connectivity.checkConnectivity().then((value) => value == ConnectivityResult.wifi);


  Future<void> _updateConnectionStatus(ConnectivityResult result) async {

    if (result == ConnectivityResult.none) {

      isOnline = false;

      connectController.sink.add(false);

    } else {

      hasNetwork().then((value) {

        isOnline = value;

        connectController.sink.add(value);

      });

    }

  }

}


Shared Preferences

class SharePrefUtil {

  static SharePrefUtil? _sharePrefUtil;

  static SharedPreferences? _prefs;


  factory SharePrefUtil() {

    _sharePrefUtil = SharePrefUtil._internal();

    return _sharePrefUtil ?? SharePrefUtil._internal();

  }


  SharePrefUtil._internal();


  Future init() async {

    _prefs = await SharedPreferences.getInstance();

  }


  static Future<bool>? putObject(String key, Object value) {

    return _prefs?.setString(key, json.encode(value));

  }


  static T? getObj<T>(String key, T Function(Map v) f, {T? defValue}) {

    Map? map = getObject(key);

    return map == null ? defValue : f(map);

  }


  static Map? getObject(String key) {

    String? data = _prefs?.getString(key);

    return (data == null || data.isEmpty) ? null : json.decode(data);

  }


  static Future<bool>? putObjectList(String key, List<Object> list) {

    List<String>? dataList = list.map((value) {

      return json.encode(value);

    }).toList();

    return _prefs?.setStringList(key, dataList);

  }


  static List<T>? getObjList<T>(String key, T Function(Map v) f, {List<T>? defValue = const []}) {

    List<Map>? dataList = getObjectList(key);

    List<T>? list = dataList?.map((value) {

      return f(value);

    }).toList();

    return list ?? defValue;

  }


  static List<Map>? getObjectList(String key) {

    List<String>? dataLis = _prefs?.getStringList(key);

    return dataLis?.map((value) {

      Map dataMap = json.decode(value);

      return dataMap;

    }).toList();

  }


  static String? getString(String key, {String? defValue = ''}) {

    return _prefs?.getString(key) ?? defValue;

  }


  static Future<bool>? putString(String key, String value) {

    return _prefs?.setString(key, value);

  }


  static bool? getBool(String key, {bool? defValue = false}) {

    return _prefs?.getBool(key) ?? defValue;

  }


  static Future<bool>? putBool(String key, bool value) {

    return _prefs?.setBool(key, value);

  }


  static int? getInt(String key, {int? defValue = 0}) {

    return _prefs?.getInt(key) ?? defValue;

  }


  static Future<bool>? putInt(String key, int value) {

    return _prefs?.setInt(key, value);

  }


  static double? getDouble(String key, {double? defValue = 0.0}) {

    return _prefs?.getDouble(key) ?? defValue;

  }


  static Future<bool>? putDouble(String key, double value) {

    return _prefs?.setDouble(key, value);

  }


  static List<String>? getStringList(String key, {List<String>? defValue = const []}) {

    return _prefs?.getStringList(key) ?? defValue;

  }


  static Future<bool>? putStringList(String key, List<String> value) {

    return _prefs?.setStringList(key, value);

  }


  static dynamic getDynamic(String key, {Object? defValue}) {

    return _prefs?.get(key) ?? defValue;

  }


  static bool? haveKey(String key) {

    return _prefs?.getKeys().contains(key);

  }


  static bool? containsKey(String key) {

    return _prefs?.containsKey(key);

  }


  static Set<String>? getKeys() {

    return _prefs?.getKeys();

  }


  static Future<bool>? remove(String key) {

    return _prefs?.remove(key);

  }


  static Future<bool>? clear() {

    return _prefs?.clear();

  }


  static Future<void>? reload() {

    return _prefs?.reload();

  }


  static bool isInitialized() {

    return _prefs != null;

  }


  static SharedPreferences? getSp() {

    return _prefs;

  }

}



Json Helper

List<String> listStringFromJson(String str) => List<String>.from(json.decode(str).map((x) => x));


String listStringToJson(List<String> data) => json.encode(List<dynamic>.from(data.map((x) => x)));


List<dynamic> listDynamicFromJson(String str) => List<dynamic>.from(json.decode(str).map((x) => x));


String listDynamicToJson(List<dynamic> data) => json.encode(List<dynamic>.from(data.map((x) => x)));


Context Extension

extension ContextExtension on BuildContext {

  PmsColor get pmsColor => PmsColor();

  PmsSize get pmsSize => PmsSize();

  PmsText get pmsText => PmsText();

}


class PmsColor {

  Color get cardColor => Colors.blue[400]!;

}


class PmsSize {

  double get height => InfoService.height;

  double get width => InfoService.width;

  EdgeInsetsGeometry get cardPadding => EdgeInsets.all(width / 25);

}


class PmsText {

  TextStyle get textSpanStyle => TextStyle(color: Colors.black, fontSize: InfoService.height / 60);

}