Skip to content
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
32 changes: 21 additions & 11 deletions package/lib/src/controls/dropdown.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:flet/src/utils/alignment.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

Expand Down Expand Up @@ -93,19 +94,26 @@ class _DropdownControlState extends State<DropdownControl> {
Theme.of(context).colorScheme.onSurface);
}

var alignment = parseAlignment(widget.control, "alignment");

var items = itemsView.children
.where((c) => c.name == null)
.map<DropdownMenuItem<String>>(
(Control itemCtrl) => DropdownMenuItem<String>(
enabled: !(disabled || itemCtrl.isDisabled),
value: itemCtrl.attrs["key"] ??
itemCtrl.attrs["text"] ??
itemCtrl.id,
child: Text(itemCtrl.attrs["text"] ??
itemCtrl.attrs["key"] ??
itemCtrl.id),
))
.toList();
.map<DropdownMenuItem<String>>((Control itemCtrl) {
Widget itemChild = Text(
itemCtrl.attrs["text"] ?? itemCtrl.attrs["key"] ?? itemCtrl.id,
);

if (alignment != null) {
itemChild = Container(alignment: alignment, child: itemChild);
}
return DropdownMenuItem<String>(
enabled: !(disabled || itemCtrl.isDisabled),
value: itemCtrl.attrs["key"] ??
itemCtrl.attrs["text"] ??
itemCtrl.id,
child: itemChild,
);
}).toList();

String? value = widget.control.attrString("value");
if (_value != value) {
Expand Down Expand Up @@ -139,6 +147,8 @@ class _DropdownControlState extends State<DropdownControl> {
focusNode: _focusNode,
value: _value,
borderRadius: borderRadius,
alignment: alignment ?? AlignmentDirectional.centerStart,
isExpanded: alignment != null,
decoration: buildInputDecoration(
context,
widget.control,
Expand Down
1 change: 1 addition & 0 deletions sdk/python/flet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
border,
border_radius,
colors,
dropdown,
icons,
margin,
padding,
Expand Down
17 changes: 17 additions & 0 deletions sdk/python/flet/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from beartype import beartype

from flet.alignment import Alignment
from flet.control import Control, OptionalNumber
from flet.focus import FocusData
from flet.form_field_control import FormFieldControl, InputBorder
Expand Down Expand Up @@ -84,6 +85,7 @@ def __init__(
value: Optional[str] = None,
autofocus: Optional[bool] = None,
options=None,
alignment: Optional[Alignment] = None,
on_change=None,
on_focus=None,
on_blur=None,
Expand Down Expand Up @@ -153,13 +155,18 @@ def __init__(
self.value = value
self.autofocus = autofocus
self.options = options
self.alignment = alignment
self.on_focus = on_focus
self.on_blur = on_blur
self.on_change = on_change

def _get_control_name(self):
return "dropdown"

def _before_build_command(self):
super()._before_build_command()
self._set_attr_json("alignment", self.__alignment)

def _get_children(self):
result = FormFieldControl._get_children(self)
result.extend(self.__options)
Expand Down Expand Up @@ -197,6 +204,16 @@ def autofocus(self) -> Optional[bool]:
def autofocus(self, value: Optional[bool]):
self._set_attr("autofocus", value)

# alignment
@property
def alignment(self) -> Optional[Alignment]:
return self.__alignment

@alignment.setter
@beartype
def alignment(self, value: Optional[Alignment]):
self.__alignment = value

# on_change
@property
def on_change(self):
Expand Down