This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/view.dart

153 lines
3.7 KiB
Dart
Raw Normal View History

2019-09-07 18:23:04 +03:00
part of 'main.dart';
2018-10-25 00:05:29 +03:00
2018-10-27 17:28:47 +03:00
class ViewWidget extends StatefulWidget {
2018-10-27 00:54:05 +03:00
final HAView view;
2018-10-25 00:05:29 +03:00
2018-10-27 17:28:47 +03:00
const ViewWidget({
2018-10-25 00:05:29 +03:00
Key key,
this.view
}) : super(key: key);
@override
State<StatefulWidget> createState() {
2018-10-27 17:28:47 +03:00
return ViewWidgetState();
2018-10-25 00:05:29 +03:00
}
}
2018-10-27 17:28:47 +03:00
class ViewWidgetState extends State<ViewWidget> {
2018-10-25 00:05:29 +03:00
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
if (widget.view.panel) {
return FractionallySizedBox(
widthFactor: 1,
heightFactor: 1,
child: _buildPanelChild(context),
);
} else {
return ListView(
2019-09-10 15:40:49 +03:00
shrinkWrap: true,
padding: EdgeInsets.all(0),
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints(maxHeight: 3000),
child: CustomMultiChildLayout(
delegate: ViewLayoutBuilder(
cardsCount: widget.view.cards.length
),
children: _buildChildren(context),
),
)
],
);
}
}
Widget _buildPanelChild(BuildContext context) {
if (widget.view.cards != null && widget.view.cards.isNotEmpty) {
return widget.view.cards[0].build(context);
} else {
return Container(width: 0, height: 0);
}
2018-10-25 00:05:29 +03:00
}
List<Widget> _buildChildren(BuildContext context) {
List<Widget> result = [];
2019-09-10 15:40:49 +03:00
int layoutChildId = 0;
2018-10-25 00:05:29 +03:00
if (widget.view.badges.isNotEmpty) {
2019-09-10 15:40:49 +03:00
result.add(
LayoutId(
id: "badges",
child: Wrap(
alignment: WrapAlignment.center,
spacing: 10.0,
runSpacing: 1.0,
children: _buildBadges(context),
),
2018-10-25 00:05:29 +03:00
)
);
}
2018-10-27 00:54:05 +03:00
widget.view.cards.forEach((HACard card){
2019-09-10 15:40:49 +03:00
result.add(
LayoutId(
id: 'card_$layoutChildId',
child: card.build(context),
)
2018-10-27 14:27:41 +03:00
);
2019-09-10 15:40:49 +03:00
layoutChildId += 1;
2018-10-25 00:05:29 +03:00
});
return result;
}
List<Widget> _buildBadges(BuildContext context) {
List<Widget> result = [];
widget.view.badges.forEach((Entity entity) {
2018-10-25 00:13:50 +03:00
if (!entity.isHidden) {
2019-09-10 15:40:49 +03:00
result.add(
entity.buildBadgeWidget(context)
);
2018-10-25 00:13:50 +03:00
}
2018-10-25 00:05:29 +03:00
});
return result;
}
@override
void dispose() {
super.dispose();
}
2019-09-10 15:40:49 +03:00
}
class ViewLayoutBuilder extends MultiChildLayoutDelegate {
final int cardsCount;
ViewLayoutBuilder({@required this.cardsCount});
2018-10-25 00:05:29 +03:00
2019-09-10 15:40:49 +03:00
@override
void performLayout(Size size) {
int columnsCount = (size.width ~/ Sizes.minViewColumnWidth);
double columnWidth = size.width / columnsCount;
List<double> columnXPositions = [];
List<double> columnYPositions = [];
double startY = 0;
if (hasChild("badges")) {
Size badgesSizes = layoutChild(
'badges', BoxConstraints.tightFor(width: size.width));
startY += badgesSizes.height;
positionChild('badges', Offset(0, 0));
}
for (int i =0; i < columnsCount; i++) {
columnXPositions.add(i*columnWidth);
columnYPositions.add(startY);
}
for (int i = 0; i < cardsCount; i++) {
final String cardId = 'card_$i';
if (hasChild(cardId)) {
int columnToAdd = 0;
double minYPosition = columnYPositions[0];
for (int i=1; i<columnsCount; i++) {
if (columnYPositions[i] < minYPosition) {
minYPosition = columnYPositions[i];
columnToAdd = i;
}
}
Size newSize = layoutChild(
'$cardId', BoxConstraints.tightFor(width: columnWidth));
positionChild('$cardId', Offset(columnXPositions[columnToAdd], columnYPositions[columnToAdd]));
columnYPositions[columnToAdd] = minYPosition + newSize.height;
}
}
}
@override
bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) => false;
2018-10-25 00:05:29 +03:00
}