playground.dart 3.3 KB

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