first commit
This commit is contained in:
21
lib/components/extensions/context.dart
Normal file
21
lib/components/extensions/context.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// Flutter imports:
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// Подключение сторонней библиотеки
|
||||
/// Сделано для упрощения чтения импортов
|
||||
export 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
|
||||
///
|
||||
/// Маштабирование в зависимости от контекста
|
||||
///
|
||||
extension ScaleFromContext on BuildContext {
|
||||
///
|
||||
/// Screen Width
|
||||
///
|
||||
double get sw => MediaQuery.of(this).size.width;
|
||||
|
||||
///
|
||||
/// Screen Height
|
||||
///
|
||||
double get sh => MediaQuery.of(this).size.height;
|
||||
}
|
||||
21
lib/components/extensions/duration_extensions.dart
Normal file
21
lib/components/extensions/duration_extensions.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
///
|
||||
/// Расширение для работы с [Duration]
|
||||
///
|
||||
extension AppDuration on Duration {
|
||||
///
|
||||
/// Получение длительности в формате mm:ss
|
||||
///
|
||||
String get mmss => hhmmss.substring('00:'.length);
|
||||
|
||||
///
|
||||
/// Получение длительности в формате HH:mm:ss
|
||||
///
|
||||
String get hhmmss {
|
||||
String twoDigits(int n) => n.toString().padLeft(2, '0');
|
||||
|
||||
String twoDigitMinutes = twoDigits(inMinutes.remainder(60));
|
||||
String twoDigitSeconds = twoDigits(inSeconds.remainder(60));
|
||||
|
||||
return '${twoDigits(inHours)}:$twoDigitMinutes:$twoDigitSeconds';
|
||||
}
|
||||
}
|
||||
26
lib/components/extensions/state.dart
Normal file
26
lib/components/extensions/state.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
// Dart imports:
|
||||
import 'dart:async';
|
||||
|
||||
// Flutter imports:
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
extension StateExtension on State {
|
||||
/// [setState] when it's not building, then wait until next frame built.
|
||||
FutureOr<void> safeSetState(FutureOr<dynamic> Function() fn) async {
|
||||
await fn();
|
||||
if (mounted &&
|
||||
!context.debugDoingBuild &&
|
||||
context.owner?.debugBuilding == false) {
|
||||
// ignore: invalid_use_of_protected_member, no-empty-block
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
final Completer<void> completer = Completer<void>();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
completer.complete();
|
||||
});
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
}
|
||||
31
lib/components/extensions/string.dart
Normal file
31
lib/components/extensions/string.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
// Flutter imports:
|
||||
import 'package:flutter/material.dart' show Color;
|
||||
|
||||
///
|
||||
/// Расширение для работы со строками
|
||||
///
|
||||
extension MString on String {
|
||||
///
|
||||
/// Слово с заглавной буквы
|
||||
///
|
||||
String capitalyze() {
|
||||
final String str = toLowerCase();
|
||||
final String first = str.substring(0, 1);
|
||||
|
||||
return '${first.toUpperCase()}${str.substring(1)}';
|
||||
}
|
||||
|
||||
///
|
||||
/// Парсинг hex string в color
|
||||
///
|
||||
Color get toColor {
|
||||
String res = replaceFirst('#', '');
|
||||
|
||||
return Color(
|
||||
int.parse(
|
||||
res.length == 8 ? res : (res.length == 6 ? 'FF$res' : res),
|
||||
radix: 16,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
21
lib/components/extensions/theme_mode.dart
Normal file
21
lib/components/extensions/theme_mode.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// Flutter imports:
|
||||
import 'package:flutter/material.dart' show ThemeMode;
|
||||
|
||||
/// Расширение [ThemeMode]
|
||||
extension ThemModeExtension on ThemeMode {
|
||||
///
|
||||
/// Получение инверсивного [ThemeMode]
|
||||
///
|
||||
ThemeMode get inversed {
|
||||
switch (this) {
|
||||
case ThemeMode.system:
|
||||
return ThemeMode.light;
|
||||
|
||||
case ThemeMode.light:
|
||||
return ThemeMode.dark;
|
||||
|
||||
case ThemeMode.dark:
|
||||
return ThemeMode.light;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user