Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor numbers.dart utils #3263

Merged
merged 10 commits into from
May 26, 2024
65 changes: 36 additions & 29 deletions packages/flet/lib/src/controls/canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,56 +344,63 @@ class FletCustomPainter extends CustomPainter {
for (var elem in (j as List)) {
var type = elem["type"];
if (type == "moveto") {
path.moveTo(parseDouble(elem["x"]), parseDouble(elem["y"]));
path.moveTo(parseDouble(elem["x"], 0)!, parseDouble(elem["y"], 0)!);
} else if (type == "lineto") {
path.lineTo(parseDouble(elem["x"]), parseDouble(elem["y"]));
path.lineTo(parseDouble(elem["x"], 0)!, parseDouble(elem["y"], 0)!);
} else if (type == "arc") {
path.addArc(
Rect.fromLTWH(parseDouble(elem["x"]), parseDouble(elem["y"]),
parseDouble(elem["width"]), parseDouble(elem["height"])),
parseDouble(elem["start_angle"]),
parseDouble(elem["sweep_angle"]));
Rect.fromLTWH(
parseDouble(elem["x"], 0)!,
parseDouble(elem["y"], 0)!,
parseDouble(elem["width"], 0)!,
parseDouble(elem["height"], 0)!),
parseDouble(elem["start_angle"], 0)!,
parseDouble(elem["sweep_angle"], 0)!);
} else if (type == "arcto") {
path.arcToPoint(Offset(parseDouble(elem["x"]), parseDouble(elem["y"])),
radius: Radius.circular(parseDouble(elem["radius"])),
rotation: parseDouble(elem["rotation"]),
largeArc: parseBool(elem["large_arc"]),
clockwise: parseBool(elem["clockwise"]));
path.arcToPoint(
Offset(parseDouble(elem["x"], 0)!, parseDouble(elem["y"], 0)!),
radius: Radius.circular(parseDouble(elem["radius"], 0)!),
rotation: parseDouble(elem["rotation"], 0)!,
largeArc: parseBool(elem["large_arc"], false)!,
clockwise: parseBool(elem["clockwise"], true)!);
} else if (type == "oval") {
path.addOval(Rect.fromLTWH(
parseDouble(elem["x"]),
parseDouble(elem["y"]),
parseDouble(elem["width"]),
parseDouble(elem["height"])));
parseDouble(elem["x"], 0)!,
parseDouble(elem["y"], 0)!,
parseDouble(elem["width"], 0)!,
parseDouble(elem["height"], 0)!));
} else if (type == "rect") {
var borderRadius = elem["border_radius"] != null
? borderRadiusFromJSON(elem["border_radius"])
: null;
path.addRRect(RRect.fromRectAndCorners(
Rect.fromLTWH(parseDouble(elem["x"]), parseDouble(elem["y"]),
parseDouble(elem["width"]), parseDouble(elem["height"])),
Rect.fromLTWH(
parseDouble(elem["x"], 0)!,
parseDouble(elem["y"], 0)!,
parseDouble(elem["width"], 0)!,
parseDouble(elem["height"], 0)!),
topLeft: borderRadius?.topLeft ?? Radius.zero,
topRight: borderRadius?.topRight ?? Radius.zero,
bottomLeft: borderRadius?.bottomLeft ?? Radius.zero,
bottomRight: borderRadius?.bottomRight ?? Radius.zero));
} else if (type == "conicto") {
path.conicTo(
parseDouble(elem["cp1x"]),
parseDouble(elem["cp1y"]),
parseDouble(elem["x"]),
parseDouble(elem["y"]),
parseDouble(elem["w"]));
parseDouble(elem["cp1x"], 0)!,
parseDouble(elem["cp1y"], 0)!,
parseDouble(elem["x"], 0)!,
parseDouble(elem["y"], 0)!,
parseDouble(elem["w"], 0)!);
} else if (type == "cubicto") {
path.cubicTo(
parseDouble(elem["cp1x"]),
parseDouble(elem["cp1y"]),
parseDouble(elem["cp2x"]),
parseDouble(elem["cp2y"]),
parseDouble(elem["x"]),
parseDouble(elem["y"]));
parseDouble(elem["cp1x"], 0)!,
parseDouble(elem["cp1y"], 0)!,
parseDouble(elem["cp2x"], 0)!,
parseDouble(elem["cp2y"], 0)!,
parseDouble(elem["x"], 0)!,
parseDouble(elem["y"], 0)!);
} else if (type == "subpath") {
path.addPath(buildPath(elem["elements"]),
Offset(parseDouble(elem["x"]), parseDouble(elem["y"])));
Offset(parseDouble(elem["x"], 0)!, parseDouble(elem["y"], 0)!));
} else if (type == "close") {
path.close();
}
Expand Down
1 change: 1 addition & 0 deletions packages/flet/lib/src/controls/linechart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ class _LineChartControlState extends State<LineChartControl> {
dashArray: dashPattern != null
? (json.decode(dashPattern) as List)
.map((e) => parseInt(e))
.whereNotNull()
.toList()
: null,
shadow: shadow.isNotEmpty
Expand Down
6 changes: 3 additions & 3 deletions packages/flet/lib/src/controls/scrollable_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _ScrollableControlState extends State<ScrollableControl>
var params = Map<String, dynamic>.from(mj["p"] as Map);

if (name == "scroll_to") {
var duration = parseInt(params["duration"]);
var duration = parseInt(params["duration"], 0)!;
var curve = parseCurve(params["curve"], Curves.ease)!;
if (params["key"] != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand All @@ -109,7 +109,7 @@ class _ScrollableControlState extends State<ScrollableControl>
});
} else if (params["offset"] != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
var offset = parseDouble(params["offset"]);
var offset = parseDouble(params["offset"], 0)!;
if (offset < 0) {
offset = _controller.position.maxScrollExtent + offset + 1;
}
Expand All @@ -125,7 +125,7 @@ class _ScrollableControlState extends State<ScrollableControl>
});
} else if (params["delta"] != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
var delta = parseDouble(params["delta"]);
var delta = parseDouble(params["delta"], 0)!;
var offset = _controller.position.pixels + delta;
if (duration < 1) {
_controller.jumpTo(offset);
Expand Down
4 changes: 2 additions & 2 deletions packages/flet/lib/src/controls/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class TextControl extends StatelessWidget with FletStoreMixin {

List<FontVariation> variations = [];
if (fontWeight.startsWith("w")) {
variations
.add(FontVariation('wght', parseDouble(fontWeight.substring(1))));
variations.add(
FontVariation('wght', parseDouble(fontWeight.substring(1), 0)!));
}

style = (style ?? const TextStyle()).copyWith(
Expand Down
2 changes: 1 addition & 1 deletion packages/flet/lib/src/utils/alignment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ Alignment? alignmentFromJson(Map<String, dynamic>? json,
if (json == null) {
return defValue;
}
return Alignment(parseDouble(json['x']), parseDouble(json['y']));
return Alignment(parseDouble(json['x'], 0)!, parseDouble(json['y'],0)!);
}
3 changes: 2 additions & 1 deletion packages/flet/lib/src/utils/animations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ImplicitAnimationDetails? parseAnimation(Control control, String propName,
ImplicitAnimationDetails animationFromJSON(dynamic json) {
if (json is int) {
return ImplicitAnimationDetails(
duration: Duration(milliseconds: parseInt(json)), curve: Curves.linear);
duration: Duration(milliseconds: parseInt(json, 0)!),
curve: Curves.linear);
} else if (json is bool && json == true) {
return ImplicitAnimationDetails(
duration: const Duration(milliseconds: 1000), curve: Curves.linear);
Expand Down
12 changes: 6 additions & 6 deletions packages/flet/lib/src/utils/borders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ OutlinedBorder? parseOutlinedBorder(Control control, String propName) {

BorderRadius borderRadiusFromJSON(dynamic json) {
if (json is int || json is double) {
return BorderRadius.all(Radius.circular(parseDouble(json)));
return BorderRadius.all(Radius.circular(parseDouble(json, 0)!));
}
return BorderRadius.only(
topLeft: Radius.circular(parseDouble(json['tl'])),
topRight: Radius.circular(parseDouble(json['tr'])),
bottomLeft: Radius.circular(parseDouble(json['bl'])),
bottomRight: Radius.circular(parseDouble(json['br'])),
topLeft: Radius.circular(parseDouble(json['tl'], 0)!),
topRight: Radius.circular(parseDouble(json['tr'], 0)!),
bottomLeft: Radius.circular(parseDouble(json['bl'], 0)!),
bottomRight: Radius.circular(parseDouble(json['br'], 0)!),
);
}

Expand All @@ -89,7 +89,7 @@ BorderSide? borderSideFromJSON(ThemeData? theme, dynamic json,
? BorderSide(
color:
parseColor(theme, json['c'], defaultSideColor ?? Colors.black)!,
width: parseDouble(json['w'], 1),
width: parseDouble(json['w'], 1)!,
style: BorderStyle.solid)
: null;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flet/lib/src/utils/buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ ButtonStyle? buttonStyleFromJSON(ThemeData theme, Map<String, dynamic>? json,
(jv) => parseColor(theme, jv as String),
defaultSurfaceTintColor),
elevation: getMaterialStateProperty(
json["elevation"], (jv) => parseDouble(jv), defaultElevation),
json["elevation"], (jv) => parseDouble(jv, 0)!, defaultElevation),
animationDuration: json["animation_duration"] != null
? Duration(milliseconds: parseInt(json["animation_duration"]))
? Duration(milliseconds: parseInt(json["animation_duration"], 0)!)
: null,
padding: getMaterialStateProperty<EdgeInsetsGeometry?>(
json["padding"], (jv) => edgeInsetsFromJson(jv), defaultPadding),
Expand Down
31 changes: 17 additions & 14 deletions packages/flet/lib/src/utils/charts.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -65,9 +66,12 @@ FlLine? parseSelectedFlLine(ThemeData theme, Control control, String propName,
color: j['color'] != null
? parseColor(theme, j['color'] as String, Colors.black)!
: defaultGetPointColor(color, gradient, 0),
strokeWidth: j['width'] != null ? parseDouble(j['width'], 3) : 3,
strokeWidth: parseDouble(j['width'], 2)!,
dashArray: j['dash_pattern'] != null
? (j['dash_pattern'] as List).map((e) => parseInt(e)).toList()
? (j['dash_pattern'] as List)
.map((e) => parseInt(e))
.whereNotNull()
.toList()
: null);
}

Expand All @@ -80,9 +84,12 @@ FlLine? flineFromJSON(theme, j) {
color: j['color'] != null
? parseColor(theme, j['color'] as String) ?? Colors.black
: Colors.black,
strokeWidth: j['width'] != null ? parseDouble(j['width'], 1) : 2,
strokeWidth: parseDouble(j['width'], 2)!,
dashArray: j['dash_pattern'] != null
? (j['dash_pattern'] as List).map((e) => parseInt(e)).toList()
? (j['dash_pattern'] as List)
.map((e) => parseInt(e))
.whereNotNull()
.toList()
: null);
}

Expand Down Expand Up @@ -140,34 +147,30 @@ FlDotPainter? chartDotPainterFromJSON(
color: json['color'] != null
? parseColor(theme, json['color'] as String) ?? Colors.green
: defaultGetPointColor(barColor, barGradient, percentage),
radius: json["radius"] != null ? parseDouble(json["radius"]) : null,
radius: parseDouble(json["radius"]),
strokeColor: json['stroke_color'] != null
? parseColor(theme, json['color'] as String) ??
const Color.fromRGBO(76, 175, 80, 1)
: defaultGetDotStrokeColor(barColor, barGradient, percentage),
strokeWidth: json["stroke_width"] != null
? parseDouble(json["stroke_width"])
: 1.0);
strokeWidth: parseDouble(json["stroke_width"], 1.0)!);
} else if (type == "square") {
return FlDotSquarePainter(
color: json['color'] != null
? parseColor(theme, json['color'] as String) ?? Colors.green
: defaultGetPointColor(barColor, barGradient, percentage),
size: json["size"] != null ? parseDouble(json["size"]) : 4.0,
size: parseDouble(json["size"], 4.0)!,
strokeColor: json['stroke_color'] != null
? parseColor(theme, json['color'] as String) ??
const Color.fromRGBO(76, 175, 80, 1)
: defaultGetDotStrokeColor(barColor, barGradient, percentage),
strokeWidth: json["stroke_width"] != null
? parseDouble(json["stroke_width"])
: 1.0);
strokeWidth: parseDouble(json["stroke_width"], 1.0)!);
} else if (type == "cross") {
return FlDotCrossPainter(
color: json['color'] != null
? parseColor(theme, json['color'] as String) ?? Colors.green
: defaultGetDotStrokeColor(barColor, barGradient, percentage),
size: json["size"] != null ? parseDouble(json["size"]) : 8.0,
width: json["width"] != null ? parseDouble(json["width"]) : 2.0,
size: parseDouble(json["size"], 8.0)!,
width: parseDouble(json["width"], 2.0)!,
);
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/flet/lib/src/utils/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ extension HexColor on Color {
}

if (color != null && colorOpacity != null) {
color = color.withOpacity(parseDouble(colorOpacity));
color = color.withOpacity(parseDouble(colorOpacity, 1.0)!);
}

return color ?? defaultColor;
Expand Down
2 changes: 1 addition & 1 deletion packages/flet/lib/src/utils/dismissible.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Map<DismissDirection, double>? parseDismissThresholds(
}

final j1 = json.decode(v);
return getDismissThresholds(j1, (jv) => parseDouble(jv));
return getDismissThresholds(j1, (jv) => parseDouble(jv, 0)!);
}

Map<DismissDirection, double>? getDismissThresholds<T>(
Expand Down
28 changes: 14 additions & 14 deletions packages/flet/lib/src/utils/drawing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:ui' as ui;

import 'package:flet/src/utils/others.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';

import '../models/control.dart';
Expand Down Expand Up @@ -30,12 +31,14 @@ List<double>? parsePaintStrokeDashPattern(Control control, String propName) {
final j1 = json.decode(v);

return j1["stroke_dash_pattern"] != null
? (j1["stroke_dash_pattern"] as List).map((e) => parseDouble(e)).toList()
? (j1["stroke_dash_pattern"] as List)
.map((e) => parseDouble(e))
.whereNotNull()
.toList()
: null;
}

Paint paintFromJSON(ThemeData? theme, Map<String, dynamic> json) {
//debugPrint("paintFromJSON: $json");
var paint = Paint();
if (json["color"] != null) {
paint.color = parseColor(theme, json["color"] as String, Colors.black)!;
Expand All @@ -54,12 +57,9 @@ Paint paintFromJSON(ThemeData? theme, Map<String, dynamic> json) {
if (json["gradient"] != null) {
paint.shader = paintGradientFromJSON(theme, json["gradient"]);
}
if (json["stroke_miter_limit"] != null) {
paint.strokeMiterLimit = parseDouble(json["stroke_miter_limit"]);
}
if (json["stroke_width"] != null) {
paint.strokeWidth = parseDouble(json["stroke_width"]);
}
paint.strokeMiterLimit = parseDouble(json["stroke_miter_limit"], 4)!;
paint.strokeWidth = parseDouble(json["stroke_width"], 0)!;

if (json["stroke_cap"] != null) {
paint.strokeCap = parseStrokeCap(json["stroke_cap"], StrokeCap.butt)!;
}
Expand Down Expand Up @@ -87,13 +87,13 @@ ui.Gradient? paintGradientFromJSON(
} else if (type == "radial") {
return ui.Gradient.radial(
offsetFromJson(json["center"])!,
parseDouble(json["radius"]),
parseDouble(json["radius"], 0)!,
parseColors(theme, json["colors"]),
parseStops(json["color_stops"]),
parseTileMode(json["tile_mode"], TileMode.clamp)!,
null,
offsetFromJson(json["focal"]),
parseDouble(json["focal_radius"]),
parseDouble(json["focal_radius"], 0)!,
);
} else if (type == "sweep") {
Offset center = offsetFromJson(json["center"])!;
Expand All @@ -102,8 +102,8 @@ ui.Gradient? paintGradientFromJSON(
parseColors(theme, json["colors"]),
parseStops(json["color_stops"]),
parseTileMode(json["tile_mode"], TileMode.clamp)!,
parseDouble(json["start_angle"]),
parseDouble(json["end_angle"]),
parseDouble(json["start_angle"], 0)!,
parseDouble(json["end_angle"], 0)!,
parseRotationToMatrix4(
json["rotation"], Rect.fromCircle(center: center, radius: 10)));
}
Expand All @@ -114,8 +114,8 @@ Offset? offsetFromJson(dynamic json) {
if (json == null) {
return null;
} else if (json is List && json.length > 1) {
return Offset(parseDouble(json[0]), parseDouble(json[1]));
return Offset(parseDouble(json[0], 0)!, parseDouble(json[1], 0)!);
} else {
return Offset(parseDouble(json["x"]), parseDouble(json["y"]));
return Offset(parseDouble(json["x"], 0)!, parseDouble(json["y"], 0)!);
}
}
Loading