From 08859fc862f5cd9a7ae1dfb09e456862f0e44b12 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Mon, 30 May 2022 20:29:32 -0700 Subject: [PATCH] Fix expand for TextField and Dropdown --- client/lib/controls/dropdown.dart | 84 +++++++++++----------- client/lib/controls/textfield.dart | 112 +++++++++++++++-------------- sdk/python/playground/hello.py | 14 ++++ 3 files changed, 116 insertions(+), 94 deletions(-) create mode 100644 sdk/python/playground/hello.py diff --git a/client/lib/controls/dropdown.dart b/client/lib/controls/dropdown.dart index 87720ddba..fd088677c 100644 --- a/client/lib/controls/dropdown.dart +++ b/client/lib/controls/dropdown.dart @@ -84,48 +84,52 @@ class _DropdownControlState extends State { var suffixControls = itemsView.children .where((c) => c.name == "suffix" && c.isVisible); - return LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { - Widget dropDown = DropdownButtonFormField( - autofocus: autofocus, - focusNode: _focusNode, - value: _value, - decoration: buildInputDecoration( - widget.control, - prefixControls.isNotEmpty ? prefixControls.first : null, - suffixControls.isNotEmpty ? suffixControls.first : null, - null), - onChanged: (String? value) { - debugPrint("Dropdown selected value: $value"); - setState(() { - _value = value!; - }); - List> props = [ - {"i": widget.control.id, "value": value!} - ]; - itemsView.dispatch(UpdateControlPropsAction( - UpdateControlPropsPayload(props: props))); - ws.updateControlProps(props: props); - ws.pageEventFromWeb( - eventTarget: widget.control.id, - eventName: "change", - eventData: value); - }, - items: items, - ); - - if (constraints.maxWidth == double.infinity && - widget.control.attrDouble("width") == null) { - dropDown = ConstrainedBox( - constraints: const BoxConstraints.tightFor(width: 300), - child: dropDown, - ); - } - - return constrainedControl( - dropDown, widget.parent, widget.control); + Widget dropDown = DropdownButtonFormField( + autofocus: autofocus, + focusNode: _focusNode, + value: _value, + decoration: buildInputDecoration( + widget.control, + prefixControls.isNotEmpty ? prefixControls.first : null, + suffixControls.isNotEmpty ? suffixControls.first : null, + null), + onChanged: (String? value) { + debugPrint("Dropdown selected value: $value"); + setState(() { + _value = value!; + }); + List> props = [ + {"i": widget.control.id, "value": value!} + ]; + itemsView.dispatch(UpdateControlPropsAction( + UpdateControlPropsPayload(props: props))); + ws.updateControlProps(props: props); + ws.pageEventFromWeb( + eventTarget: widget.control.id, + eventName: "change", + eventData: value); }, + items: items, ); + + if (widget.control.attrInt("expand", 0)! > 0) { + return constrainedControl(dropDown, widget.parent, widget.control); + } else { + return LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + if (constraints.maxWidth == double.infinity && + widget.control.attrDouble("width") == null) { + dropDown = ConstrainedBox( + constraints: const BoxConstraints.tightFor(width: 300), + child: dropDown, + ); + } + + return constrainedControl( + dropDown, widget.parent, widget.control); + }, + ); + } }); } } diff --git a/client/lib/controls/textfield.dart b/client/lib/controls/textfield.dart index b7d16de59..e472aa520 100644 --- a/client/lib/controls/textfield.dart +++ b/client/lib/controls/textfield.dart @@ -138,63 +138,67 @@ class _TextFieldControlState extends State { orElse: () => TextAlign.start, ); - return LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { - Widget textField = TextFormField( - autofocus: autofocus, - enabled: !disabled, - onFieldSubmitted: !multiline - ? (_) { - ws.pageEventFromWeb( - eventTarget: widget.control.id, - eventName: "submit", - eventData: ""); - } - : null, - decoration: buildInputDecoration( - widget.control, - prefixControls.isNotEmpty ? prefixControls.first : null, - suffixControls.isNotEmpty ? suffixControls.first : null, - revealPasswordIcon), - keyboardType: keyboardType, - textAlign: textAlign, - minLines: minLines, - maxLines: maxLines, - readOnly: readOnly, - obscureText: password && !_revealPassword, - controller: _controller, - focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode, - onChanged: (String value) { - //debugPrint(value); - setState(() { - _value = value; - }); - List> props = [ - {"i": widget.control.id, "value": value} - ]; - dispatch(UpdateControlPropsAction( - UpdateControlPropsPayload(props: props))); - ws.updateControlProps(props: props); - if (onChange) { + Widget textField = TextFormField( + autofocus: autofocus, + enabled: !disabled, + onFieldSubmitted: !multiline + ? (_) { ws.pageEventFromWeb( eventTarget: widget.control.id, - eventName: "change", - eventData: value); + eventName: "submit", + eventData: ""); } - }); - - if (constraints.maxWidth == double.infinity && - widget.control.attrDouble("width") == null) { - textField = ConstrainedBox( - constraints: const BoxConstraints.tightFor(width: 300), - child: textField, - ); - } - - return constrainedControl( - textField, widget.parent, widget.control); - }, - ); + : null, + decoration: buildInputDecoration( + widget.control, + prefixControls.isNotEmpty ? prefixControls.first : null, + suffixControls.isNotEmpty ? suffixControls.first : null, + revealPasswordIcon), + keyboardType: keyboardType, + textAlign: textAlign, + minLines: minLines, + maxLines: maxLines, + readOnly: readOnly, + obscureText: password && !_revealPassword, + controller: _controller, + focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode, + onChanged: (String value) { + //debugPrint(value); + setState(() { + _value = value; + }); + List> props = [ + {"i": widget.control.id, "value": value} + ]; + dispatch(UpdateControlPropsAction( + UpdateControlPropsPayload(props: props))); + ws.updateControlProps(props: props); + if (onChange) { + ws.pageEventFromWeb( + eventTarget: widget.control.id, + eventName: "change", + eventData: value); + } + }); + + if (widget.control.attrInt("expand", 0)! > 0) { + return constrainedControl(textField, widget.parent, widget.control); + } else { + return LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + if (constraints.maxWidth == double.infinity && + widget.control.attrDouble("width") == null) { + textField = ConstrainedBox( + constraints: const BoxConstraints.tightFor(width: 300), + child: textField, + ); + } + + return constrainedControl( + textField, widget.parent, widget.control); + }, + ); + } }); } } diff --git a/sdk/python/playground/hello.py b/sdk/python/playground/hello.py new file mode 100644 index 000000000..d1b86b514 --- /dev/null +++ b/sdk/python/playground/hello.py @@ -0,0 +1,14 @@ +import flet +from flet import Dropdown, Page, Row, Text, TextField + + +def main(page: Page): + page.add(Row( + [Dropdown( + #expand=True, + autofocus=True, + )] + )) + + +flet.app(target=main, view=flet.WEB_BROWSER)