Skip to content

Commit

Permalink
.focus() method for TextField and Dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Jun 3, 2022
1 parent 9478c6e commit 307791b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
12 changes: 12 additions & 0 deletions client/lib/controls/dropdown.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

Expand Down Expand Up @@ -84,6 +86,16 @@ class _DropdownControlState extends State<DropdownControl> {
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<String>(
autofocus: autofocus,
focusNode: _focusNode,
Expand Down
16 changes: 15 additions & 1 deletion client/lib/controls/textfield.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_redux/flutter_redux.dart';
Expand Down Expand Up @@ -138,6 +140,18 @@ class _TextFieldControlState extends State<TextFieldControl> {
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,
Expand All @@ -161,7 +175,7 @@ class _TextFieldControlState extends State<TextFieldControl> {
readOnly: readOnly,
obscureText: password && !_revealPassword,
controller: _controller,
focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode,
focusNode: focusNode,
onChanged: (String value) {
//debugPrint(value);
setState(() {
Expand Down
13 changes: 0 additions & 13 deletions client/web/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">

<meta charset="UTF-8">
Expand Down
5 changes: 5 additions & 0 deletions sdk/python/flet/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions sdk/python/flet/focus.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions sdk/python/flet/textfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 307791b

Please sign in to comment.