Skip to content

Commit

Permalink
NavigationRail selected_index
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed May 16, 2022
1 parent 0e60f56 commit fd8728b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 67 deletions.
54 changes: 16 additions & 38 deletions client/lib/controls/navigation_rail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,41 @@ class NavigationRailControl extends StatefulWidget {
}

class _NavigationRailControlState extends State<NavigationRailControl> {
List<String> _destIndex = [];
String? _value;
int? _selectedIndex;
dynamic _dispatch;

@override
void initState() {
super.initState();
_destIndex = widget.children
.where((c) => c.isVisible && c.name == null)
.map((c) => c.attrString("key") ?? c.attrString("label", "")!)
.toList();
}

void _destinationChanged(int index) {
_selectedIndex = index;
var value = _destIndex[index];
if (_value != value) {
debugPrint("Selected dest: $value");
List<Map<String, String>> props = [
{"i": widget.control.id, "value": value}
];
_dispatch(
UpdateControlPropsAction(UpdateControlPropsPayload(props: props)));
ws.updateControlProps(props: props);
ws.pageEventFromWeb(
eventTarget: widget.control.id,
eventName: "change",
eventData: value);
}
_value = value;
debugPrint("Selected index: $_selectedIndex");
List<Map<String, String>> props = [
{"i": widget.control.id, "selectedindex": _selectedIndex.toString()}
];
_dispatch(
UpdateControlPropsAction(UpdateControlPropsPayload(props: props)));
ws.updateControlProps(props: props);
ws.pageEventFromWeb(
eventTarget: widget.control.id,
eventName: "change",
eventData: _selectedIndex.toString());
}

@override
Widget build(BuildContext context) {
debugPrint("NavigationRailControl build: ${widget.control.id}");

var destIndex = widget.children
.where((c) => c.isVisible && c.name == null)
.map((c) => c.attrString("key") ?? c.attrString("label", "")!)
.toList();
if (destIndex.length != _destIndex.length ||
!destIndex.every((item) => _destIndex.contains(item))) {
_destIndex = destIndex;
}
var selectedIndex = widget.control.attrInt("selectedIndex");

bool disabled = widget.control.isDisabled || widget.parentDisabled;
debugPrint(selectedIndex.toString());

String? value = widget.control.attrString("value");

if (_value != value) {
_value = value;
bool disabled = widget.control.isDisabled || widget.parentDisabled;

int idx = _destIndex.indexOf(_value ?? "");
if (idx != -1) {
_selectedIndex = idx;
}
if (_selectedIndex != selectedIndex) {
_selectedIndex = selectedIndex;
}

NavigationRailLabelType? labelType = NavigationRailLabelType.values
Expand Down
2 changes: 1 addition & 1 deletion client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ packages:
name: flutter_redux
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.0-beta.1"
version: "0.10.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
redux: ^5.0.0
flutter_redux: ^0.10.0-beta.1
flutter_redux: ^0.10.0
equatable: ^2.0.3
web_socket_channel: ^2.1.0
window_manager: ^0.2.1
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/flet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def _get_attr(self, name, def_value=None, data_type="string"):
return s_val.lower() == "true"
elif data_type == "float" and s_val != None and isinstance(s_val, str):
return float(s_val)
elif data_type == "int" and s_val != None and isinstance(s_val, str):
return int(s_val)
else:
return s_val

Expand Down
29 changes: 8 additions & 21 deletions sdk/python/flet/navigation_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class NavigationRailDestination(Control):
def __init__(
self,
ref: Ref = None,
key: str = None,
icon: str = None,
icon_content: Control = None,
selected_icon: str = None,
Expand All @@ -27,8 +26,6 @@ def __init__(
padding: PaddingValue = None,
):
Control.__init__(self, ref=ref)
assert key or label, "key or label must be specified"
self.key = key
self.label = label
self.icon = icon
self.__icon_content: Control = None
Expand Down Expand Up @@ -58,15 +55,6 @@ def _get_children(self):
children.append(self.__selected_icon_content)
return children

# key
@property
def key(self):
return self._get_attr("key")

@key.setter
def key(self, value):
self._set_attr("key", value)

# icon
@property
def icon(self):
Expand Down Expand Up @@ -154,7 +142,7 @@ def __init__(
#
# NavigationRail-specific
destinations: List[NavigationRailDestination] = None,
value: str = None,
selected_index: int = None,
extended: bool = None,
label_type: NavigationRailLabelType = None,
bgcolor: str = None,
Expand All @@ -179,7 +167,7 @@ def __init__(
)

self.destinations = destinations
self.value = value
self.selected_index = selected_index
self.extended = extended
self.label_type = label_type
self.bgcolor = bgcolor
Expand Down Expand Up @@ -216,7 +204,6 @@ def destinations(self):
def destinations(self, value: Optional[List[NavigationRailDestination]]):
value = value or []
self.__destinations = value
self.value = value and value[0].key or ""

# on_change
@property
Expand All @@ -227,15 +214,15 @@ def on_change(self):
def on_change(self, handler):
self._add_event_handler("change", handler)

# value
# selected_index
@property
def value(self):
return self._get_attr("value")
def selected_index(self):
return self._get_attr("selectedIndex", data_type="int")

@value.setter
@selected_index.setter
@beartype
def value(self, value: Optional[str]):
self._set_attr("value", value)
def selected_index(self, value: Optional[int]):
self._set_attr("selectedIndex", value)

# label_type
@property
Expand Down
40 changes: 34 additions & 6 deletions sdk/python/playground/rail-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,40 @@
icons,
)
from flet.divider import Divider
from flet.vertical_divider import VerticalDivider

logging.basicConfig(level=logging.DEBUG)


def main(page: Page):
def fab_click(e):
rail.value = "third"
rail.selected_index = 2
select_page()
page.update()

pages = [
Text("First", visible=False),
Text("Second!", visible=False),
Text("Settings", visible=False),
]

def select_page():
print(f"Selected index: {rail.selected_index}")
for index, p in enumerate(pages):
p.visible = True if index == rail.selected_index else False
page.update()

def dest_change(e):
select_page()

rail = NavigationRail(
value="Second",
selected_index=0,
label_type="all",
# extended=True,
min_width=100,
min_extended_width=400,
leading=FloatingActionButton(icon=icons.CREATE, text="Add", on_click=fab_click),
trailing=Text("Something"),
# trailing=Text("Something"),
group_alignment=-0.9,
destinations=[
NavigationRailDestination(
Expand All @@ -42,13 +59,24 @@ def fab_click(e):
label="Second",
),
NavigationRailDestination(
key="third", icon=icons.STAR_BORDER, label_content=Text("Third")
icon=icons.SETTINGS, label_content=Text("Settings")
),
],
on_change=lambda e: print(f"Selected tab: {e.control.value}"),
on_change=dest_change,
)

page.add(Row([rail], expand=True))
select_page()

page.add(
Row(
[
rail,
VerticalDivider(width=1),
Column(pages, alignment="start", expand=True),
],
expand=True,
)
)


flet.app(name="test1", port=8550, target=main, view=flet.WEB_BROWSER)

0 comments on commit fd8728b

Please sign in to comment.