flet-dev / examples

Flet sample applications
MIT License
420 stars 171 forks source link

Chip control has no way of controlling text color when selected #103

Closed Petapton closed 1 week ago

Petapton commented 7 months ago

Hello, I found out that Chip cannot use different label colors for selected and unselected states. Instead, the check mark color can be changed.

Here is an example:

# ...
[
    ft.Chip(
        label=ft.Text("Chip"),
        selected_color=ft.colors.PRIMARY,
        check_color=ft.colors.ON_PRIMARY,
        on_select=lambda _: page.update(),
    )
]
# ...

Result: chip-demo

It would be nice if there were a way to set also the label to colors.PRIMARY when selected. I think an option could be a selected_label_color-ish argument. Alternatively, the check_color could be used on label when selected.

ndonkoHenri commented 7 months ago

I found out that Chip cannot use different label colors for selected and unselected states.

Make the changes in your callback function:

import flet as ft

def main(page: ft.Page):
    def chip_callback(e):
        x.label_style.color = ft.colors.YELLOW if x.selected else ft.colors.RED
        page.update()

    x = ft.Chip(
        label=ft.Text(
            "Chip",
            # color=ft.colors.AMBER
        ),
        label_style=ft.TextStyle(weight=ft.FontWeight.BOLD, italic=True, color=ft.colors.RED),
        selected_color=ft.colors.PRIMARY,
        check_color=ft.colors.ON_PRIMARY,
        on_select=chip_callback,
    )

    page.add(x)

ft.app(target=main)