Files
flutter_server_box/lib/core/extension/stringx.dart
2024-03-25 15:03:39 +08:00

37 lines
888 B
Dart

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
extension StringX on String {
/// Format: `#8b2252` or `8b2252`
Color? get hexToColor {
final hexCode = replaceAll('#', '');
final val = int.tryParse('FF$hexCode', radix: 16);
if (val == null) {
return null;
}
return Color(val);
}
Uint8List get uint8List => Uint8List.fromList(utf8.encode(this));
/// Upper the first letter.
String get upperFirst {
if (isEmpty) {
return this;
}
final runes = codeUnits;
if (runes[0] >= 97 && runes[0] <= 122) {
final origin = String.fromCharCode(runes[0]);
final upper = origin.toUpperCase();
return replaceFirst(origin, upper);
}
return this;
}
}
extension StringXX on String? {
String? get selfIfNotNullEmpty => this?.isEmpty == true ? null : this;
}