Skip to content

Commit ca5dbc8

Browse files
committed
TextField and Dropdown unlim size fix
1 parent 073c824 commit ca5dbc8

File tree

9 files changed

+223
-83
lines changed

9 files changed

+223
-83
lines changed

client/lib/controls/dropdown.dart

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,57 +56,76 @@ class _DropdownControlState extends State<DropdownControl> {
5656
bool autofocus = widget.control.attrBool("autofocus", false)!;
5757
bool disabled = widget.control.isDisabled || widget.parentDisabled;
5858

59+
var items = itemsView.children
60+
.where((c) => c.name == null)
61+
.map<DropdownMenuItem<String>>(
62+
(Control itemCtrl) => DropdownMenuItem<String>(
63+
enabled: !(disabled || itemCtrl.isDisabled),
64+
value: itemCtrl.attrs["key"] ??
65+
itemCtrl.attrs["text"] ??
66+
itemCtrl.id,
67+
child: Text(itemCtrl.attrs["text"] ??
68+
itemCtrl.attrs["key"] ??
69+
itemCtrl.id),
70+
))
71+
.toList();
72+
5973
String? value = widget.control.attrString("value");
6074
if (_value != value) {
6175
_value = value;
6276
}
6377

78+
if (items.where((item) => item.value == value).isEmpty) {
79+
_value = null;
80+
}
81+
6482
var prefixControls = itemsView.children
6583
.where((c) => c.name == "prefix" && c.isVisible);
6684
var suffixControls = itemsView.children
6785
.where((c) => c.name == "suffix" && c.isVisible);
6886

69-
var dropDown = DropdownButtonFormField<String>(
70-
autofocus: autofocus,
71-
focusNode: _focusNode,
72-
value: _value,
73-
decoration: buildInputDecoration(
74-
widget.control,
75-
prefixControls.isNotEmpty ? prefixControls.first : null,
76-
suffixControls.isNotEmpty ? suffixControls.first : null,
77-
null),
78-
onChanged: (String? value) {
79-
debugPrint("Dropdown selected value: $value");
80-
setState(() {
81-
_value = value!;
82-
});
83-
List<Map<String, String>> props = [
84-
{"i": widget.control.id, "value": value!}
85-
];
86-
itemsView.dispatch(UpdateControlPropsAction(
87-
UpdateControlPropsPayload(props: props)));
88-
ws.updateControlProps(props: props);
89-
ws.pageEventFromWeb(
90-
eventTarget: widget.control.id,
91-
eventName: "change",
92-
eventData: value);
93-
},
94-
items: itemsView.children
95-
.where((c) => c.name == null)
96-
.map<DropdownMenuItem<String>>((Control itemCtrl) {
97-
return DropdownMenuItem<String>(
98-
enabled: !(disabled || itemCtrl.isDisabled),
99-
value: itemCtrl.attrs["key"] ??
100-
itemCtrl.attrs["text"] ??
101-
itemCtrl.id,
102-
child: Text(itemCtrl.attrs["text"] ??
103-
itemCtrl.attrs["key"] ??
104-
itemCtrl.id),
87+
return LayoutBuilder(
88+
builder: (BuildContext context, BoxConstraints constraints) {
89+
Widget dropDown = DropdownButtonFormField<String>(
90+
autofocus: autofocus,
91+
focusNode: _focusNode,
92+
value: _value,
93+
decoration: buildInputDecoration(
94+
widget.control,
95+
prefixControls.isNotEmpty ? prefixControls.first : null,
96+
suffixControls.isNotEmpty ? suffixControls.first : null,
97+
null),
98+
onChanged: (String? value) {
99+
debugPrint("Dropdown selected value: $value");
100+
setState(() {
101+
_value = value!;
102+
});
103+
List<Map<String, String>> props = [
104+
{"i": widget.control.id, "value": value!}
105+
];
106+
itemsView.dispatch(UpdateControlPropsAction(
107+
UpdateControlPropsPayload(props: props)));
108+
ws.updateControlProps(props: props);
109+
ws.pageEventFromWeb(
110+
eventTarget: widget.control.id,
111+
eventName: "change",
112+
eventData: value);
113+
},
114+
items: items,
105115
);
106-
}).toList(),
107-
);
108116

109-
return constrainedControl(dropDown, widget.parent, widget.control);
117+
if (constraints.maxWidth == double.infinity &&
118+
widget.control.attrDouble("width") == null) {
119+
dropDown = ConstrainedBox(
120+
constraints: const BoxConstraints.tightFor(width: 300),
121+
child: dropDown,
122+
);
123+
}
124+
125+
return constrainedControl(
126+
dropDown, widget.parent, widget.control);
127+
},
128+
);
110129
});
111130
}
112131
}

