exts.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. extension MapExt<K, V> on Map<K, V> {
  3. T? getCheck<T>(K? key) {
  4. if (key == null || !containsKey(key)) {
  5. return null;
  6. }
  7. var value = this[key];
  8. if (value is! T) {
  9. return null;
  10. }
  11. return value as T?;
  12. }
  13. }
  14. extension StringExt on String? {
  15. bool get isNullOrEmpty {
  16. return this == null || this!.isEmpty;
  17. }
  18. }
  19. extension ListExt<T> on List<T>? {
  20. bool get isNullOrEmpty {
  21. return this == null || this!.isEmpty;
  22. }
  23. }
  24. extension DateTimeExt on DateTime {
  25. String get yyyyMMddHHmmss =>
  26. "$year-${month < 10 ? "$month".padLeft(2, "0") : month}-$day ${hour < 10 ? "$hour".padLeft(2, "0") : hour}:${minute < 10 ? "$minute".padLeft(2, "0") : minute}:${second < 10 ? "$second".padLeft(2, "0") : second}";
  27. }
  28. extension ContextExt on BuildContext {
  29. ThemeData get theme => Theme.of(this);
  30. TextTheme get textTheme => theme.textTheme;
  31. TextStyle? get titleLarge => textTheme.titleLarge;
  32. TextStyle? get titleMedium => textTheme.titleMedium;
  33. TextStyle? get titleSmall => textTheme.titleSmall;
  34. TextStyle? get bodyLarge => textTheme.bodyLarge;
  35. TextStyle? get bodyMedium => textTheme.bodyMedium;
  36. TextStyle? get bodySmall => textTheme.bodySmall;
  37. ColorScheme get colorScheme => theme.colorScheme;
  38. Color get primaryColor => colorScheme.primary;
  39. Color get onPrimaryColor => colorScheme.onPrimary;
  40. Color get surfaceContainerHighColor => colorScheme.surfaceContainerHigh;
  41. Color get onSurfaceVariantColor => colorScheme.onSurfaceVariant;
  42. Color get onSurfaceColor => colorScheme.onSurface;
  43. }