screen.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:tetris/gamer/gamer.dart';
  4. import 'package:tetris/material/briks.dart';
  5. import 'package:tetris/material/material.dart';
  6. import 'package:vector_math/vector_math_64.dart' as v;
  7. import 'player_panel.dart';
  8. import 'status_panel.dart';
  9. const Color screenBackground = Color(0xff9ead86);
  10. /// screen H : W;
  11. class Screen extends StatelessWidget {
  12. ///the with of screen
  13. final double width;
  14. const Screen({
  15. Key? key,
  16. required this.width,
  17. }) : super(key: key);
  18. const Screen.fromHeight(double height)
  19. : this(width: ((height - 6) / 2 + 6) / 0.6);
  20. @override
  21. Widget build(BuildContext context) {
  22. //play panel need 60%
  23. final playerPanelWidth = width * 0.6;
  24. return Shake(
  25. shake: GameState.of(context).states == GameStates.drop,
  26. child: SizedBox(
  27. height: (playerPanelWidth - 6) * 2 + 6,
  28. width: width,
  29. child: Container(
  30. color: screenBackground,
  31. child: GameMaterial(
  32. child: BrikSize(
  33. size: getBrikSizeForScreenWidth(playerPanelWidth),
  34. child: Row(
  35. children: <Widget>[
  36. PlayerPanel(width: playerPanelWidth),
  37. SizedBox(
  38. width: width - playerPanelWidth,
  39. child: const StatusPanel(),
  40. )
  41. ],
  42. ),
  43. ),
  44. ),
  45. ),
  46. ),
  47. );
  48. }
  49. }
  50. class Shake extends StatefulWidget {
  51. final Widget child;
  52. ///true to shake screen vertically
  53. final bool shake;
  54. const Shake({
  55. Key? key,
  56. required this.child,
  57. required this.shake,
  58. }) : super(key: key);
  59. @override
  60. State<Shake> createState() => _ShakeState();
  61. }
  62. ///摇晃屏幕
  63. class _ShakeState extends State<Shake> with TickerProviderStateMixin {
  64. late AnimationController _controller;
  65. @override
  66. void initState() {
  67. _controller = AnimationController(
  68. vsync: this, duration: const Duration(milliseconds: 150))
  69. ..addListener(() {
  70. setState(() {});
  71. });
  72. super.initState();
  73. }
  74. @override
  75. void didUpdateWidget(Shake oldWidget) {
  76. super.didUpdateWidget(oldWidget);
  77. if (widget.shake) {
  78. _controller.forward(from: 0);
  79. }
  80. }
  81. @override
  82. void dispose() {
  83. _controller.dispose();
  84. super.dispose();
  85. }
  86. v.Vector3 _getTranslation() {
  87. double progress = _controller.value;
  88. double offset = sin(progress * pi) * 1.5;
  89. return v.Vector3(0, offset, 0.0);
  90. }
  91. @override
  92. Widget build(BuildContext context) {
  93. return Transform(
  94. transform: Matrix4.translation(_getTranslation()),
  95. child: widget.child,
  96. );
  97. }
  98. }