Skip to content

Commit

Permalink
Multiline textboxes done right
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed May 1, 2022
1 parent ef697ef commit e8b09f4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/lib/controls/form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TextInputType parseTextInputType(String type) {
case "visiblepassword":
return TextInputType.visiblePassword;
}
return TextInputType.none;
return TextInputType.text;
}

InputDecoration buildInputDecoration(
Expand Down
7 changes: 6 additions & 1 deletion client/lib/controls/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ class PageControl extends StatelessWidget {
colorScheme: lightColorScheme,
brightness: Brightness.light,
useMaterial3: true,
// fontFamily: kIsWeb && window.navigator.userAgent.contains('OS 15_')
// ? '-apple-system'
// : null,
visualDensity: VisualDensity.adaptivePlatformDensity);

var darkTheme = parseTheme(control, "darkTheme") ??
ThemeData(
//colorSchemeSeed: const Color.fromARGB(255, 104, 192, 233),
colorScheme: darkColorScheme,
brightness: Brightness.dark,
useMaterial3: true,
// fontFamily: kIsWeb && window.navigator.userAgent.contains('OS 15_')
// ? '-apple-system'
// : null,
visualDensity: VisualDensity.adaptivePlatformDensity);

var themeMode = ThemeMode.values.firstWhere(
Expand Down
33 changes: 17 additions & 16 deletions client/lib/controls/textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ class _TextFieldControlState extends State<TextFieldControl> {
var prefixControls = widget.children.where((c) => c.name == "prefix");
var suffixControls = widget.children.where((c) => c.name == "suffix");

int? minLines = widget.control.attrInt("minLines");
int? maxLines = widget.control.attrInt("maxLines");
bool shiftEnter = widget.control.attrBool("shiftEnter", false)!;
bool multiline =
widget.control.attrBool("multiline", false)! || shiftEnter;
int minLines = widget.control.attrInt("minLines", 1)!;
int? maxLines =
widget.control.attrInt("maxLines", multiline ? null : 1);

bool readOnly = widget.control.attrBool("readOnly", false)!;
bool password = widget.control.attrBool("password", false)!;
bool shiftEnter = widget.control.attrBool("shiftEnter", false)!;
bool canRevealPassword =
widget.control.attrBool("canRevealPassword", false)!;
bool onChange = widget.control.attrBool("onChange", false)!;
Expand All @@ -122,9 +125,7 @@ class _TextFieldControlState extends State<TextFieldControl> {
TextInputType keyboardType = parseTextInputType(
widget.control.attrString("keyboardType", "")!);

if (keyboardType == TextInputType.none &&
minLines != null &&
minLines > 0) {
if (multiline) {
keyboardType = TextInputType.multiline;
}

Expand All @@ -138,12 +139,14 @@ class _TextFieldControlState extends State<TextFieldControl> {
var textField = TextFormField(
autofocus: autofocus,
enabled: !disabled,
onFieldSubmitted: (_) {
ws.pageEventFromWeb(
eventTarget: widget.control.id,
eventName: "submit",
eventData: "");
},
onFieldSubmitted: !multiline
? (_) {
ws.pageEventFromWeb(
eventTarget: widget.control.id,
eventName: "submit",
eventData: "");
}
: null,
decoration: buildInputDecoration(
widget.control,
prefixControls.isNotEmpty ? prefixControls.first : null,
Expand All @@ -152,13 +155,11 @@ class _TextFieldControlState extends State<TextFieldControl> {
keyboardType: keyboardType,
textAlign: textAlign,
minLines: minLines,
maxLines: password ? 1 : maxLines,
maxLines: maxLines,
readOnly: readOnly,
obscureText: password && !_revealPassword,
controller: _controller,
focusNode: keyboardType == TextInputType.multiline && shiftEnter
? _shiftEnterfocusNode
: _focusNode,
focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode,
onChanged: (String value) {
//debugPrint(value);
setState(() {
Expand Down
1 change: 1 addition & 0 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ Properties:

- value
- keyboardType
- multiline
- minLines
- maxLines
- password
Expand Down
12 changes: 12 additions & 0 deletions sdk/python/flet/textfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
#
value: str = None,
keyboard_type: TextInputType = None,
multiline: bool = None,
min_lines: int = None,
max_lines: int = None,
password: bool = None,
Expand Down Expand Up @@ -108,6 +109,7 @@ def __init__(
self.value = value
self.keyboard_type = keyboard_type
self.text_align = text_align
self.multiline = multiline
self.min_lines = min_lines
self.max_lines = max_lines
self.read_only = read_only
Expand Down Expand Up @@ -152,6 +154,16 @@ def text_align(self):
def text_align(self, value: TextAlign):
self._set_attr("textAlign", value)

# multiline
@property
def multiline(self):
return self._get_attr("multiline", data_type="bool", def_value=False)

@multiline.setter
@beartype
def multiline(self, value: Optional[bool]):
self._set_attr("multiline", value)

# min_lines
@property
def min_lines(self):
Expand Down
5 changes: 3 additions & 2 deletions sdk/python/playground/textfield-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def chat_submit(e):

chat_input = TextField(
hint_text="Say something...",
min_lines=1,
shift_enter=True,
min_lines=1,
on_submit=chat_submit,
max_lines=5,
)

form = Column(
Expand All @@ -83,7 +84,7 @@ def chat_submit(e):
helper_text="Tell something about us",
border="underline",
filled=True,
min_lines=1,
multiline=True,
),
Text(
"New line - Shift + Enter and submit on Enter",
Expand Down

0 comments on commit e8b09f4

Please sign in to comment.