game_bg.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_redux/flutter_redux.dart';
  3. import '../store/game_state.dart';
  4. import '../utils/screen.dart';
  5. /// 游戏格子背景
  6. class GameBg extends StatefulWidget {
  7. @override
  8. State<StatefulWidget> createState() => _GameBgState();
  9. }
  10. class _GameBgState extends State<StatefulWidget> {
  11. @override
  12. Widget build(BuildContext context) {
  13. return StoreConnector<GameState, GameBgProps>(
  14. converter: (store) => GameBgProps(
  15. borderWidth: Screen.getBorderWidth(store.state.mode),
  16. blockWidth: Screen.getBlockWidth(store.state.mode),
  17. mode: store.state.mode,
  18. ),
  19. builder: (context, vm) {
  20. var width = Screen.stageWidth;
  21. return Center(
  22. child: Container(
  23. width: width,
  24. padding: EdgeInsets.fromLTRB(vm.borderWidth, vm.borderWidth, 0, 0),
  25. decoration: BoxDecoration(
  26. color: const Color(0xffbbada0),
  27. border: Border.all(color: Colors.transparent, width: 0),
  28. borderRadius: BorderRadius.circular(5),
  29. ),
  30. child: getGrid(vm),
  31. ),
  32. );
  33. },
  34. );
  35. }
  36. getGrid(GameBgProps props) {
  37. var rows = <Widget>[];
  38. for (var i = 0; i < props.mode; i++) {
  39. var columns = <Widget>[];
  40. for (var j = 0; j < props.mode; j++) {
  41. columns.add(Container(
  42. width: props.blockWidth,
  43. height: props.blockWidth,
  44. decoration: BoxDecoration(
  45. color: Color.fromRGBO(238, 228, 218, 0.35),
  46. border: Border.all(color: Colors.transparent, width: 0),
  47. borderRadius: BorderRadius.circular(5),
  48. ),
  49. margin:
  50. EdgeInsets.fromLTRB(0, 0, props.borderWidth, props.borderWidth),
  51. ));
  52. }
  53. rows.add(Row(
  54. textDirection: TextDirection.ltr,
  55. crossAxisAlignment: CrossAxisAlignment.center,
  56. children: columns,
  57. ));
  58. }
  59. return Column(
  60. children: rows,
  61. crossAxisAlignment: CrossAxisAlignment.center,
  62. );
  63. }
  64. }
  65. class GameBgProps {
  66. double borderWidth; //边框大小
  67. double blockWidth; //块大小
  68. int mode; //游戏模式,3*3 4*4 6*6
  69. GameBgProps({this.borderWidth, this.blockWidth, this.mode});
  70. }