Skip to content

ElevatedButton, FilledButton and FilledTonalButton issue on disable #1754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrerotband opened this issue Aug 21, 2023 · 6 comments
Closed
Labels
bug Something isn't working controls

Comments

@andrerotband
Copy link

Description

The code works if I leave the default button:

import flet as ft

def main(page: ft.Page):
    def change_bt2(e):
        bt2.disabled = not bt2.disabled
        bt2.text = "Disabled" if bt2.disabled else "Enabled"
        page.update()

    bt1 = ft.ElevatedButton(text="Disable / Enable", on_click=change_bt2, width=200)
    bt2 = ft.ElevatedButton(
        height=40,
        width=150,
        text="Enabled",
    )
    page.add(bt1, bt2)

if __name__ == "__main__":
    ft.app(target=main)

image

But if I change the color or background color of the button it has problems.
It does not change the color of the button to appear disabled

import flet as ft

def main(page: ft.Page):
    def change_bt2(e):
        bt2.disabled = not bt2.disabled
        bt2.text = "Disabled" if bt2.disabled else "Enabled"
        page.update()

    bt1 = ft.ElevatedButton(text="Disable / Enable", on_click=change_bt2, width=200)
    bt2 = ft.ElevatedButton(
        bgcolor=ft.colors.INDIGO,
        height=40,
        width=150,
        text="Enabled",
    )
    page.add(bt1, bt2)

if __name__ == "__main__":
    ft.app(target=main)

image

Flet version (pip show flet):

Name: flet
Version: 0.9.0
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: [email protected]
License: Apache-2.0
Location: c:\src\flet\copahospeda\.venv\Lib\site-packages
Requires: copier, flet-runtime, packaging, pydantic, qrcode, watchdog, websocket-client, websockets
Required-by:

Operating system:

Windows 10

@andrerotband
Copy link
Author

I checked the ElevatedButton code and fixed the issue:

flet_core: elevated_button.py

   def _before_build_command(self):
        super()._before_build_command()
        if self.__color is not None or self.__bgcolor is not None or self.__elevation is not None:
            if self.__style is None:
                self.__style = ButtonStyle()
            if self.__style.color != self.__color and not self.disabled:
                self.__style.color = self.__color
            if self.__style.bgcolor != self.__bgcolor and not self.disabled:
                self.__style.bgcolor = self.__bgcolor
            if self.__style.elevation != self.__elevation:
                self.__style.elevation = self.__elevation
        if self.__style is not None:
            self.__style.side = self._wrap_attr_dict(self.__style.side)
            self.__style.shape = self._wrap_attr_dict(self.__style.shape)
        self._set_attr_json("style", self.__style)

I changed to:

    def _before_build_command(self):
        super()._before_build_command()
        if self.__color is not None or self.__bgcolor is not None or self.__elevation is not None:
            if self.__style is None:
                self.__style = ButtonStyle()
            self.__style.color = self.__color if not self.disabled else None
            self.__style.bgcolor = self.__bgcolor if not self.disabled else None
            if self.__style.elevation != self.__elevation:
                self.__style.elevation = self.__elevation
        if self.__style is not None:
            self.__style.side = self._wrap_attr_dict(self.__style.side)
            self.__style.shape = self._wrap_attr_dict(self.__style.shape)
        self._set_attr_json("style", self.__style)

And it's working properly!

I hope I have contributed!

@ndonkoHenri
Copy link
Contributor

Related issue: #1503 ?

@andrerotband
Copy link
Author

The problem is different. In #1503 the person is disabling the column and wants the buttons inside the column to be disabled as well.

@andrerotband
Copy link
Author

Hi!
What do you think about of my fix for this bug?
I would like to contribute to the Flet project.

@FeodorFitsner FeodorFitsner added bug Something isn't working controls labels Sep 5, 2023
@andrerotband
Copy link
Author

Dear @FeodorFitsner :

Did you see my fix here?

FeodorFitsner added a commit that referenced this issue Sep 9, 2023
@FeodorFitsner
Copy link
Contributor

@andrerotband Yes, thank you. I've modified it a little bit though: 684bf1f

zrr1999 pushed a commit to zrr1999/flet that referenced this issue Jul 17, 2024
* Fix incomplete code blocks in flet_core/page.py

Fix flet-dev#1734

* Fix disabled color of FilledButtons

Fix flet-dev#1754

* Add `AppView` and `WebRenderer` enums to flet-pyodide

* Bump pyodide to 0.24.0

* Bump Flet version to 0.10.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working controls
Projects
None yet
Development

No branches or pull requests

3 participants