Widget rendering improvements

This commit is contained in:
Yegor Vialov 2020-04-25 14:15:19 +00:00
parent 513bf85cae
commit 16c06a2d48
7 changed files with 120 additions and 132 deletions

View File

@ -19,6 +19,7 @@ class HACard {
String unit; String unit;
int min; int min;
int max; int max;
int depth;
Map severity; Map severity;
HACard({ HACard({
@ -37,6 +38,7 @@ class HACard {
this.unit, this.unit,
this.min, this.min,
this.max, this.max,
this.depth: 1,
this.severity, this.severity,
@required this.type @required this.type
}) { }) {

View File

@ -87,12 +87,14 @@ class CardWidget extends StatelessWidget {
child: childCard.build(context), child: childCard.build(context),
) )
).toList(); ).toList();
return Row( return IntrinsicHeight(
child: Row(
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch,
children: children, children: children,
); ),
);
} }
return Container(height: 0.0, width: 0.0,); return Container(height: 0.0, width: 0.0,);
} }
@ -237,52 +239,48 @@ class CardWidget extends StatelessWidget {
if (entitiesToShow.isEmpty && !card.showEmpty) { if (entitiesToShow.isEmpty && !card.showEmpty) {
return Container(height: 0.0, width: 0.0,); return Container(height: 0.0, width: 0.0,);
} }
List<Widget> rows = []; int length = entitiesToShow.length;
rows.add(CardHeader(name: card.name)); int columnsCount = length >= card.columnsCount ? card.columnsCount : entitiesToShow.length;
int rowsCount = (length / columnsCount).round();
int columnsCount = entitiesToShow.length >= card.columnsCount ? card.columnsCount : entitiesToShow.length; List<TableRow> rows = [];
for (int i = 0; i < rowsCount; i++) {
rows.add( int start = i*columnsCount;
Padding( int end = start + math.min(columnsCount, length - start);
padding: EdgeInsets.only(bottom: Sizes.rowPadding, top: Sizes.rowPadding), List<Widget> rowChildren = [];
child: FractionallySizedBox( rowChildren.addAll(entitiesToShow.sublist(
widthFactor: 1, start, end
child: LayoutBuilder( ).map(
builder: (BuildContext context, BoxConstraints constraints) { (EntityWrapper entity){
List<Widget> buttons = []; return EntityModel(
double buttonWidth = constraints.maxWidth / columnsCount; entityWrapper: entity,
entitiesToShow.forEach((EntityWrapper entity) { child: GlanceCardEntityContainer(
buttons.add( showName: card.showName,
SizedBox( showState: card.showState,
width: buttonWidth, ),
child: EntityModel( handleTap: true
entityWrapper: entity, );
child: GlanceCardEntityContainer( }
showName: card.showName, ).toList()
showState: card.showState, );
), while (rowChildren.length < columnsCount) {
handleTap: true rowChildren.add(
), Container()
) );
); }
}); rows.add(
return Wrap( TableRow(
//spacing: 5.0, children: rowChildren
//alignment: WrapAlignment.spaceEvenly,
runSpacing: Sizes.doubleRowPadding,
children: buttons,
);
}
),
),
) )
); );
}
return Card( return Card(
child: Column( child: Padding(
mainAxisSize: MainAxisSize.min, padding: EdgeInsets.symmetric(vertical: Sizes.rowPadding),
children: rows child: Table(
children: rows
) )
)
); );
} }
@ -303,6 +301,7 @@ class CardWidget extends StatelessWidget {
child: EntityModel( child: EntityModel(
entityWrapper: card.linkedEntityWrapper, entityWrapper: card.linkedEntityWrapper,
child: EntityButtonCardBody( child: EntityButtonCardBody(
depth: card.depth,
showName: card.showName, showName: card.showName,
), ),
handleTap: true handleTap: true

View File

@ -3,9 +3,10 @@ part of '../../main.dart';
class EntityButtonCardBody extends StatelessWidget { class EntityButtonCardBody extends StatelessWidget {
final bool showName; final bool showName;
final int depth;
EntityButtonCardBody({ EntityButtonCardBody({
Key key, this.showName: true, Key key, this.showName: true, @required this.depth
}) : super(key: key); }) : super(key: key);
@override @override
@ -17,27 +18,20 @@ class EntityButtonCardBody extends StatelessWidget {
if (entityWrapper.entity.statelessType > StatelessEntityType.MISSED) { if (entityWrapper.entity.statelessType > StatelessEntityType.MISSED) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
double widthBase = math.min(MediaQuery.of(context).size.width, MediaQuery.of(context).size.height) / 6;
return InkWell( return InkWell(
onTap: () => entityWrapper.handleTap(), onTap: () => entityWrapper.handleTap(),
onLongPress: () => entityWrapper.handleHold(), onLongPress: () => entityWrapper.handleHold(),
onDoubleTap: () => entityWrapper.handleDoubleTap(), onDoubleTap: () => entityWrapper.handleDoubleTap(),
child: FractionallySizedBox( child: Column(
widthFactor: 1, mainAxisSize: MainAxisSize.min,
child: Column( children: <Widget>[
mainAxisSize: MainAxisSize.min, EntityIcon(
children: <Widget>[ padding: EdgeInsets.fromLTRB(2.0, 6.0, 2.0, 2.0),
LayoutBuilder( size: widthBase / (depth * 0.5),
builder: (BuildContext context, BoxConstraints constraints) { ),
return EntityIcon( _buildName()
padding: EdgeInsets.fromLTRB(2.0, 6.0, 2.0, 2.0), ],
size: constraints.maxWidth / 2.5,
);
}
),
_buildName()
],
),
), ),
); );
} }

View File

@ -94,75 +94,61 @@ class GaugeCardBody extends StatelessWidget {
onDoubleTap: () => entityWrapper.handleDoubleTap(), onDoubleTap: () => entityWrapper.handleDoubleTap(),
child: AspectRatio( child: AspectRatio(
aspectRatio: 2, aspectRatio: 2,
child: LayoutBuilder( child: SfRadialGauge(
builder: (BuildContext context, BoxConstraints constraints) { axes: <RadialAxis>[
double fontSizeFactor; RadialAxis(
if (constraints.maxWidth > 300.0) { maximum: max.toDouble(),
fontSizeFactor = 1.6; minimum: min.toDouble(),
} else if (constraints.maxWidth > 150.0) { showLabels: false,
fontSizeFactor = 1; useRangeColorForAxis: true,
} else if (constraints.maxWidth > 100.0) { showTicks: false,
fontSizeFactor = 0.6; canScaleToFit: true,
} else { ranges: ranges,
fontSizeFactor = 0.4; axisLineStyle: AxisLineStyle(
} thickness: 0.3,
return SfRadialGauge( thicknessUnit: GaugeSizeUnit.factor,
axes: <RadialAxis>[ color: Colors.transparent
RadialAxis( ),
maximum: max.toDouble(), annotations: <GaugeAnnotation>[
minimum: min.toDouble(), GaugeAnnotation(
showLabels: false, angle: -90,
useRangeColorForAxis: true, positionFactor: 1.3,
showTicks: false, //verticalAlignment: GaugeAlignment.far,
canScaleToFit: true, widget: EntityName(
ranges: ranges, textStyle: Theme.of(context).textTheme.body1.copyWith(
axisLineStyle: AxisLineStyle( fontSize: Theme.of(context).textTheme.body1.fontSize
thickness: 0.3,
thicknessUnit: GaugeSizeUnit.factor,
color: Colors.transparent
),
annotations: <GaugeAnnotation>[
GaugeAnnotation(
angle: -90,
positionFactor: 1.3,
//verticalAlignment: GaugeAlignment.far,
widget: EntityName(
textStyle: Theme.of(context).textTheme.body1.copyWith(
fontSize: Theme.of(context).textTheme.body1.fontSize * fontSizeFactor
),
),
), ),
GaugeAnnotation( ),
angle: 180, ),
positionFactor: 0, GaugeAnnotation(
verticalAlignment: GaugeAlignment.center, angle: 180,
widget: SimpleEntityState( positionFactor: 0,
expanded: false, verticalAlignment: GaugeAlignment.center,
maxLines: 1, widget: SimpleEntityState(
textAlign: TextAlign.center, expanded: false,
textStyle: Theme.of(context).textTheme.title.copyWith( maxLines: 1,
fontSize: Theme.of(context).textTheme.title.fontSize * fontSizeFactor, textAlign: TextAlign.center,
), textStyle: Theme.of(context).textTheme.title.copyWith(
), fontSize: Theme.of(context).textTheme.title.fontSize,
) ),
], ),
startAngle: 180,
endAngle: 0,
pointers: <GaugePointer>[
RangePointer(
value: fixedValue,
sizeUnit: GaugeSizeUnit.factor,
width: 0.3,
color: currentColor,
enableAnimation: true,
animationType: AnimationType.bounceOut,
)
]
) )
], ],
); startAngle: 180,
}, endAngle: 0,
), pointers: <GaugePointer>[
RangePointer(
value: fixedValue,
sizeUnit: GaugeSizeUnit.factor,
width: 0.3,
color: currentColor,
enableAnimation: true,
animationType: AnimationType.bounceOut,
)
]
)
],
)
), ),
); );
} }

View File

@ -1,5 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:async'; import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';

View File

@ -33,10 +33,10 @@ class HAView {
}); });
} }
cards.addAll(_createLovelaceCards(rawData["cards"] ?? [])); cards.addAll(_createLovelaceCards(rawData["cards"] ?? [], 1));
} }
List<HACard> _createLovelaceCards(List rawCards) { List<HACard> _createLovelaceCards(List rawCards, int depth) {
List<HACard> result = []; List<HACard> result = [];
rawCards.forEach((rawCard){ rawCards.forEach((rawCard){
try { try {
@ -58,10 +58,11 @@ class HAView {
min: rawCardInfo['min'] ?? 0, min: rawCardInfo['min'] ?? 0,
max: rawCardInfo['max'] ?? 100, max: rawCardInfo['max'] ?? 100,
unit: rawCardInfo['unit'], unit: rawCardInfo['unit'],
depth: depth,
severity: rawCardInfo['severity'] severity: rawCardInfo['severity']
); );
if (rawCardInfo["cards"] != null) { if (rawCardInfo["cards"] != null) {
card.childCards = _createLovelaceCards(rawCardInfo["cards"]); card.childCards = _createLovelaceCards(rawCardInfo["cards"], depth + 1);
} }
var rawEntities = rawCard["entities"] ?? rawCardInfo["entities"]; var rawEntities = rawCard["entities"] ?? rawCardInfo["entities"];
rawEntities?.forEach((rawEntity) { rawEntities?.forEach((rawEntity) {

View File

@ -26,6 +26,11 @@ class ViewWidget extends StatelessWidget {
} else { } else {
cardsContainer = Container(); cardsContainer = Container();
} }
return ListView(
shrinkWrap: false,
padding: EdgeInsets.all(0),
children: this.view.cards.map((card) => card.build(context)).toList()
);
return ListView( return ListView(
shrinkWrap: true, shrinkWrap: true,
padding: EdgeInsets.all(0), padding: EdgeInsets.all(0),