From 307791b56ad6b520e18f712002410f998b78ff55 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Fri, 3 Jun 2022 12:28:53 -0700 Subject: [PATCH] .focus() method for TextField and Dropdown --- client/lib/controls/dropdown.dart | 12 ++++++++++++ client/lib/controls/textfield.dart | 16 +++++++++++++++- client/web/index.html | 13 ------------- sdk/python/flet/dropdown.py | 5 +++++ sdk/python/flet/focus.py | 12 ++++++++++++ sdk/python/flet/textfield.py | 5 +++++ 6 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 sdk/python/flet/focus.py diff --git a/client/lib/controls/dropdown.dart b/client/lib/controls/dropdown.dart index fd088677c..4b03a8afc 100644 --- a/client/lib/controls/dropdown.dart +++ b/client/lib/controls/dropdown.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:flutter_redux/flutter_redux.dart'; @@ -84,6 +86,16 @@ class _DropdownControlState extends State { var suffixControls = itemsView.children .where((c) => c.name == "suffix" && c.isVisible); + var focusValue = widget.control.attrString("focus"); + if (focusValue != null) { + debugPrint("Focus JSON value: $focusValue"); + var jv = json.decode(focusValue); + var focus = jv["d"] as bool; + if (focus) { + _focusNode.requestFocus(); + } + } + Widget dropDown = DropdownButtonFormField( autofocus: autofocus, focusNode: _focusNode, diff --git a/client/lib/controls/textfield.dart b/client/lib/controls/textfield.dart index e472aa520..6013c14bd 100644 --- a/client/lib/controls/textfield.dart +++ b/client/lib/controls/textfield.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_redux/flutter_redux.dart'; @@ -138,6 +140,18 @@ class _TextFieldControlState extends State { orElse: () => TextAlign.start, ); + FocusNode focusNode = shiftEnter ? _shiftEnterfocusNode : _focusNode; + + var focusValue = widget.control.attrString("focus"); + if (focusValue != null) { + debugPrint("Focus JSON value: $focusValue"); + var jv = json.decode(focusValue); + var focus = jv["d"] as bool; + if (focus) { + focusNode.requestFocus(); + } + } + Widget textField = TextFormField( autofocus: autofocus, enabled: !disabled, @@ -161,7 +175,7 @@ class _TextFieldControlState extends State { readOnly: readOnly, obscureText: password && !_revealPassword, controller: _controller, - focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode, + focusNode: focusNode, onChanged: (String value) { //debugPrint(value); setState(() { diff --git a/client/web/index.html b/client/web/index.html index 111dbe083..629f9d5a2 100644 --- a/client/web/index.html +++ b/client/web/index.html @@ -1,19 +1,6 @@ - diff --git a/sdk/python/flet/dropdown.py b/sdk/python/flet/dropdown.py index 0c66f4846..c0618df14 100644 --- a/sdk/python/flet/dropdown.py +++ b/sdk/python/flet/dropdown.py @@ -3,6 +3,7 @@ from beartype import beartype from flet.control import Control, InputBorder, OptionalNumber, PaddingValue +from flet.focus import FocusData from flet.form_field_control import FormFieldControl from flet.ref import Ref @@ -94,6 +95,10 @@ def _get_children(self): result.extend(self.__options) return result + def focus(self): + self._set_attr_json("focus", FocusData()) + self.update() + # options @property def options(self): diff --git a/sdk/python/flet/focus.py b/sdk/python/flet/focus.py new file mode 100644 index 000000000..c72dfcfb3 --- /dev/null +++ b/sdk/python/flet/focus.py @@ -0,0 +1,12 @@ +import dataclasses +import time +from typing import Optional + +from beartype._decor.main import beartype + + +@beartype +@dataclasses.dataclass +class FocusData: + ts: str = dataclasses.field(default=str(time.time())) + d: Optional[str] = dataclasses.field(default=True) diff --git a/sdk/python/flet/textfield.py b/sdk/python/flet/textfield.py index 42a5ba539..f25181a20 100644 --- a/sdk/python/flet/textfield.py +++ b/sdk/python/flet/textfield.py @@ -3,6 +3,7 @@ from beartype import beartype from flet.control import Control, InputBorder, OptionalNumber, PaddingValue, TextAlign +from flet.focus import FocusData from flet.form_field_control import FormFieldControl from flet.ref import Ref @@ -125,6 +126,10 @@ def __init__( def _get_control_name(self): return "textfield" + def focus(self): + self._set_attr_json("focus", FocusData()) + self.update() + # value @property def value(self):