import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:ftc_mobile_app/view/custom_widgets/loading_widget.dart'; import 'package:get/get.dart'; //BuildContext extension BuildContextExtensions on BuildContext { showErrorSnackBar({required String message, Duration? duration}) { try { ScaffoldMessenger.of(this).showSnackBar(SnackBar( content: Text(message), backgroundColor: Colors.red, duration: duration ?? const Duration(seconds: 4), )); } catch (e) { debugPrint(e.toString()); } } } // num extension NumExtensions on num { BorderRadius toRadius() { return BorderRadius.circular(toDouble().r); } RoundedRectangleBorder toRoundedRectRadius() { return RoundedRectangleBorder(borderRadius: toRadius()); } num percentOf(num value) { return value * (this / 100.0); } double toRadian() { return this * pi / 180; } double toDegree() { return this * 180 / pi; } double get milesToMeters => this * 1609.344; double get metersToMiles => this / 1609.344; } // string extension StringExtensions on String? { bool isNullOrEmpty() { return (this == null || this!.isEmpty); } bool isNullOrEmptyNot() { return (this != null && this!.isNotEmpty); } String removeSpaces() { return this?.replaceAll(RegExp(r"\s\b|\b\s"), "") ?? ""; } bool get hasDigitsOnly { return isNotNullOrEmpty() && RegExp(r'^[0-9]+$').hasMatch(this!); } } // widgets extension WidgetsExtension on Widget { // add click Widget addInkwell({required Function onClick}) { return InkWell( child: this, onTap: () => onClick(), ); } // add padding Widget addPaddingAll(num value) { return Padding( padding: REdgeInsets.all(value.toDouble()), child: this, ); } // add horizontal padding Widget addPaddingHorizontal(num value) { return Padding( padding: REdgeInsets.symmetric(horizontal: value.toDouble()), child: this, ); } // add vertical padding Widget addPaddingVertical(num value) { return Padding( padding: REdgeInsets.symmetric(vertical: value.toDouble()), child: this, ); } Widget alignCenterLeft() { return Align(alignment: Alignment.centerLeft, child: this); } Widget alignCenterRight() { return Align(alignment: Alignment.centerRight, child: this); } Widget alignCenter() { return Align(alignment: Alignment.center, child: this); } } //bool extension BoolExtensions on bool { bool get not => !this; } //object extension ObjectExtension on Object? { bool isNullObject() => this == null; bool isNotNull() => isNullObject().not; bool isNullOrEmpty() { if (this is String) { return (isNullObject() || (this as String).isEmpty); } else if (this is List) { return (isNullObject() || (this as List).isEmpty); } else { return isNullObject(); } } bool isNotNullOrEmpty() => isNullOrEmpty().not; } 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)); } /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`). String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}' '${alpha.toRadixString(16).padLeft(2, '0')}' '${red.toRadixString(16).padLeft(2, '0')}' '${green.toRadixString(16).padLeft(2, '0')}' '${blue.toRadixString(16).padLeft(2, '0')}'; } extension TimeOfDayChecker on TimeOfDay { bool isBefore(TimeOfDay other) { // print("now: $hour:$minute"); // print("other: ${other.hour}:${other.minute}"); // final int minutes1 = hour * 60 + minute; // final int minutes2 = other.hour * 60 + other.minute; // // return minutes1 < minutes2; print("isBefore"); print("now: $hour:$minute"); print("other: ${other.hour}:${other.minute}"); final n = DateTime.now(); final d1 = DateTime(n.year, n.month, n.day, hour, minute); final d2 = DateTime(n.year, n.month, n.day, other.hour, other.minute); // final int minutes1 = hour * 60 + minute; // final int minutes2 = other.hour * 60 + other.minute; return d1.isBefore(d2); } bool isAfter(TimeOfDay other) { print("isAfter"); print("now: $hour:$minute"); print("other: ${other.hour}:${other.minute}"); final n = DateTime.now(); final d1 = DateTime(n.year, n.month, n.day, hour, minute); final d2 = DateTime(n.year, n.month, n.day, other.hour, other.minute); // final int minutes1 = hour * 60 + minute; // final int minutes2 = other.hour * 60 + other.minute; return d1.isAfter(d2); } } extension FutureExt on Future { Future showLoader() { return Get.showOverlay( asyncFunction: () async => this, opacity: 1, opacityColor: Colors.black.withOpacity(0.5), loadingWidget: const LoadingWidget(), ); } }