Skip to content

Commit

Permalink
FloatingActionButtonLocation offset (#2411)
Browse files Browse the repository at this point in the history
* offset type

* CustomFloatingActionButtonLocation

* Prevent FAB from winking when location changed

---------

Co-authored-by: Feodor Fitsner <[email protected]>
  • Loading branch information
ndonkoHenri and FeodorFitsner authored Jan 17, 2024
1 parent 9f5a2ef commit ed672c5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
46 changes: 41 additions & 5 deletions package/lib/src/controls/floating_action_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../utils/borders.dart';
import '../utils/colors.dart';
import '../utils/icons.dart';
import '../utils/launch_url.dart';
import '../utils/transforms.dart';
import 'create_control.dart';
import 'error.dart';

Expand Down Expand Up @@ -129,9 +130,44 @@ FloatingActionButtonLocation parseFloatingActionButtonLocation(
FloatingActionButtonLocation.startTop
];

return fabLocations.firstWhere(
(l) =>
l.toString().split('.').last.toLowerCase() ==
control.attrString(propName, "")!.toLowerCase(),
orElse: () => defValue);
try {
OffsetDetails? fabLocationOffsetDetails = parseOffset(control, propName);
if (fabLocationOffsetDetails != null) {
return CustomFloatingActionButtonLocation(
dx: fabLocationOffsetDetails.x, dy: fabLocationOffsetDetails.y);
} else {
return defValue;
}
} catch (e) {
return fabLocations.firstWhere(
(l) =>
l.toString().split('.').last.toLowerCase() ==
control.attrString(propName, "")!.toLowerCase(),
orElse: () => defValue);
}
}

class CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
final double dx;
final double dy;

CustomFloatingActionButtonLocation({required this.dx, required this.dy});

@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
return Offset(scaffoldGeometry.scaffoldSize.width - dx,
scaffoldGeometry.scaffoldSize.height - dy);
}

@override
bool operator ==(Object other) =>
other is CustomFloatingActionButtonLocation &&
other.dx == dx &&
other.dy == dy;

@override
int get hashCode => dx.hashCode + dy.hashCode;

@override
String toString() => 'CustomFloatingActionButtonLocation';
}
7 changes: 5 additions & 2 deletions sdk/python/packages/flet-core/src/flet_core/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
CrossAxisAlignment,
FloatingActionButtonLocation,
MainAxisAlignment,
OffsetValue,
PaddingValue,
PageDesignLanguage,
PageDesignString,
Expand Down Expand Up @@ -1343,12 +1344,14 @@ def floating_action_button(self, value: Optional[FloatingActionButton]):

# floating_action_button_location
@property
def floating_action_button_location(self) -> Optional[FloatingActionButtonLocation]:
def floating_action_button_location(
self,
) -> Union[FloatingActionButtonLocation, OffsetValue]:
return self.__default_view.floating_action_button_location

@floating_action_button_location.setter
def floating_action_button_location(
self, value: Optional[FloatingActionButtonLocation]
self, value: Union[FloatingActionButtonLocation, OffsetValue]
):
self.__default_view.floating_action_button_location = value

Expand Down
17 changes: 14 additions & 3 deletions sdk/python/packages/flet-core/src/flet_core/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MainAxisAlignmentString,
PaddingValue,
ScrollMode,
OffsetValue,
)


Expand All @@ -39,7 +40,9 @@ def __init__(
appbar: Union[AppBar, CupertinoAppBar, None] = None,
bottom_appbar: Optional[BottomAppBar] = None,
floating_action_button: Optional[FloatingActionButton] = None,
floating_action_button_location: Optional[FloatingActionButtonLocation] = None,
floating_action_button_location: Union[
FloatingActionButtonLocation, OffsetValue
] = None,
navigation_bar: Union[NavigationBar, CupertinoNavigationBar, None] = None,
drawer: Optional[NavigationDrawer] = None,
end_drawer: Optional[NavigationDrawer] = None,
Expand Down Expand Up @@ -91,6 +94,10 @@ def _get_control_name(self):
def _before_build_command(self):
super()._before_build_command()
self._set_attr_json("padding", self.__padding)
if not isinstance(self.__floating_action_button_location, (FloatingActionButtonLocation, str)):
self._set_attr_json(
"floatingActionButtonLocation", self.__floating_action_button_location
)

def _get_children(self):
children = []
Expand Down Expand Up @@ -158,11 +165,15 @@ def floating_action_button(self, value: Optional[FloatingActionButton]):

# floating_action_button_location
@property
def floating_action_button_location(self) -> FloatingActionButtonLocation:
def floating_action_button_location(
self,
) -> Union[FloatingActionButtonLocation, OffsetValue]:
return self.__floating_action_button_location

@floating_action_button_location.setter
def floating_action_button_location(self, value: FloatingActionButtonLocation):
def floating_action_button_location(
self, value: Union[FloatingActionButtonLocation, OffsetValue]
):
self.__floating_action_button_location = value
self._set_attr(
"floatingActionButtonLocation",
Expand Down

0 comments on commit ed672c5

Please sign in to comment.