block_factory.dart 1.7 KB

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