game_page.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'package:battle/button/button_controller.dart';
  2. import 'package:battle/data/card.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:widget/widget.dart';
  5. import '../button/button_painter.dart';
  6. import 'game/count_second.dart';
  7. import 'summary_page.dart';
  8. import 'view/count_down_dialog.dart';
  9. class GamePage extends StatefulWidget {
  10. final CardItem card;
  11. const GamePage({
  12. super.key,
  13. required this.card,
  14. });
  15. @override
  16. State<GamePage> createState() => _GamePageState();
  17. }
  18. class _GamePageState extends State<GamePage> with SingleTickerProviderStateMixin {
  19. late GameController _controller;
  20. @override
  21. void initState() {
  22. super.initState();
  23. _controller = GameController(widget.card, vsync: this);
  24. _controller.addStatusListener(_listenStatus);
  25. }
  26. void _listenStatus(GameStatus status) {
  27. if (status == GameStatus.end) {
  28. if (mounted) {
  29. showScore(context, score: _controller.score, time: _controller.count);
  30. }
  31. }
  32. }
  33. @override
  34. void dispose() {
  35. _controller.clearStatusListener();
  36. _controller.dispose();
  37. super.dispose();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return Scaffold(
  42. body: Stack(
  43. children: [
  44. Positioned.fill(
  45. child: ColorFiltered(
  46. colorFilter: const ColorFilter.matrix(<double>[
  47. 0.299, 0.587, 0.114, 0, 0, //r
  48. 0.299, 0.587, 0.114, 0, 0, //g
  49. 0.299, 0.587, 0.114, 0, 0, //b
  50. 0, 0, 0, 0.1, 0, //a
  51. ]),
  52. child: Image.asset(
  53. 'assets/images/background.png',
  54. repeat: ImageRepeat.repeat,
  55. cacheWidth: 182,
  56. cacheHeight: 52,
  57. ),
  58. ),
  59. ),
  60. Positioned.fill(
  61. child: SafeArea(
  62. top: true,
  63. bottom: true,
  64. child: Column(
  65. mainAxisSize: MainAxisSize.min,
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. children: [
  68. Padding(
  69. padding: const EdgeInsets.symmetric(horizontal: 16),
  70. child: Align(
  71. alignment: Alignment.centerRight,
  72. child: AnimatedBuilder(
  73. animation: _controller,
  74. builder: (_, value) {
  75. return CountSecond(second: Duration(seconds: _controller.count).inSeconds);
  76. },
  77. ),
  78. ),
  79. ),
  80. const SizedBox(height: 16),
  81. Padding(
  82. padding: const EdgeInsets.symmetric(horizontal: 40),
  83. child: BattleBoard(
  84. card: ClipRRect(
  85. borderRadius: BorderRadius.circular(8),
  86. child: Container(
  87. width: double.infinity,
  88. height: double.infinity,
  89. color: Colors.grey,
  90. child: Image.asset(
  91. widget.card.assetPath,
  92. fit: BoxFit.cover,
  93. ),
  94. ),
  95. ),
  96. button: CustomPaint(
  97. painter: ButtonPainter(controller: _controller),
  98. child: const SizedBox.expand(),
  99. ),
  100. ),
  101. ),
  102. const SizedBox(height: 32),
  103. Padding(
  104. padding: const EdgeInsets.symmetric(horizontal: 16),
  105. child: ButtonBar(
  106. alignment: MainAxisAlignment.spaceAround,
  107. children: [
  108. FilledButton(
  109. style: const ButtonStyle(
  110. fixedSize: WidgetStatePropertyAll<Size>(Size(180, 66)),
  111. ),
  112. onPressed: () {
  113. _controller.stop();
  114. showCountDown(context).then((_) {
  115. _controller.startOrReset();
  116. });
  117. },
  118. child: const Text(
  119. '开始/重新开始',
  120. style: TextStyle(
  121. fontSize: 18,
  122. ),
  123. ),
  124. ),
  125. ],
  126. ),
  127. ),
  128. ],
  129. ),
  130. ),
  131. ),
  132. ],
  133. ),
  134. );
  135. }
  136. }