screen.dart 2.6 KB

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