Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focus() method for TextField and Dropdown controls #11

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ for:
- path: client\build\windows\runner\flet.zip
name: flet_windows

deploy:
provider: GitHub
auth_token: $(GITHUB_TOKEN)
release: $(APPVEYOR_REPO_TAG_NAME)
artifact: flet_windows
on:
APPVEYOR_REPO_TAG: true

# ======================================
# Build Flet View for macOS
# ======================================
Expand Down Expand Up @@ -139,6 +147,14 @@ for:
- path: client/flet.app.tar.gz
name: flet_macos

deploy:
provider: GitHub
auth_token: $(GITHUB_TOKEN)
release: $(APPVEYOR_REPO_TAG_NAME)
artifact: flet_macos
on:
APPVEYOR_REPO_TAG: true

# ======================================
# Build Flet View for iOS
# ======================================
Expand Down
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