-
In flutter ListView.builder every tile has an index so that the tiles have sort on an index number so that for example when clicked you could get the index number attribute of the listTile. I looked through the flet documentary for listTiles and listViews I couldn't find anything like index number attributes pertaining to either of them. Is there a way around this? I really need this because I made each listTile for a person using my app and I want to make it such that when it is clicked it shows the person's info to and do that I need the person's id which I'm hoping could be obtained from the person's listTile. Any help would be appreciated, Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
All Flet controls have a ft.ListTile(
title=ft.Text("One-line list tile"),
data=20,
on_click=lambda e: print(e.control.data)
) Let me know if it helped. |
Beta Was this translation helpful? Give feedback.
-
Try this : import flet as ft
def getIdFromListTile(e):
idValue = e.control.data
print(f"Here is the id from the listTile {idValue}")
def test(page=ft.Page):
userNameAndId = [["1", "firstname", "middleName", "lastName"],
["2", "firstname", "middleName", "lastName"],
["3", "firstname", "middleName", "lastName"],
["4", "firstname", "middleName", "lastName"],
["5", "firstname", "middleName", "lastName"],
["6", "firstname", "middleName", "lastName"],
["7", "firstname", "middleName", "lastName"],
["8", "firstname", "middleName", "lastName"], ]
for userData in userNameAndId:
idValue = userData[0]
firstName = userData[1]
middleName = userData[2]
lastName = userData[3]
page.add(
ft.ListTile(
leading=ft.Container(content=ft.Icon(ft.icons.PERSON, size=45, color=ft.colors.TEAL_ACCENT_700),
data=int(idValue),
bgcolor=ft.colors.AMBER, border_radius=100),
data=idValue,
on_click=getIdFromListTile,
title=ft.Text(
f"{idValue} - {firstName} {middleName} {lastName}"),
trailing=ft.Row(
width=100,
controls=[
ft.IconButton(
data=idValue,
on_click=getIdFromListTile,
icon=ft.icons.EDIT_OUTLINED,
tooltip="Edit student data",
),
ft.IconButton(
ft.icons.DELETE_OUTLINE,
data=idValue,
tooltip="Delete student data",
on_click=getIdFromListTile
),
],
alignment="end",
),
)
)
ft.app(target=test)``` |
Beta Was this translation helpful? Give feedback.
Try this :