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

Simple Toast messages #14

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions python/community/toasts/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import flet
from flet import Page, colors, ElevatedButton, icons
from flet_toasts import Toast

def main(page: Page):
btn = ElevatedButton("Toast")
page.add(btn)
Toast(
page,
icons.PERSON_SHARP,
"Toast title",
"Toast description",
btn,
colors.WHITE,
).struct()


if __name__ == "__main__":
flet.app(target=main)
1 change: 1 addition & 0 deletions python/community/toasts/flet_toasts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from flet_toasts.simple_toast import Toast
117 changes: 117 additions & 0 deletions python/community/toasts/flet_toasts/simple_toast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import time
import threading
import flet
from flet import *
from flet import colors, icons, alignment, border


class Toast:
"""
Note: UserControl is not used due to some issues of display

`Parameters`:
`page` -> page of main function\n
`icon` -> icon heading\n
`msg_title` -> message title\n
`trigger` -> button that triggers toast\n
`bgcolor` -> container background color\n
`auto_close` -> closes in `auto_close` seconds

TODOS:
TODO: Implement timer
TODO: Implement different types. `Success`, `Info`, `Danger`
"""

def __init__(
self,
page: Page,
icon,
msg_title,
msg_desc,
trigger,
bgcolor: str = None,
auto_close: int = 5,
):
self.page = page
self.icon = icon
self.msg_title = msg_title
self.msg_desc = msg_desc
self.trigger = trigger
assert hasattr(
self.trigger, "on_click"
), "Control must contain `on_click` attribute"
self.trigger.on_click = lambda x: threading.Thread(
target=self._update_visibility, daemon=True
).start()

self.bgcolor = bgcolor
self.auto_close = auto_close

def _update_visibility(self):
self.stack.opacity = 0 if self.stack.opacity == 1 else 1
self.page.update()
time.sleep(self.auto_close)

if self.stack.opacity == 1:
threading.Thread(target=self._auto_disapper, daemon=True).start()

def _auto_disapper(self):
self.stack.opacity = 0
self.page.update()

def _close(self):
self.stack.opacity = 0
self.page.update()

def _start_timer(self):
pass

def struct(self):
main_stack = Stack(expand=True)
main_stack.controls = [self.toast_container()]
self.page.add(main_stack)

def toast_container(self):
self.timer = 0

header = Row(
controls=[
Row([Icon(self.icon), Text(self.msg_title)]),
Row(
[
Text(f"{self.timer} seconds ago"),
IconButton(
icons.CLOSE_OUTLINED,
on_click=lambda x: threading.Thread(
target=self._close, daemon=True
).start(),
),
]
),
],
alignment="spaceBetween",
)

toast_content = Text(self.msg_desc)

self.container = Container(
content=Column([header, Divider(), toast_content]),
width=400,
bgcolor=self.bgcolor,
border_radius=10,
padding=10,
border=border.all(0.5, colors.BLACK12),
right=0,
bottom=0,
expand=True,
)

self.stack = Stack(
width=self.page.window_width,
height=self.page.window_height,
controls=[self.container],
opacity=0,
animate_opacity=500,
expand=True,
)
return self.stack
12 changes: 12 additions & 0 deletions python/community/toasts/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Simple toasts

## Components

>Toast(page: Page, icon: str, msg_title: str, msg_desc: str, trigger: Type[Control], bgcolor: str = None, auto_close: int = 5)
* page -> Instance of Page
* icon -> Message Icon
* msg_title -> Toast message title, after icon
* msg_desc -> Toast message description
* trigger -> A button to fire Toast message
* bgcolor -> Toast message background color
* auto_close -> Time till toast message is closed automatically