client/lib/controls/textfield.dart

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -138,50 +138,63 @@ class _TextFieldControlState extends State<TextFieldControl> {
138138
orElse: () => TextAlign.start,
139139
);
140140

141-
var textField = TextFormField(
142-
autofocus: autofocus,
143-
enabled: !disabled,
144-
onFieldSubmitted: !multiline
145-
? (_) {
141+
return LayoutBuilder(
142+
builder: (BuildContext context, BoxConstraints constraints) {
143+
Widget textField = TextFormField(
144+
autofocus: autofocus,
145+
enabled: !disabled,
146+
onFieldSubmitted: !multiline
147+
? (_) {
148+
ws.pageEventFromWeb(
149+
eventTarget: widget.control.id,
150+
eventName: "submit",
151+
eventData: "");
152+
}
153+
: null,
154+
decoration: buildInputDecoration(
155+
widget.control,
156+
prefixControls.isNotEmpty ? prefixControls.first : null,
157+
suffixControls.isNotEmpty ? suffixControls.first : null,
158+
revealPasswordIcon),
159+
keyboardType: keyboardType,
160+
textAlign: textAlign,
161+
minLines: minLines,
162+
maxLines: maxLines,
163+
readOnly: readOnly,
164+
obscureText: password && !_revealPassword,
165+
controller: _controller,
166+
focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode,
167+
onChanged: (String value) {
168+
//debugPrint(value);
169+
setState(() {
170+
_value = value;
171+
});
172+
List<Map<String, String>> props = [
173+
{"i": widget.control.id, "value": value}
174+
];
175+
dispatch(UpdateControlPropsAction(
176+
UpdateControlPropsPayload(props: props)));
177+
ws.updateControlProps(props: props);
178+
if (onChange) {
146179
ws.pageEventFromWeb(
147180
eventTarget: widget.control.id,
148-
eventName: "submit",
149-
eventData: "");
181+
eventName: "change",
182+
eventData: value);
150183
}
151-
: null,
152-
decoration: buildInputDecoration(
153-
widget.control,
154-
prefixControls.isNotEmpty ? prefixControls.first : null,
155-
suffixControls.isNotEmpty ? suffixControls.first : null,
156-
revealPasswordIcon),
157-
keyboardType: keyboardType,
158-
textAlign: textAlign,
159-
minLines: minLines,
160-
maxLines: maxLines,
161-
readOnly: readOnly,
162-
obscureText: password && !_revealPassword,
163-
controller: _controller,
164-
focusNode: shiftEnter ? _shiftEnterfocusNode : _focusNode,
165-
onChanged: (String value) {
166-
//debugPrint(value);
167-
setState(() {
168-
_value = value;
169-
});
170-
List<Map<String, String>> props = [
171-
{"i": widget.control.id, "value": value}
172-
];
173-
dispatch(UpdateControlPropsAction(
174-
UpdateControlPropsPayload(props: props)));
175-
ws.updateControlProps(props: props);
176-
if (onChange) {
177-
ws.pageEventFromWeb(
178-
eventTarget: widget.control.id,
179-
eventName: "change",
180-
eventData: value);
181-
}
182-
});
183-
184-
return constrainedControl(textField, widget.parent, widget.control);
184+
});
185+
186+
if (constraints.maxWidth == double.infinity &&
187+
widget.control.attrDouble("width") == null) {
188+
textField = ConstrainedBox(
189+
constraints: const BoxConstraints.tightFor(width: 300),
190+
child: textField,
191+
);
192+
}
193+
194+
return constrainedControl(
195+
textField, widget.parent, widget.control);
196+
},
197+
);
185198
});
186199
}
187200
}

