blocks.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_2048/service/block_factory.dart';
  3. import 'package:flutter_2048/model/block_info.dart';
  4. import 'package:flutter_2048/store/game_state.dart';
  5. import 'package:flutter_2048/utils/screen.dart';
  6. import 'package:flutter_redux/flutter_redux.dart';
  7. class Blocks extends StatefulWidget {
  8. @override
  9. _BlocksState createState() => _BlocksState();
  10. }
  11. class _BlocksState extends State<Blocks> with TickerProviderStateMixin {
  12. @override
  13. Widget build(BuildContext context) {
  14. return StoreConnector<GameState, BlocksProps>(
  15. converter: (store) => BlocksProps(
  16. data: store.state.data,
  17. mode: store.state.mode,
  18. padding: Screen.getBorderWidth(store.state.mode),
  19. ),
  20. builder: (context, props) {
  21. var blockFactory = BlockFactory(this, props.mode);
  22. blockFactory.play();
  23. return Center(
  24. child: Container(
  25. width: Screen.stageWidth,
  26. height: Screen.stageWidth,
  27. padding: EdgeInsets.fromLTRB(props.padding, props.padding, 0, 0),
  28. child: Stack(
  29. fit: StackFit.expand,
  30. children: getBlocks(blockFactory, props),
  31. ),
  32. ),
  33. );
  34. },
  35. );
  36. }
  37. getBlocks(BlockFactory blockFactory, BlocksProps props) {
  38. var blocks = <Widget>[];
  39. props.data.forEach((row) {
  40. row.forEach((block) {
  41. if (block.value != 0) {
  42. blocks.add(blockFactory.create(block));
  43. }
  44. });
  45. });
  46. return blocks;
  47. }
  48. }
  49. class BlocksProps {
  50. int mode;
  51. double padding;
  52. List<List<BlockInfo>> data;
  53. BlocksProps({required this.padding, required this.mode, required this.data});
  54. }