playground.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ? Center(
  31. child: GestureDetector(
  32. onHorizontalDragStart: (evt) => onDragStart(evt, props),
  33. onHorizontalDragEnd: (evt) => onHorizontalDragEnd(evt, props),
  34. onVerticalDragStart: (evt) => onDragStart(evt, props),
  35. onVerticalDragEnd: (evt) => onVerticalDragEnd(evt, props),
  36. child: Container(
  37. color: Colors.transparent,
  38. height: Screen.stageWidth,
  39. ),
  40. ),
  41. )
  42. : Center(child: Container(
  43. decoration: BoxDecoration(
  44. borderRadius: BorderRadius.circular(5),
  45. color: Color.fromRGBO(255, 255, 255, 0.4)),
  46. height: Screen.stageWidth,
  47. child: Center(
  48. child: Text(
  49. 'Game Over',
  50. style: TextStyle(
  51. fontSize: 50,
  52. color: Color(0xff776e65),
  53. fontWeight: FontWeight.bold,
  54. ),
  55. ),
  56. ),
  57. ),);
  58. },
  59. );
  60. }
  61. void onDragStart(DragStartDetails evt, PlaygroundProps props) {
  62. props.startTime = DateTime.now().millisecondsSinceEpoch;
  63. }
  64. void onHorizontalDragEnd(DragEndDetails evt, PlaygroundProps props) {
  65. if (DateTime.now().millisecondsSinceEpoch - props.startTime >
  66. pressTimeout ||
  67. evt.primaryVelocity.abs() < dragLength) return;
  68. if (evt.primaryVelocity > 0) {
  69. props.onRight();
  70. } else {
  71. props.onLeft();
  72. }
  73. }
  74. void onVerticalDragEnd(DragEndDetails evt, PlaygroundProps props) {
  75. if (DateTime.now().millisecondsSinceEpoch - props.startTime >
  76. pressTimeout ||
  77. evt.primaryVelocity.abs() < dragLength) return;
  78. // 是否ios和android纵轴是相反的?
  79. if (evt.primaryVelocity < 0) {
  80. props.onUp();
  81. } else {
  82. props.onDown();
  83. }
  84. }
  85. }
  86. class PlaygroundProps {
  87. int mode;
  88. bool end;
  89. int startTime;
  90. Function onLeft;
  91. Function onRight;
  92. Function onUp;
  93. Function onDown;
  94. PlaygroundProps({
  95. this.end,
  96. this.mode,
  97. this.startTime,
  98. this.onDown,
  99. this.onLeft,
  100. this.onRight,
  101. this.onUp,
  102. });
  103. }