playground.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_redux/flutter_redux.dart';
  4. import '../reducers/moveDown.dart';
  5. import '../reducers/moveLeft.dart';
  6. import '../reducers/moveRight.dart';
  7. import '../reducers/moveUp.dart';
  8. import '../store/game_state.dart';
  9. import '../utils/screen.dart';
  10. const pressTimeout = 200;
  11. const dragLength = 300;
  12. /// 上下左右滑动事件
  13. class Playground extends StatelessWidget {
  14. @override
  15. Widget build(BuildContext context) {
  16. return StoreConnector<GameState, PlaygroundProps>(
  17. converter: (store) {
  18. return PlaygroundProps(
  19. end: store.state.status.end,
  20. mode: store.state.mode,
  21. startTime: 0,
  22. onDown: () => store.dispatch(MoveDownAction()),
  23. onLeft: () => store.dispatch(MoveLeftAction()),
  24. onRight: () => store.dispatch(MoveRightAction()),
  25. onUp: () => store.dispatch(MoveUpAction()),
  26. );
  27. },
  28. builder: (context, props) {
  29. return props.end != true
  30. ? GestureDetector(
  31. onHorizontalDragStart: (evt) => onDragStart(evt, props),
  32. onHorizontalDragEnd: (evt) => onHorizontalDragEnd(evt, props),
  33. onVerticalDragStart: (evt) => onDragStart(evt, props),
  34. onVerticalDragEnd: (evt) => onVerticalDragEnd(evt, props),
  35. child: Container(
  36. color: Colors.transparent,
  37. height: Screen.stageWidth,
  38. ),
  39. )
  40. : Container(
  41. decoration: BoxDecoration(
  42. borderRadius: BorderRadius.circular(5),
  43. color: Color.fromRGBO(255, 255, 255, 0.4)),
  44. height: Screen.stageWidth,
  45. child: Center(
  46. child: Text(
  47. 'Game Over',
  48. style: TextStyle(
  49. fontSize: 50,
  50. color: Color(0xff776e65),
  51. fontWeight: FontWeight.bold,
  52. ),
  53. ),
  54. ),
  55. );
  56. },
  57. );
  58. }
  59. void onDragStart(DragStartDetails evt, PlaygroundProps props) {
  60. props.startTime = DateTime.now().millisecondsSinceEpoch;
  61. }
  62. void onHorizontalDragEnd(DragEndDetails evt, PlaygroundProps props) {
  63. if (DateTime.now().millisecondsSinceEpoch - props.startTime >
  64. pressTimeout ||
  65. evt.primaryVelocity.abs() < dragLength) return;
  66. if (evt.primaryVelocity > 0) {
  67. props.onRight();
  68. } else {
  69. props.onLeft();
  70. }
  71. }
  72. void onVerticalDragEnd(DragEndDetails evt, PlaygroundProps props) {
  73. if (DateTime.now().millisecondsSinceEpoch - props.startTime >
  74. pressTimeout ||
  75. evt.primaryVelocity.abs() < dragLength) return;
  76. // 是否ios和android纵轴是相反的?
  77. if (evt.primaryVelocity < 0) {
  78. props.onUp();
  79. } else {
  80. props.onDown();
  81. }
  82. }
  83. }
  84. class PlaygroundProps {
  85. int mode;
  86. bool end;
  87. int startTime;
  88. Function onLeft;
  89. Function onRight;
  90. Function onUp;
  91. Function onDown;
  92. PlaygroundProps({
  93. this.end,
  94. this.mode,
  95. this.startTime,
  96. this.onDown,
  97. this.onLeft,
  98. this.onRight,
  99. this.onUp,
  100. });
  101. }