Avoid dividing by 0 and setting scale to 0

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-02-27 14:24:15 +08:00
parent 04fd17f526
commit cb4b658e48

View File

@ -617,13 +617,28 @@ class ViewStyle {
final int displayWidth; final int displayWidth;
final int displayHeight; final int displayHeight;
ViewStyle({ ViewStyle({
this.style = '', required this.style,
this.width = 0.0, required this.width,
this.height = 0.0, required this.height,
this.displayWidth = 0, required this.displayWidth,
this.displayHeight = 0, required this.displayHeight,
}); });
static defaultViewStyle() {
final desktop = (isDesktop || isWebDesktop);
final w =
desktop ? kDesktopDefaultDisplayWidth : kMobileDefaultDisplayWidth;
final h =
desktop ? kDesktopDefaultDisplayHeight : kMobileDefaultDisplayHeight;
return ViewStyle(
style: '',
width: w.toDouble(),
height: h.toDouble(),
displayWidth: w,
displayHeight: h,
);
}
static int _double2Int(double v) => (v * 100).round().toInt(); static int _double2Int(double v) => (v * 100).round().toInt();
@override @override
@ -652,10 +667,15 @@ class ViewStyle {
double get scale { double get scale {
double s = 1.0; double s = 1.0;
if (style == kRemoteViewStyleAdaptive) { if (style == kRemoteViewStyleAdaptive) {
if (width != 0 &&
height != 0 &&
displayWidth != 0 &&
displayHeight != 0) {
final s1 = width / displayWidth; final s1 = width / displayWidth;
final s2 = height / displayHeight; final s2 = height / displayHeight;
s = s1 < s2 ? s1 : s2; s = s1 < s2 ? s1 : s2;
} }
}
return s; return s;
} }
} }
@ -680,7 +700,7 @@ class CanvasModel with ChangeNotifier {
// scroll offset y percent // scroll offset y percent
double _scrollY = 0.0; double _scrollY = 0.0;
ScrollStyle _scrollStyle = ScrollStyle.scrollauto; ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
ViewStyle _lastViewStyle = ViewStyle(); ViewStyle _lastViewStyle = ViewStyle.defaultViewStyle();
final _imageOverflow = false.obs; final _imageOverflow = false.obs;