Skip to content

Commit be77d91

Browse files
committed
Implement safe area for canvases
1 parent 163958c commit be77d91

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

lib/src/api/models/models.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export 'paint_type.dart';
4949
export 'pins_model.dart';
5050
export 'reaction.dart';
5151
export 'rect.dart';
52+
export 'safe_area.dart';
5253
export 'shape_border.dart';
5354
export 'size.dart';
5455
export 'start_end_prop.dart';

lib/src/api/models/safe_area.dart

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
2+
import 'package:equatable/equatable.dart';
3+
4+
part 'safe_area.g.dart';
5+
6+
/// Represents a safe area.
7+
@JsonSerializable(useDynamics: true, createToJson: false)
8+
class SafeAreaModel with EquatableMixin {
9+
/// Whether to apply safe area to the top side of the widget.
10+
final bool top;
11+
12+
/// Whether to apply safe area to the bottom side of the widget.
13+
final bool bottom;
14+
15+
/// Whether to apply safe area to the left side of the widget.
16+
final bool left;
17+
18+
/// Whether to apply safe area to the right side of the widget.
19+
final bool right;
20+
21+
/// Whether to apply safe area to the widget.
22+
final bool enabled;
23+
24+
/// Creates a new [SafeAreaModel] instance.
25+
const SafeAreaModel({
26+
required this.top,
27+
required this.bottom,
28+
required this.left,
29+
required this.right,
30+
required this.enabled,
31+
});
32+
33+
/// Creates a new [SafeAreaModel] instance with all sides set to [value].
34+
const SafeAreaModel.all(bool value)
35+
: this(
36+
top: value,
37+
bottom: value,
38+
left: value,
39+
right: value,
40+
enabled: value,
41+
);
42+
43+
/// Creates a new [SafeAreaModel] instance with overridden values.
44+
SafeAreaModel copyWith({
45+
bool? top,
46+
bool? bottom,
47+
bool? left,
48+
bool? right,
49+
bool? enabled,
50+
}) {
51+
return SafeAreaModel(
52+
top: top ?? this.top,
53+
bottom: bottom ?? this.bottom,
54+
left: left ?? this.left,
55+
right: right ?? this.right,
56+
enabled: enabled ?? this.enabled,
57+
);
58+
}
59+
60+
@override
61+
List<Object?> get props => [
62+
top,
63+
bottom,
64+
left,
65+
right,
66+
enabled,
67+
];
68+
69+
/// Creates a new [SafeAreaModel] from a JSON data.
70+
factory SafeAreaModel.fromJson(dynamic json) {
71+
if (json case bool value) {
72+
return SafeAreaModel(
73+
top: value,
74+
bottom: value,
75+
left: value,
76+
right: value,
77+
enabled: value,
78+
);
79+
}
80+
if (json case [bool horizontal, bool vertical, bool enabled]) {
81+
return SafeAreaModel(
82+
top: vertical,
83+
bottom: vertical,
84+
left: horizontal,
85+
right: horizontal,
86+
enabled: enabled,
87+
);
88+
}
89+
if (json
90+
case [bool left, bool top, bool right, bool bottom, bool enabled]) {
91+
return SafeAreaModel(
92+
top: top,
93+
bottom: bottom,
94+
left: left,
95+
right: right,
96+
enabled: enabled,
97+
);
98+
}
99+
return _$SafeAreaModelFromJson(json);
100+
}
101+
102+
/// Converts this [SafeAreaModel] to JSON.
103+
dynamic toJson() {
104+
if (left == top && top == right && right == bottom && bottom == enabled) {
105+
return enabled;
106+
}
107+
if (left == right && top == bottom && left != top) {
108+
return [left, top, enabled];
109+
}
110+
return [left, top, right, bottom, enabled];
111+
}
112+
}

lib/src/api/models/safe_area.g.dart

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/nodes/canvas_node.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class CanvasProperties with SerializableMixin, EquatableMixin {
174174
/// FAB properties if enabled.
175175
FloatingActionButtonProperties? floatingActionButton;
176176

177+
/// Safe area properties.
178+
SafeAreaModel safeArea;
179+
177180
/// Whether this canvas is a scaffold. A scaffold can hold an app bar, drawer,
178181
/// bottom navigation bar, and floating action button. If any of this is
179182
/// present, the canvas is a scaffold.
@@ -188,13 +191,15 @@ class CanvasProperties with SerializableMixin, EquatableMixin {
188191
this.navigationBarPlaceholderId,
189192
this.topAppBarPlaceholderId,
190193
this.floatingActionButton,
194+
this.safeArea = const SafeAreaModel.all(true),
191195
});
192196

193197
@override
194198
List<Object?> get props => [
195199
navigationBarPlaceholderId,
196200
topAppBarPlaceholderId,
197201
floatingActionButton,
202+
safeArea,
198203
];
199204

200205
/// Creates a new [CanvasProperties] from a JSON data.

lib/src/api/nodes/canvas_node.g.dart

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)