block_factory.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import 'package:flutter/widgets.dart';
  2. import 'package:flutter_2048/views/block/combin_block.dart';
  3. import 'package:flutter_2048/views/block/move_block.dart';
  4. import 'package:flutter_2048/views/block/new_block.dart';
  5. import 'package:flutter_2048/views/block/static_block.dart';
  6. import 'package:flutter_2048/model/block_info.dart';
  7. class BlockFactory {
  8. late AnimationController combinController;
  9. late AnimationController addController;
  10. late AnimationController moveController;
  11. int _mode = 0;
  12. BlockFactory(TickerProvider provider, int mode) {
  13. combinController = AnimationController(
  14. duration: const Duration(milliseconds: 60), vsync: provider);
  15. addController = AnimationController(
  16. duration: const Duration(milliseconds: 80), vsync: provider);
  17. moveController = AnimationController(
  18. duration: const Duration(milliseconds: 95), vsync: provider);
  19. _mode = mode;
  20. }
  21. Widget create(BlockInfo info) {
  22. if (info.myis) {
  23. return NewBlock(
  24. info: info,
  25. controller: this.addController,
  26. );
  27. }
  28. if (info.needMove && info.needCombine) {
  29. return CombinBlock(
  30. info: info,
  31. mode: _mode,
  32. combinController: combinController,
  33. moveController: moveController,
  34. );
  35. }
  36. if (info.needMove && info.needCombine != true) {
  37. return MoveBlock(info: info, mode: _mode, controller: moveController);
  38. }
  39. return StaticBlock(
  40. info: info,
  41. controller: this.addController,
  42. );
  43. }
  44. play() {
  45. moveController.forward().whenComplete(() {
  46. addController.forward();
  47. combinController.forward().whenComplete(() {
  48. combinController.reverse();
  49. });
  50. });
  51. }
  52. }