1. class Duration implements Comparable<Duration> {
    2. static const int microsecondsPerMillisecond = 1000;
    3. static const int millisecondsPerSecond = 1000;
    4. static const int secondsPerMinute = 60;
    5. static const int minutesPerHour = 60;
    6. static const int hoursPerDay = 24;
    7. static const int microsecondsPerSecond =
    8. microsecondsPerMillisecond * millisecondsPerSecond;
    9. static const int microsecondsPerMinute =
    10. microsecondsPerSecond * secondsPerMinute;
    11. static const int microsecondsPerHour = microsecondsPerMinute * minutesPerHour;
    12. static const int microsecondsPerDay = microsecondsPerHour * hoursPerDay;
    13. static const int millisecondsPerMinute =
    14. millisecondsPerSecond * secondsPerMinute;
    15. static const int millisecondsPerHour = millisecondsPerMinute * minutesPerHour;
    16. static const int millisecondsPerDay = millisecondsPerHour * hoursPerDay;
    17. static const int secondsPerHour = secondsPerMinute * minutesPerHour;
    18. static const int secondsPerDay = secondsPerHour * hoursPerDay;
    19. static const int minutesPerDay = minutesPerHour * hoursPerDay;
    20. static const Duration zero = Duration(seconds: 0);
    21. final int _duration;
    22. const Duration(
    23. {int days = 0,
    24. int hours = 0,
    25. int minutes = 0,
    26. int seconds = 0,
    27. int milliseconds = 0,
    28. int microseconds = 0})
    29. : this._microseconds(microsecondsPerDay * days +
    30. microsecondsPerHour * hours +
    31. microsecondsPerMinute * minutes +
    32. microsecondsPerSecond * seconds +
    33. microsecondsPerMillisecond * milliseconds +
    34. microseconds);
    35. const Duration._microseconds(this._duration);
    36. Duration operator +(Duration other) {
    37. return Duration._microseconds(_duration + other._duration);
    38. }
    39. Duration operator -(Duration other) {
    40. return Duration._microseconds(_duration - other._duration);
    41. }
    42. Duration operator *(num factor) {
    43. return Duration._microseconds((_duration * factor).round());
    44. }
    45. Duration operator ~/(int quotient) {
    46. if (quotient == 0) throw IntegerDivisionByZeroException();
    47. return Duration._microseconds(_duration ~/ quotient);
    48. }
    49. bool operator <(Duration other) => this._duration < other._duration;
    50. bool operator >(Duration other) => this._duration > other._duration;
    51. bool operator <=(Duration other) => this._duration <= other._duration;
    52. bool operator >=(Duration other) => this._duration >= other._duration;
    53. int get inDays => _duration ~/ Duration.microsecondsPerDay;
    54. int get inHours => _duration ~/ Duration.microsecondsPerHour;
    55. int get inMinutes => _duration ~/ Duration.microsecondsPerMinute;
    56. int get inSeconds => _duration ~/ Duration.microsecondsPerSecond;
    57. int get inMilliseconds => _duration ~/ Duration.microsecondsPerMillisecond;
    58. int get inMicroseconds => _duration;
    59. bool operator ==(Object other) =>
    60. other is Duration && _duration == other.inMicroseconds;
    61. int get hashCode => _duration.hashCode;
    62. int compareTo(Duration other) => _duration.compareTo(other._duration);
    63. String toString() {
    64. String sixDigits(int n) {
    65. if (n >= 100000) return "$n";
    66. if (n >= 10000) return "0$n";
    67. if (n >= 1000) return "00$n";
    68. if (n >= 100) return "000$n";
    69. if (n >= 10) return "0000$n";
    70. return "00000$n";
    71. }
    72. String twoDigits(int n) {
    73. if (n >= 10) return "$n";
    74. return "0$n";
    75. }
    76. if (inMicroseconds < 0) { return "-${-this}"; }
    77. String twoDigitMinutes =
    78. twoDigits(inMinutes.remainder(minutesPerHour) as int);
    79. String twoDigitSeconds =
    80. twoDigits(inSeconds.remainder(secondsPerMinute) as int);
    81. String sixDigitUs =
    82. sixDigits(inMicroseconds.remainder(microsecondsPerSecond) as int);
    83. return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
    84. }
    85. bool get isNegative => _duration < 0;
    86. Duration abs() => Duration._microseconds(_duration.abs());
    87. Duration operator -() => Duration._microseconds(0 - _duration);
    88. }
    89. main(List<String> args) {
    90. print(Duration(milliseconds: -1));
    91. }
    1. @immutable
    2. class Permission {
    3. const Permission._(this.value);
    4. factory Permission.byValue(int value) => values[value];
    5. final int value;
    6. static const calendar = Permission._(0);
    7. static const camera = Permission._(1);
    8. static const contacts = Permission._(2);
    9. static const location = PermissionWithService._(3);
    10. static const locationAlways = PermissionWithService._(4);
    11. static const locationWhenInUse = PermissionWithService._(5);
    12. static const mediaLibrary = Permission._(6);
    13. static const microphone = Permission._(7);
    14. static const phone = PermissionWithService._(8);
    15. static const photos = Permission._(9);
    16. static const photosAddOnly = Permission._(10);
    17. static const reminders = Permission._(11);
    18. static const sensors = Permission._(12);
    19. static const sms = Permission._(13);
    20. static const speech = Permission._(14);
    21. static const storage = Permission._(15);
    22. static const ignoreBatteryOptimizations = Permission._(16);
    23. static const notification = Permission._(17);
    24. static const accessMediaLocation = Permission._(18);
    25. static const activityRecognition = Permission._(19);
    26. static const unknown = Permission._(20);
    27. static const bluetooth = Permission._(21);
    28. static const manageExternalStorage = Permission._(22);
    29. static const systemAlertWindow = Permission._(23);
    30. static const requestInstallPackages = Permission._(24);
    31. static const appTrackingTransparency = Permission._(25);
    32. static const criticalAlerts = Permission._(26);
    33. static const accessNotificationPolicy = Permission._(27);
    34. static const bluetoothScan = Permission._(28);
    35. static const bluetoothAdvertise = Permission._(29);
    36. static const bluetoothConnect = Permission._(30);
    37. static const List<Permission> values = <Permission>[
    38. calendar,
    39. camera,
    40. contacts,
    41. location,
    42. locationAlways,
    43. locationWhenInUse,
    44. mediaLibrary,
    45. microphone,
    46. phone,
    47. photos,
    48. photosAddOnly,
    49. reminders,
    50. sensors,
    51. sms,
    52. speech,
    53. storage,
    54. ignoreBatteryOptimizations,
    55. notification,
    56. accessMediaLocation,
    57. activityRecognition,
    58. unknown,
    59. bluetooth,
    60. manageExternalStorage,
    61. systemAlertWindow,
    62. requestInstallPackages,
    63. appTrackingTransparency,
    64. criticalAlerts,
    65. accessNotificationPolicy,
    66. bluetoothScan,
    67. bluetoothAdvertise,
    68. bluetoothConnect,
    69. ];
    70. static const List<String> _names = <String>[
    71. 'calendar',
    72. 'camera',
    73. 'contacts',
    74. 'location',
    75. 'locationAlways',
    76. 'locationWhenInUse',
    77. 'mediaLibrary',
    78. 'microphone',
    79. 'phone',
    80. 'photos',
    81. 'photosAddOnly',
    82. 'reminders',
    83. 'sensors',
    84. 'sms',
    85. 'speech',
    86. 'storage',
    87. 'ignoreBatteryOptimizations',
    88. 'notification',
    89. 'access_media_location',
    90. 'activity_recognition',
    91. 'unknown',
    92. 'bluetooth',
    93. 'manageExternalStorage',
    94. 'systemAlertWindow',
    95. 'requestInstallPackages',
    96. 'appTrackingTransparency',
    97. 'criticalAlerts',
    98. 'accessNotificationPolicy',
    99. 'bluetoothScan',
    100. 'bluetoothAdvertise',
    101. 'bluetoothConnect',
    102. ];
    103. @override
    104. String toString() => 'Permission.${_names[value]}';
    105. @override
    106. bool operator ==(Object other) {
    107. if (identical(this, other)) {
    108. return true;
    109. }
    110. if (other.runtimeType != runtimeType) {
    111. return false;
    112. }
    113. return other is Permission && other.value == value;
    114. }
    115. @override
    116. int get hashCode => value.hashCode;
    117. }