client/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main([List<String>? args]) async {
3030
var pageUri = Uri.base;
3131

3232
if (kDebugMode) {
33-
pageUri = Uri.parse("http://192.168.0.12:8550");
33+
pageUri = Uri.parse("http://localhost:8550");
3434
}
3535

3636
if (kIsWeb) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import flet
2+
from flet import Dropdown, ElevatedButton, Page, Text, dropdown
3+
4+
5+
def main(page: Page):
6+
def button_clicked(e):
7+
t.value = f"Dropdown value is: {dd.value}"
8+
page.update()
9+
10+
t = Text()
11+
b = ElevatedButton(text="Submit", on_click=button_clicked)
12+
dd = Dropdown(
13+
width=100,
14+
options=[
15+
dropdown.Option("Red"),
16+
dropdown.Option("Green"),
17+
dropdown.Option("Blue"),
18+
],
19+
)
20+
page.add(dd, b, t)
21+
22+
23+
flet.app(target=main)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import flet
2+
from flet import (
3+
Column,
4+
Container,
5+
Dropdown,
6+
ElevatedButton,
7+
OutlinedButton,
8+
Page,
9+
Row,
10+
TextField,
11+
dropdown,
12+
)
13+
14+
15+
def main(page: Page):
16+
def find_option(option_name):
17+
for option in d.options:
18+
if option_name == option.key:
19+
return option
20+
return None
21+
22+
def add_clicked(e):
23+
d.options.append(dropdown.Option(option_textbox.value))
24+
d.value = option_textbox.value
25+
option_textbox.value = ""
26+
page.update()
27+
28+
def delete_clicked(e):
29+
option = find_option(d.value)
30+
if option != None:
31+
d.options.remove(option)
32+
# d.value = None
33+
page.update()
34+
35+
d = Dropdown()
36+
option_textbox = TextField(hint_text="Enter item name")
37+
add = ElevatedButton("Add", on_click=add_clicked)
38+
delete = OutlinedButton("Delete selected", on_click=delete_clicked)
39+
page.add(d, Row(controls=[option_textbox, add, delete]))
40+
41+
42+
flet.app(port=8550, target=main, view=flet.WEB_BROWSER)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import flet
2+
from flet import Dropdown, Page, dropdown
3+
4+
5+
def main(page: Page):
6+
page.add(
7+
Dropdown(
8+
label="Color",
9+
hint_text="Choose your favourite color?",
10+
options=[
11+
dropdown.Option("Red"),
12+
dropdown.Option("Green"),
13+
dropdown.Option("Blue"),
14+
],
15+
autofocus=True,
16+
)
17+
)
18+
19+
20+
flet.app(target=main)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import flet
2+
from flet import Dropdown, Page, Text, dropdown
3+
4+
5+
def main(page: Page):
6+
def dropdown_changed(e):
7+
t.value = f"Dropdown changed to {dd.value}"
8+
page.update()
9+
10+
t = Text()
11+
dd = Dropdown(
12+
on_change=dropdown_changed,
13+
options=[
14+
dropdown.Option("Red"),
15+
dropdown.Option("Green"),
16+
dropdown.Option("Blue"),
17+
],
18+
width=200,
19+
)
20+
page.add(dd, t)
21+
22+
23+
flet.app(target=main)

sdk/python/flet/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Container(ConstrainedControl):
2020
def __init__(
2121
self,
22+
content: Control = None,
2223
ref: Ref = None,
2324
width: OptionalNumber = None,
2425
height: OptionalNumber = None,
@@ -31,7 +32,6 @@ def __init__(
3132
#
3233
# Specific
3334
#
34-
content: Control = None,
3535
padding: PaddingValue = None,
3636
margin: MarginValue = None,
3737
alignment: Alignment = None,

sdk/python/flet/dropdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def __init__(
4242
#
4343
value: str = None,
4444
autofocus: bool = None,
45+
options=None,
4546
on_change=None,
4647
on_focus=None,
4748
on_blur=None,
48-
options=None,
4949
):
5050
FormFieldControl.__init__(
5151
self,

0 commit comments

Comments
 (0)