Skip to content
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

CupertinoTimerPicker and CupertinorPicker Controls #2743

Merged
merged 9 commits into from
Mar 2, 2024
Merged

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Feb 25, 2024

TODO:

  • CupertinorPicker: Maintain state (this is done atm in a hacky way - see handle_picker_change in code below)

Test code:

import time

import flet as ft


def main(page):
    page.window_always_on_top = True
    page.theme_mode = ft.ThemeMode.LIGHT
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    selected_fruit_ref = ft.Ref[ft.Text]()
    timer_picker_value_ref = ft.Ref[ft.Text]()

    fruits = [
        "Apple",
        "Mango",
        "Banana",
        "Orange",
        "Pineapple",
        "Strawberry",
    ]

    def handle_picker_change(e):
        selected_fruit_ref.current.value = fruits[int(e.data)]
        page.update()

    def handle_timer_picker_change(e):
        val = int(e.data)
        timer_picker_value_ref.current.value = time.strftime(
            "%H:%M:%S", time.gmtime(val / 1000)
        )
        page.update()

    def open_bottom_sheet(control):
        page.show_bottom_sheet(control)

    picker = ft.CupertinoPicker(
        selected_index=3,
        # item_extent=40,
        magnification=1.22,
        # diameter_ratio=2,
        squeeze=1.2,
        use_magnifier=True,
        # looping=False,
        on_change=handle_picker_change,
        controls=[ft.Text(f) for f in fruits],
        on_dismiss=lambda _: print("Fruit picker dismissed"),
    )

    timer_picker = ft.CupertinoTimerPicker(
        value=70000,
        second_interval=10,
        minute_interval=1,
        mode=ft.CupertinoTimerPickerMode.HOUR_MINUTE_SECONDS,
        on_change=handle_timer_picker_change,
        on_dismiss=lambda _: print("Time picker dismissed"),
    )

    page.add(
        ft.Row(
            tight=True,
            controls=[
                ft.Text("Selected Fruit:", size=23),
                ft.TextButton(
                    content=ft.Text(fruits[3], size=23, ref=selected_fruit_ref),
                    style=ft.ButtonStyle(color=ft.colors.BLUE),
                    on_click=lambda _: open_bottom_sheet(picker),
                ),
            ],
        ),
        ft.Row(
            tight=True,
            controls=[
                ft.Text("TimerPicker Value:", size=23),
                ft.TextButton(
                    content=ft.Text("00:01:10", size=23, ref=timer_picker_value_ref),
                    style=ft.ButtonStyle(color=ft.colors.RED),
                    on_click=lambda _: open_bottom_sheet(timer_picker),
                ),
            ],
        ),
    )


ft.app(target=main)

@ndonkoHenri ndonkoHenri added enhancement Improvement/Optimization controls labels Feb 25, 2024
@ndonkoHenri ndonkoHenri self-assigned this Feb 25, 2024
@ndonkoHenri ndonkoHenri mentioned this pull request Feb 27, 2024
6 tasks
@ndonkoHenri ndonkoHenri changed the title CupertinoTimerPicker Control CupertinoTimerPicker and CupertinorPicker Controls Feb 29, 2024
@FeodorFitsner FeodorFitsner merged commit e172698 into main Mar 2, 2024
2 checks passed
@ndonkoHenri ndonkoHenri deleted the timer-picker branch March 2, 2024 22:42
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* initial commit

* switch to ConstrainedControl

* remove children in CupertinoTimerPickerControl

* CupertinoTimerPicker: update types and defaults

* CupertinoPicker: initial commit

* Fix python side

* Fix pickers

* Make time picker work in seconds

---------

Co-authored-by: Feodor Fitsner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
controls enhancement Improvement/Optimization
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants