Skip to content

Commit 307791b

Browse files
committed
.focus() method for TextField and Dropdown
1 parent 9478c6e commit 307791b

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

client/lib/controls/dropdown.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_redux/flutter_redux.dart';
35

@@ -84,6 +86,16 @@ class _DropdownControlState extends State<DropdownControl> {
8486
var suffixControls = itemsView.children
8587
.where((c) => c.name == "suffix" && c.isVisible);
8688

89+
var focusValue = widget.control.attrString("focus");
90+
if (focusValue != null) {
91+
debugPrint("Focus JSON value: $focusValue");
92+
var jv = json.decode(focusValue);
93+
var focus = jv["d"] as bool;
94+
if (focus) {
95+
_focusNode.requestFocus();
96+
}
97+
}
98+
8799
Widget dropDown = DropdownButtonFormField<String>(
88100
autofocus: autofocus,
89101
focusNode: _focusNode,

client/lib/controls/textfield.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter/services.dart';
35
import 'package:flutter_redux/flutter_redux.dart';
@@ -138,6 +140,18 @@ class _TextFieldControlState extends State<TextFieldControl> {
138140
orElse: () => TextAlign.start,
139141
);
140142

143+
FocusNode focusNode = shiftEnter ? _shiftEnterfocusNode : _focusNode;
144+
145+
var focusValue = widget.control.attrString("focus");
146+
if (focusValue != null) {
147+
debugPrint("Focus JSON value: $focusValue");
148+
var jv = json.decode(focusValue);
149+
var focus = jv["d"] as bool;
150+
if (focus) {
151+
focusNode.requestFocus();
152+
}
153+
}
154+
141155
Widget textField = TextFormField(
142156
autofocus: autofocus,
143157
enabled: !disabled,
@@ -161,7 +175,7 @@ class _TextFieldControlState extends State<TextFieldControl> {
161175
readOnly: readOnly,
162176
obscureText: password && !_revealPassword,
163177
controller: _controller,
164-
focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode,
178+
focusNode: focusNode,
165179
onChanged: (String value) {
166180
//debugPrint(value);
167181
setState(() {

client/web/index.html

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<!--
5-
If you are serving your web app in a path other than the root, change the
6-
href value below to reflect the base path you are serving from.
7-
8-
The path provided below has to start and end with a slash "/" in order for
9-
it to work correctly.
10-
11-
For more details:
12-
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
13-
14-
This is a placeholder for base href that will be replaced by the value of
15-
the `--base-href` argument provided to `flutter build`.
16-
-->
174
<base href="$FLUTTER_BASE_HREF">
185

196
<meta charset="UTF-8">

sdk/python/flet/dropdown.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from beartype import beartype
44

55
from flet.control import Control, InputBorder, OptionalNumber, PaddingValue
6+
from flet.focus import FocusData
67
from flet.form_field_control import FormFieldControl
78
from flet.ref import Ref
89

@@ -94,6 +95,10 @@ def _get_children(self):
9495
result.extend(self.__options)
9596
return result
9697

98+
def focus(self):
99+
self._set_attr_json("focus", FocusData())
100+
self.update()
101+
97102
# options
98103
@property
99104
def options(self):

sdk/python/flet/focus.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import dataclasses
2+
import time
3+
from typing import Optional
4+
5+
from beartype._decor.main import beartype
6+
7+
8+
@beartype
9+
@dataclasses.dataclass
10+
class FocusData:
11+
ts: str = dataclasses.field(default=str(time.time()))
12+
d: Optional[str] = dataclasses.field(default=True)

sdk/python/flet/textfield.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from beartype import beartype
44

55
from flet.control import Control, InputBorder, OptionalNumber, PaddingValue, TextAlign
6+
from flet.focus import FocusData
67
from flet.form_field_control import FormFieldControl
78
from flet.ref import Ref
89

@@ -125,6 +126,10 @@ def __init__(
125126
def _get_control_name(self):
126127
return "textfield"
127128

129+
def focus(self):
130+
self._set_attr_json("focus", FocusData())
131+
self.update()
132+
128133
# value
129134
@property
130135
def value(self):

0 commit comments

Comments
 (0)