battle_layout.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/widgets.dart';
  2. import 'package:widget/src/constant.dart';
  3. import 'battle_background.dart';
  4. import 'battle_foreground.dart';
  5. enum _BattleSlot {
  6. background,
  7. card,
  8. foreground,
  9. topRightLogo,
  10. bottomLeftLogo,
  11. bottomRightLogo,
  12. button,
  13. }
  14. class _BattleLayoutDelegate extends MultiChildLayoutDelegate {
  15. @override
  16. void performLayout(Size size) {
  17. final constraints = BoxConstraints.loose(size);
  18. double boardWidth = size.width * kBoardWidthAspectRatio;
  19. double boardHeight = size.height * kBoardHeightAspectRatio;
  20. final boardConstraints = BoxConstraints.loose(Size(boardWidth, boardHeight));
  21. layoutChild(_BattleSlot.background, boardConstraints);
  22. positionChild(_BattleSlot.background, Offset(size.width - boardWidth, size.height - boardHeight));
  23. layoutChild(_BattleSlot.foreground, boardConstraints);
  24. positionChild(_BattleSlot.foreground, Offset(size.width - boardWidth, size.height - boardHeight));
  25. double cardWidth = size.width * kCardWidthAspectRatio;
  26. double cardHeight = size.height * kCardHeightAspectRatio;
  27. double leftPadding = size.width * kLeftBorderAspectRatio;
  28. double coverPadding = size.width * kCardCoverAspectRatio;
  29. final cardConstraints = BoxConstraints.loose(Size(cardWidth, cardHeight));
  30. layoutChild(_BattleSlot.card, cardConstraints);
  31. positionChild(_BattleSlot.card, Offset(leftPadding - coverPadding, 0));
  32. layoutChild(_BattleSlot.button, boardConstraints);
  33. positionChild(_BattleSlot.button, Offset(size.width - boardWidth, size.height - boardHeight));
  34. }
  35. @override
  36. bool shouldRelayout(_BattleLayoutDelegate oldDelegate) => false;
  37. }
  38. class BattleBoard extends StatefulWidget {
  39. final Widget card;
  40. final Widget button;
  41. const BattleBoard({
  42. super.key,
  43. required this.card,
  44. required this.button,
  45. });
  46. @override
  47. State<BattleBoard> createState() => _BattleBoardState();
  48. }
  49. class _BattleBoardState extends State<BattleBoard> {
  50. void _addWithSlot(List<LayoutId> children, _BattleSlot slot, Widget child) {
  51. children.add(LayoutId(id: slot, child: child));
  52. }
  53. @override
  54. Widget build(BuildContext context) {
  55. List<LayoutId> children = <LayoutId>[];
  56. const Widget foreground = BattleForeground();
  57. const Widget background = BattleBackground();
  58. children.add(LayoutId(id: _BattleSlot.background, child: background));
  59. children.add(LayoutId(id: _BattleSlot.card, child: widget.card));
  60. children.add(LayoutId(id: _BattleSlot.foreground, child: foreground));
  61. children.add(LayoutId(id: _BattleSlot.button, child: widget.button));
  62. return Center(
  63. child: AspectRatio(
  64. aspectRatio: kTextureAspectRatio,
  65. child: CustomMultiChildLayout(
  66. delegate: _BattleLayoutDelegate(),
  67. children: children,
  68. ),
  69. ),
  70. );
  71. }
  72. }