status_panel.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:tetris/gamer/block.dart';
  4. import 'package:tetris/gamer/gamer.dart';
  5. import 'package:tetris/generated/l10n.dart';
  6. import 'package:tetris/material/briks.dart';
  7. import 'package:tetris/material/images.dart';
  8. class StatusPanel extends StatelessWidget {
  9. const StatusPanel({super.key});
  10. @override
  11. Widget build(BuildContext context) {
  12. return Container(
  13. padding: const EdgeInsets.all(8),
  14. child: Column(
  15. crossAxisAlignment: CrossAxisAlignment.start,
  16. children: <Widget>[
  17. Text(S.of(context).points,
  18. style: const TextStyle(fontWeight: FontWeight.bold)),
  19. const SizedBox(height: 4),
  20. Number(number: GameState.of(context).points),
  21. const SizedBox(height: 10),
  22. Text(S.of(context).cleans,
  23. style: const TextStyle(fontWeight: FontWeight.bold)),
  24. const SizedBox(height: 4),
  25. Number(number: GameState.of(context).cleared),
  26. const SizedBox(height: 10),
  27. Text(S.of(context).level,
  28. style: const TextStyle(fontWeight: FontWeight.bold)),
  29. const SizedBox(height: 4),
  30. Number(number: GameState.of(context).level),
  31. const SizedBox(height: 10),
  32. Text(S.of(context).next,
  33. style: const TextStyle(fontWeight: FontWeight.bold)),
  34. const SizedBox(height: 4),
  35. _NextBlock(),
  36. const Spacer(),
  37. _GameStatus(),
  38. ],
  39. ),
  40. );
  41. }
  42. }
  43. class _NextBlock extends StatelessWidget {
  44. @override
  45. Widget build(BuildContext context) {
  46. List<List<int>> data = [List.filled(4, 0), List.filled(4, 0)];
  47. final next = blockShapes[GameState.of(context).next.type]!;
  48. for (int i = 0; i < next.length; i++) {
  49. for (int j = 0; j < next[i].length; j++) {
  50. data[i][j] = next[i][j];
  51. }
  52. }
  53. return Column(
  54. children: data.map((list) {
  55. return Row(
  56. children: list.map((b) {
  57. return b == 1 ? const Brik.normal() : const Brik.empty();
  58. }).toList(),
  59. );
  60. }).toList(),
  61. );
  62. }
  63. }
  64. class _GameStatus extends StatefulWidget {
  65. @override
  66. _GameStatusState createState() {
  67. return _GameStatusState();
  68. }
  69. }
  70. class _GameStatusState extends State<_GameStatus> {
  71. Timer? _timer;
  72. bool _colonEnable = true;
  73. int _minute = 0;
  74. int _hour = 0;
  75. @override
  76. void initState() {
  77. super.initState();
  78. _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
  79. final now = DateTime.now();
  80. setState(() {
  81. _colonEnable = !_colonEnable;
  82. _minute = now.minute;
  83. _hour = now.hour;
  84. });
  85. });
  86. }
  87. @override
  88. void dispose() {
  89. _timer?.cancel();
  90. super.dispose();
  91. }
  92. @override
  93. Widget build(BuildContext context) {
  94. return Row(
  95. children: <Widget>[
  96. IconSound(enable: GameState.of(context).muted),
  97. const SizedBox(width: 4),
  98. IconPause(enable: GameState.of(context).states == GameStates.paused),
  99. const Spacer(),
  100. Number(number: _hour, length: 2, padWithZero: true),
  101. IconColon(enable: _colonEnable),
  102. Number(number: _minute, length: 2, padWithZero: true),
  103. ],
  104. );
  105. }
  106. }