12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import 'package:flutter/widgets.dart';
- import 'package:widget/src/constant.dart';
- import 'battle_background.dart';
- import 'battle_foreground.dart';
- enum _BattleSlot {
- background,
- card,
- foreground,
- topRightLogo,
- bottomLeftLogo,
- bottomRightLogo,
- button,
- }
- class _BattleLayoutDelegate extends MultiChildLayoutDelegate {
- @override
- void performLayout(Size size) {
- final constraints = BoxConstraints.loose(size);
- double boardWidth = size.width * kBoardWidthAspectRatio;
- double boardHeight = size.height * kBoardHeightAspectRatio;
- final boardConstraints = BoxConstraints.loose(Size(boardWidth, boardHeight));
- layoutChild(_BattleSlot.background, boardConstraints);
- positionChild(_BattleSlot.background, Offset(size.width - boardWidth, size.height - boardHeight));
- layoutChild(_BattleSlot.foreground, boardConstraints);
- positionChild(_BattleSlot.foreground, Offset(size.width - boardWidth, size.height - boardHeight));
- double cardWidth = size.width * kCardWidthAspectRatio;
- double cardHeight = size.height * kCardHeightAspectRatio;
- double leftPadding = size.width * kLeftBorderAspectRatio;
- double coverPadding = size.width * kCardCoverAspectRatio;
- final cardConstraints = BoxConstraints.loose(Size(cardWidth, cardHeight));
- layoutChild(_BattleSlot.card, cardConstraints);
- positionChild(_BattleSlot.card, Offset(leftPadding - coverPadding, 0));
- layoutChild(_BattleSlot.button, boardConstraints);
- positionChild(_BattleSlot.button, Offset(size.width - boardWidth, size.height - boardHeight));
- }
- @override
- bool shouldRelayout(_BattleLayoutDelegate oldDelegate) => false;
- }
- class BattleBoard extends StatefulWidget {
- final Widget card;
- final Widget button;
- const BattleBoard({
- super.key,
- required this.card,
- required this.button,
- });
- @override
- State<BattleBoard> createState() => _BattleBoardState();
- }
- class _BattleBoardState extends State<BattleBoard> {
- void _addWithSlot(List<LayoutId> children, _BattleSlot slot, Widget child) {
- children.add(LayoutId(id: slot, child: child));
- }
- @override
- Widget build(BuildContext context) {
- List<LayoutId> children = <LayoutId>[];
- const Widget foreground = BattleForeground();
- const Widget background = BattleBackground();
- children.add(LayoutId(id: _BattleSlot.background, child: background));
- children.add(LayoutId(id: _BattleSlot.card, child: widget.card));
- children.add(LayoutId(id: _BattleSlot.foreground, child: foreground));
- children.add(LayoutId(id: _BattleSlot.button, child: widget.button));
- return Center(
- child: AspectRatio(
- aspectRatio: kTextureAspectRatio,
- child: CustomMultiChildLayout(
- delegate: _BattleLayoutDelegate(),
- children: children,
- ),
- ),
- );
- }
- }
|