基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

41 lines
1.3 KiB

  1. /*
  2. * @Author: your name
  3. * @Date: 2020-05-18 14:01:56
  4. * @LastEditTime: 2020-06-24 13:45:26
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /hairuyi_flutter_yunfadan/lib/utils/color.dart
  8. */
  9. import 'dart:ui';
  10. import 'dart:math';
  11. import 'package:flutter/material.dart';
  12. import 'package:zhiying_comm/util/log/let_log.dart';
  13. extension HexColor on Color {
  14. /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  15. static Color fromHex(String hexString) {
  16. try{
  17. final buffer = StringBuffer();
  18. if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
  19. buffer.write(hexString.replaceFirst('#', ''));
  20. return Color(int.parse(buffer.toString(), radix: 16));
  21. }catch(e){
  22. Logger.log(e);
  23. }
  24. return Colors.white;
  25. }
  26. static Color random() {
  27. return Color(Random().nextInt(1 << 32));
  28. }
  29. /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  30. String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
  31. '${alpha.toRadixString(16).padLeft(2, '0')}'
  32. '${red.toRadixString(16).padLeft(2, '0')}'
  33. '${green.toRadixString(16).padLeft(2, '0')}'
  34. '${blue.toRadixString(16).padLeft(2, '0')}';
  35. }