Files
nearle_pos/lib/core/utils/extensions.dart
2026-07-29 11:41:53 +05:30

60 lines
1.8 KiB
Dart

import 'package:flutter/material.dart';
import '../theme/app_dimens.dart';
extension BuildContextX on BuildContext {
ThemeData get theme => Theme.of(this);
TextTheme get text => Theme.of(this).textTheme;
ColorScheme get colors => Theme.of(this).colorScheme;
Size get screen => MediaQuery.sizeOf(this);
double get screenWidth => MediaQuery.sizeOf(this).width;
double get screenHeight => MediaQuery.sizeOf(this).height;
bool get isCompact => AppBreakpoints.isCompact(screenWidth);
bool get isMedium => AppBreakpoints.isMedium(screenWidth);
bool get isExpanded => AppBreakpoints.isExpanded(screenWidth);
/// Picks a value appropriate to the current breakpoint.
T responsive<T>({required T compact, T? medium, required T expanded}) {
if (isCompact) return compact;
if (isExpanded) return expanded;
return medium ?? expanded;
}
void showSnack(String message, {Color? background}) {
ScaffoldMessenger.of(this)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(
content: Text(message),
backgroundColor: background,
duration: const Duration(seconds: 2),
));
}
}
extension DoubleX on double {
/// Rounds to two decimals to avoid floating point drift in money maths.
double get asMoney => (this * 100).roundToDouble() / 100;
/// Indian retail round-off to the nearest rupee.
double get roundedToRupee => roundToDouble();
}
extension StringX on String {
String get capitalized =>
isEmpty ? this : '${this[0].toUpperCase()}${substring(1)}';
bool containsIgnoreCase(String other) =>
toLowerCase().contains(other.toLowerCase());
}
extension IterableX<T> on Iterable<T> {
T? firstWhereOrNull(bool Function(T) test) {
for (final element in this) {
if (test(element)) return element;
}
return null;
}
}