move_block.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_2048/views/number_text.dart';
  4. import 'package:flutter_2048/views/block/base_block.dart';
  5. import 'package:flutter_2048/model/block_info.dart';
  6. double getBegin(BlockInfo info, int mode) {
  7. return (info.current % mode == info.before % mode
  8. ? info.before ~/ mode - info.current ~/ mode
  9. : info.before % mode - info.current % mode) *
  10. 1.0;
  11. }
  12. class MoveBlock extends BaseBlock {
  13. final BlockInfo info;
  14. final int mode;
  15. MoveBlock({
  16. Key? key,
  17. required this.info,
  18. required this.mode,
  19. required AnimationController controller,
  20. }) : super(
  21. key: key,
  22. animation: Tween<double>(begin: getBegin(info, mode), end: 0)
  23. .animate(controller),
  24. );
  25. @override
  26. Widget buildBlock(BuildContext context, BlockProps props) {
  27. Animation<double> animation = listenable as Animation<double>;
  28. var direction = info.current % mode == info.before % mode ? 1 : 0;
  29. return Positioned(
  30. top:
  31. (info.current ~/ props.mode) * (props.blockWidth + props.borderWidth),
  32. left:
  33. (info.current % props.mode) * (props.blockWidth + props.borderWidth),
  34. child: Transform.translate(
  35. offset: direction == 0
  36. ? Offset(
  37. animation.value * (props.blockWidth + props.borderWidth), 0)
  38. : Offset(
  39. 0, animation.value * (props.blockWidth + props.borderWidth)),
  40. child: NumberText(value: this.info.value, size: props.blockWidth),
  41. ),
  42. );
  43. }
  44. }
  45. void getDirection(double current, double before) {}