I want the button popupbutton's color change when I click on the popupmenuitem. My button didn't change the color when I click, but after I debug, the dStatus properties already change the color. Here is my code.
self.dStatus = Icon(name=icons.CIRCLE_ROUNDED, color="White")
def onButtonClick(e):
if e.control.data == "Online":
self.dStatus = Icon(name=icons.CIRCLE_ROUNDED, color="Green" )
elif e.control.data == "Busy":
self.dStatus = Icon(name=icons.CIRCLE_ROUNDED, color="Red")
avatar_container.content = self.dStatus
avatar_container = Container(
width=40,
height=40,
margin=margin.only(top=35, left=300),
content = PopupMenuButton(
content= self.dStatus,
items=[
PopupMenuItem(text="Status"),
PopupMenuItem(content=ElevatedButton(text="Online",icon=icons.CIRCLE_ROUNDED, icon_color="Green",data="Online", on_click= onButtonClick), ),
PopupMenuItem(content=ElevatedButton(text="Busy",icon=icons.CIRCLE_ROUNDED, icon_color="Red",data="BUsy", on_click= onButtonClick ), )
]
)
)
The main thing you're missing is a
self.update()
at the end of your callback (oravatar_container.update()
if you don't want to update the whole UserControl, or even `avatar_container.content.content.update() to update only the icon). Without it, the data is update but the component is not refreshed so you don't see the change.There were three other problems in your code:
avatar_container.content
toself.dStatus
, butavatar_container.content
is the whole container, not just the icon. Therefore, once you've used the menu once, it is replaced by an icon and the menu has disapeared. You need to update only the icon for the menu to stay here. The icon is in thecontent
of the PopupMenuButton, which is in thecontent
of the avatar_container. Therefore, the update looks like that:avatar_container.container.container = self.dStatus
.onButtonClick
callback, and clicking on the button will execute the callback but not close the menu. Therefore I changed buttons to simple rows containing the icon and the text.Here is the updated code:
Note: When asking a question, create a minimal reproducible example, the current one you posted does not work out of the box as we don't know the class of the structure of the code. It will be easier for people to help if they can start to search a solution straight away instead of having to figure out how to run the example first.