|
| 1 | +import flet as ft |
| 2 | + |
| 3 | + |
| 4 | +def main(page: ft.Page) -> None: |
| 5 | + page.title = "Device orientation lock" |
| 6 | + page.appbar = ft.AppBar( |
| 7 | + title=ft.Text("Device orientation Playground"), |
| 8 | + center_title=True, |
| 9 | + bgcolor=ft.Colors.BLUE, |
| 10 | + ) |
| 11 | + |
| 12 | + def handle_media_change(e: ft.PageMediaData) -> None: |
| 13 | + page.show_dialog( |
| 14 | + ft.SnackBar( |
| 15 | + f"I see you rotated the device to {e.orientation.name} orientation. 👀", |
| 16 | + action="Haha!", |
| 17 | + duration=ft.Duration(seconds=3), |
| 18 | + ) |
| 19 | + ) |
| 20 | + |
| 21 | + page.on_media_change = handle_media_change |
| 22 | + |
| 23 | + async def on_checkbox_change(e: ft.Event[ft.Checkbox]) -> None: |
| 24 | + # get selection |
| 25 | + selected = [o for o, checkbox in checkboxes.items() if checkbox.value] |
| 26 | + # apply selection |
| 27 | + await page.set_allowed_device_orientations(selected) |
| 28 | + |
| 29 | + checkboxes: dict[ft.DeviceOrientation, ft.Checkbox] = { |
| 30 | + orientation: ft.Checkbox( |
| 31 | + label=orientation.name, |
| 32 | + value=True, |
| 33 | + on_change=on_checkbox_change, |
| 34 | + disabled=not page.platform.is_mobile(), # disabled on non-mobile platforms |
| 35 | + ) |
| 36 | + for orientation in list(ft.DeviceOrientation) |
| 37 | + } |
| 38 | + |
| 39 | + page.add( |
| 40 | + ft.Text( |
| 41 | + spans=[ |
| 42 | + # shown only on mobile platforms |
| 43 | + ft.TextSpan( |
| 44 | + "Select the orientations that should remain enabled for the app. " |
| 45 | + "If no orientation is selected, the system defaults will be used.", |
| 46 | + visible=page.platform.is_mobile(), |
| 47 | + ), |
| 48 | + # shown only on non-mobile platforms |
| 49 | + ft.TextSpan( |
| 50 | + "Please open this example on a mobile device instead.", |
| 51 | + visible=not page.platform.is_mobile(), |
| 52 | + style=ft.TextStyle(weight=ft.FontWeight.BOLD), |
| 53 | + ), |
| 54 | + ], |
| 55 | + ), |
| 56 | + ft.Column(controls=list(checkboxes.values())), |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + ft.run(main) |
0 commit comments