|
| 1 | +import 'dart:collection'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +/// Created by NieBin on 2020-03-26 |
| 6 | +/// Github: https://github.com/nb312 |
| 7 | +/// Email: niebin312@gmail.com |
| 8 | +abstract class Clock { |
| 9 | + Color color; |
| 10 | + |
| 11 | + Widget display(); |
| 12 | +} |
| 13 | + |
| 14 | +class PinkClock implements Clock { |
| 15 | + @override |
| 16 | + Color color = Colors.pink; |
| 17 | + |
| 18 | + @override |
| 19 | + Widget display() => Container( |
| 20 | + width: 100, |
| 21 | + height: 100, |
| 22 | + decoration: BoxDecoration( |
| 23 | + shape: BoxShape.rectangle, |
| 24 | + border: Border.all(color: Colors.green), |
| 25 | + color: color, |
| 26 | + ), |
| 27 | + child: Center(child: Text(DateTime.now().toIso8601String())), |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +class GreenClock implements Clock { |
| 32 | + @override |
| 33 | + Color color = Colors.green; |
| 34 | + |
| 35 | + @override |
| 36 | + Widget display() => Container( |
| 37 | + width: 200, |
| 38 | + height: 100, |
| 39 | + decoration: BoxDecoration(shape: BoxShape.circle, color: color), |
| 40 | + child: Center(child: Text(DateTime.now().toIso8601String())), |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +class BlueClock implements Clock { |
| 45 | + @override |
| 46 | + Color color = Colors.blue; |
| 47 | + |
| 48 | + @override |
| 49 | + Widget display() => Container( |
| 50 | + width: 150, |
| 51 | + height: 150, |
| 52 | + decoration: BoxDecoration(shape: BoxShape.circle, color: color), |
| 53 | + child: Center(child: Text(DateTime.now().toIso8601String())), |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +class WhiteClock implements Clock { |
| 58 | + @override |
| 59 | + Color color = Colors.white; |
| 60 | + |
| 61 | + @override |
| 62 | + Widget display() => Container( |
| 63 | + width: 100, |
| 64 | + height: 100, |
| 65 | + decoration: BoxDecoration( |
| 66 | + shape: BoxShape.rectangle, |
| 67 | + color: color, |
| 68 | + border: Border.all(color: Colors.orangeAccent)), |
| 69 | + child: Center(child: Text(DateTime.now().toIso8601String())), |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +class ClockManager { |
| 74 | + static Map<String, Clock> _clocks = HashMap(); |
| 75 | + |
| 76 | + static Clock getColorClock(String color) { |
| 77 | + if (_clocks.containsKey(color)) { |
| 78 | + return _clocks[color]; |
| 79 | + } else { |
| 80 | + _clocks.putIfAbsent(color, () { |
| 81 | + if (color.toLowerCase() == "pink") { |
| 82 | + return PinkClock(); |
| 83 | + } else if (color.toLowerCase() == "green") { |
| 84 | + return GreenClock(); |
| 85 | + } else if (color.toLowerCase() == "blue") { |
| 86 | + return BlueClock(); |
| 87 | + } else { |
| 88 | + return WhiteClock(); |
| 89 | + } |
| 90 | + }); |
| 91 | + return _clocks[color]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + static Clock removeClock(String color) { |
| 96 | + if (_clocks.containsKey(color)) { |
| 97 | + return _clocks.remove(color); |
| 98 | + } else { |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + static void clearAll() { |
| 104 | + _clocks.clear(); |
| 105 | + } |
| 106 | +} |
0 commit comments