Replies: 4 comments 3 replies
-
Inside |
Beta Was this translation helpful? Give feedback.
-
Inside my UserControl, self.page is always == None |
Beta Was this translation helpful? Give feedback.
-
Ok, I confirmed self.page is None in build, but available in did_mount. It means that I need to build partially in build method and complete in did_mount. It is a bit weird. |
Beta Was this translation helpful? Give feedback.
-
Hi @FeodorFitsner , import fletii as ii
import flet as ft
import random
# Services ------------------------------
# Super complex service interface
class MyService:
def get_some_data(self, arg):
pass
# Alternative A
class MyServiceA(MyService):
def get_some_data(self, arg):
return f"Hello Mr. {arg}!!!"
# Alternative B
class MyServiceB(MyService):
def get_some_data(self, arg):
return f"Bye Mr. {arg}!!!"
# Widgets (Controls) --------------------
# Directly depends on MyService
class MyGreeter(ii.CustomControl):
def __init__(self, value):
super().__init__()
self.value = value
def build(self, context: ii.BuildContext):
hw = context.find(MyService) # HERE: Access to scoped service instance
return ft.Text(hw.get_some_data(self.value))
# Transitively depends on MyService
class MyCtl(ii.CustomControl):
def build(self, context: ii.BuildContext):
return ft.Column([
MyGreeter("Paul"),
MyGreeter("Ringo"),
MyGreeter("John"),
MyGreeter("George"),
])
# Application ---------------------------
class MyApp(ii.CustomControl):
def build(self, context: ii.BuildContext):
# Configure service implementation at runtime...
context.set_service(MyServiceA() if random.random() > 0.5 else MyServiceB(), key=MyService)
# Use MyCtl (implementation agnostic)
return MyCtl()
# Bootstrap -----------------------------
def main(page: ft.Page):
page.add(MyApp())
ft.app(target=main)
What do you think about this approach? Implementing ii.CustomControl and ii.BuildContext were just 70 lines of code, flet is fantastic. My only concern is how stable are the current apis. I implemented ii.CustomControl based on flet.UserControl. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Friends,
Is there a way to access the current
page
instance from anUserControl
without the need to pass it explicitly in constructor? I Want to implement something similar to InheritedWidget/Provider/Hooks but I need access to the root to maintain a pseudo singleton registry.Beta Was this translation helpful? Give feedback.
All reactions