number_text.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/material.dart';
  2. class BlockStyle {
  3. Color textColor;
  4. Color background;
  5. BlockStyle({required this.textColor, required this.background});
  6. }
  7. var styles = Map.fromEntries([
  8. MapEntry(2,
  9. BlockStyle(textColor: Color(0xff776e65), background: Color(0xffeee4da))),
  10. MapEntry(4,
  11. BlockStyle(textColor: Color(0xff776e65), background: Color(0xffede0c8))),
  12. MapEntry(8,
  13. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xfff2b179))),
  14. MapEntry(16,
  15. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xfff59563))),
  16. MapEntry(32,
  17. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xfff67c5f))),
  18. MapEntry(64,
  19. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xfff65e3b))),
  20. MapEntry(128,
  21. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xffedcf72))),
  22. MapEntry(256,
  23. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xffedcc61))),
  24. MapEntry(512,
  25. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xffedc850))),
  26. MapEntry(1024,
  27. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xffedc53f))),
  28. MapEntry(2048,
  29. BlockStyle(textColor: Color(0xfff9f6f2), background: Color(0xffedc22e))),
  30. ]);
  31. class NumberText extends StatelessWidget {
  32. final int value;
  33. final double size;
  34. NumberText({required this.value, required this.size});
  35. @override
  36. Widget build(BuildContext context) {
  37. var numberText = this.value.toString();
  38. return Container(
  39. width: size,
  40. height: size,
  41. decoration: BoxDecoration(
  42. color: (this.value ~/ 2048) > 1
  43. ? styles[this.value ~/ 2048]!.background
  44. : styles[this.value]!.background,
  45. border: Border.all(color: Colors.transparent, width: 0),
  46. borderRadius: BorderRadius.circular(5),
  47. ),
  48. child: Center(
  49. child: Text(
  50. numberText,
  51. style: TextStyle(
  52. fontWeight: FontWeight.bold,
  53. color: (this.value ~/ 2048) > 1
  54. ? styles[this.value ~/ 2048]?.textColor
  55. : styles[this.value]?.textColor,
  56. fontSize:
  57. size / (numberText.length <= 2 ? 2 : numberText.length * 0.8),
  58. ),
  59. ),
  60. ),
  61. );
  62. }
  63. }