Skip to content

Commit

Permalink
Add focus, onFocus, onBlur to SearchBar (#3417) (#3752)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaotic-dev authored Aug 5, 2024
1 parent 94a672e commit 55ea817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/flet/lib/src/controls/search_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,33 @@ class SearchAnchorControl extends StatefulWidget {

class _SearchAnchorControlState extends State<SearchAnchorControl> {
late final SearchController _controller;
bool _focused = false;
late final FocusNode _focusNode;
String? _lastFocusValue;

@override
void initState() {
super.initState();
_controller = SearchController();
_controller.addListener(_searchTextChanged);
_focusNode = FocusNode();
_focusNode.addListener(_onFocusChange);
}

void _onFocusChange() {
setState(() {
_focused = _focusNode.hasFocus;
});
widget.backend.triggerControlEvent(
widget.control.id, _focusNode.hasFocus ? "focus" : "blur");
}

@override
void dispose() {
_controller.removeListener(_searchTextChanged);
_controller.dispose();
_focusNode.removeListener(_onFocusChange);
_focusNode.dispose();
super.dispose();
}

Expand Down Expand Up @@ -102,6 +117,12 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
TextInputType keyboardType = parseTextInputType(
widget.control.attrString("keyboardType"), TextInputType.text)!;

var focusValue = widget.control.attrString("focus");
if (focusValue != null && focusValue != _lastFocusValue) {
_lastFocusValue = focusValue;
_focusNode.requestFocus();
}

var method = widget.control.attrString("method");

if (method != null) {
Expand Down Expand Up @@ -182,6 +203,7 @@ class _SearchAnchorControlState extends State<SearchAnchorControl> {
keyboardType: keyboardType,
textCapitalization: textCapitalization,
autoFocus: widget.control.attrBool("autoFocus", false)!,
focusNode: _focusNode,
hintText: widget.control.attrString("barHintText"),
backgroundColor: parseWidgetStateColor(
Theme.of(context), widget.control, "barBgcolor"),
Expand Down
26 changes: 26 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/search_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def __init__(
on_tap: OptionalEventCallable = None,
on_submit: OptionalEventCallable = None,
on_change: OptionalEventCallable = None,
on_focus: OptionalEventCallable = None,
on_blur: OptionalEventCallable = None,
#
# ConstrainedControl
#
Expand Down Expand Up @@ -131,6 +133,8 @@ def __init__(
self.divider_color = divider_color
self.full_screen = full_screen
self.capitalization = capitalization
self.on_focus = on_focus
self.on_blur = on_blur
self.on_tap = on_tap
self.on_submit = on_submit
self.on_change = on_change
Expand Down Expand Up @@ -173,6 +177,10 @@ def _get_children(self):
return children

# Public methods
def focus(self):
self._set_attr_json("focus", str(time.time()))
self.update()

def open_view(self):
m = {
"n": "openView",
Expand Down Expand Up @@ -418,6 +426,24 @@ def on_change(self, handler: OptionalControlEventCallable):
self._add_event_handler("change", handler)
self._set_attr("onchange", True if handler is not None else None)

# on_focus
@property
def on_focus(self) -> OptionalControlEventCallable:
return self._get_event_handler("focus")

@on_focus.setter
def on_focus(self, handler: OptionalControlEventCallable):
self._add_event_handler("focus", handler)

# on_blur
@property
def on_blur(self) -> OptionalControlEventCallable:
return self._get_event_handler("blur")

@on_blur.setter
def on_blur(self, handler: OptionalControlEventCallable):
self._add_event_handler("blur", handler)

# on_tap
@property
def on_tap(self) -> OptionalControlEventCallable:
Expand Down

0 comments on commit 55ea817

Please sign in to comment.