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

Added ImageViewer Control feature #960

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create image_viewer.py
Skquark authored Jan 31, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6050eed446c123b88e1b94a0071c450ab1da69fd
179 changes: 179 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/image_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
from typing import Any, List, Dict, Optional, Union

from flet_core.control import Control
from flet_core.ref import Ref
from flet_core.types import (
MaterialState
)

class ImageViewer(Control):
"""
A ImageViewer displays a full screen Image that allows Zoom, Pan, and paging through multipe images.
The image src can be a single image path String or a List of image strings to page through.
Example:
```
import flet as ft
def main(page):
def show_image_viewer_click(e):
img = e.control.data
page.dialog = ft.ImageViewer(src=img, swipe_dismissable=False)
page.dialog.open = True
page.update()
images = ft.GridView(expand=1, runs_count=5)
for i in range(0, 60):
images.controls.append(
ft.GestureDetector(
content=ft.Image(
src=f"https://picsum.photos/150/150?{i}",
fit=ft.ImageFit.NONE,
),
data=f"https://picsum.photos/150/150?{i}",
on_click=show_image_viewer_click,
)
)
page.add(images)
ft.app(target=main)
```
-----
Online docs: https://flet.dev/docs/controls/ImageViewer
"""

def __init__(
self,
ref: Optional[Ref] = None,
disabled: Optional[bool] = None,
visible: Optional[bool] = None,
data: Any = None,
#
# Specific
#
open: bool = False,
src: Union[str, List[str]] = None,
swipe_dismissible: Optional[bool] = True,
double_tap_zoomable: Optional[bool] = True,
background_color: Optional[str] = None,
close_button_color: Optional[str] = None,
close_button_tooltip: Optional[str] = "Close",
immersive: Optional[bool] = True,
initial_index: Optional[int] = 0,
):
Control.__init__(
self,
ref=ref,
disabled=disabled,
visible=visible,
data=data,
)

self.open = open
self.src = src
self.swipe_dismissible = swipe_dismissible
self.double_tap_zoomable = double_tap_zoomable
self.background_color = background_color
self.close_button_color = close_button_color
self.close_button_tooltip = close_button_tooltip
self.immersive = immersive
self.initial_index = initial_index

def _get_control_name(self):
return "imageviewer"

def _before_build_command(self):
super()._before_build_command()

# open
@property
def open(self) -> Optional[bool]:
return self._get_attr("open", data_type="bool", def_value=False)

@open.setter
def open(self, value: Optional[bool]):
self._set_attr("open", value)

# src
@property
def src(self) -> Union[str, List[str]]:
img = self._get_attr("src")
if "|" in img:
img = img.split("|")
return img

@src.setter
def src(self, value: Union[str, List[str]]):
self.src = value
img = value
if isinstance(value, List):
img = "|".join(value)
self._set_attr("src", img)

# swipe_dismissible
@property
def swipe_dismissible(self) -> Optional[bool]:
return self._get_attr("swipeDismissible", data_type="bool", def_value=False)

@swipe_dismissible.setter
def swipe_dismissible(self, value: Optional[bool]):
self._set_attr("swipeDismissible", value)

# double_tap_zoomable
@property
def double_tap_zoomable(self) -> Optional[bool]:
return self._get_attr("doubleTapZoomable", data_type="bool", def_value=True)

@double_tap_zoomable.setter
def double_tap_zoomable(self, value: Optional[bool]):
self._set_attr("doubleTapZoomable", value)

# close_button_color
@property
def background_color(self):
return self._get_attr("backgroundColor")

@background_color.setter
def background_color(self, value):
self._set_attr("backgroundColor", value)

# close_button_color
@property
def close_button_color(self):
return self._get_attr("closeButtonColor")

@close_button_color.setter
def close_button_color(self, value):
self._set_attr("closeButtonColor", value)

# close_button_tooltip
@property
def close_button_tooltip(self):
return self._get_attr("closeButtonTooltip", def_value="Close")

@close_button_tooltip.setter
def close_button_tooltip(self, value):
self._set_attr("closeButtonTooltip", value)

# immersive
@property
def immersive(self) -> Optional[bool]:
return self._get_attr("immersive", data_type="bool", def_value=True)

@immersive.setter
def immersive(self, value: Optional[bool]):
self._set_attr("immersive", value)

# initial_index
@property
def initial_index(self) -> Optional[int]:
return self._get_attr("initialIndex", def_value=0)

@initial_index.setter
def divisions(self, value: Optional[int]):
self._set_attr("initialIndex", value)