Add View.shader and move decoration Container behind the main content#6123
Add View.shader and move decoration Container behind the main content#6123bl1nch wants to merge 3 commits intoflet-dev:mainfrom
Conversation
|
Can you please share code example to best see the effect of your addition/usecase? |
# Base app
import flet as ft
async def before_main(page: ft.Page):
container = ft.Container(
width=500,
height=300,
bgcolor=ft.Colors.SURFACE_CONTAINER,
alignment=ft.Alignment.CENTER,
border_radius=15,
content=ft.Text(
value="Hello World!",
size=26,
weight=ft.FontWeight.W_600,
),
)
view = ft.View(
route="/",
bgcolor=ft.Colors.TRANSPARENT,
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
controls=[container],
)
view.decoration = ft.BoxDecoration(
image=ft.DecorationImage(
src="https://fedoraproject.org/w/uploads/d/de/F34_default_wallpaper_night.jpg",
fit=ft.BoxFit.COVER,
),
)
page.views.clear()
page.views.append(view)
async def main(page: ft.Page):
pass
ft.run(main, before_main=before_main)
# Image is rendering on top of gradient
import flet as ft
async def before_main(page: ft.Page):
container = ft.Container(
width=500,
height=300,
bgcolor=ft.Colors.SURFACE_CONTAINER,
alignment=ft.Alignment.CENTER,
border_radius=15,
content=ft.Text(
value="Hello World!",
size=26,
weight=ft.FontWeight.W_600,
),
)
view = ft.View(
route="/",
bgcolor=ft.Colors.TRANSPARENT,
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
controls=[container],
)
view.decoration = ft.BoxDecoration(
image=ft.DecorationImage(
src="https://fedoraproject.org/w/uploads/d/de/F34_default_wallpaper_night.jpg",
fit=ft.BoxFit.COVER,
),
gradient=ft.RadialGradient(
colors=[
ft.Colors.TRANSPARENT,
ft.Colors.with_opacity(0.9, ft.Colors.RED)
],
stops=[0.1, 1.0],
radius=1,
),
)
page.views.clear()
page.views.append(view)
async def main(page: ft.Page):
pass
ft.run(main, before_main=before_main)
# Gradient is rendering on top of main content (in current flet version)
# Main content is rendering on top of gradient (with PR)
import flet as ft
async def before_main(page: ft.Page):
page.theme_mode = ft.ThemeMode.LIGHT
container = ft.Container(
width=500,
height=300,
bgcolor=ft.Colors.SURFACE_CONTAINER,
alignment=ft.Alignment.CENTER,
border_radius=15,
content=ft.Text(
value="Hello World!",
size=26,
weight=ft.FontWeight.W_600,
),
)
view = ft.View(
route="/",
bgcolor=ft.Colors.TRANSPARENT,
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
controls=[container],
)
view.decoration = ft.BoxDecoration(
image=ft.DecorationImage(
src="https://fedoraproject.org/w/uploads/d/de/F34_default_wallpaper_night.jpg",
fit=ft.BoxFit.COVER,
),
)
view.foreground_decoration = ft.BoxDecoration(
gradient=ft.RadialGradient(
colors=[
ft.Colors.TRANSPARENT,
ft.Colors.with_opacity(0.9, ft.Colors.RED)
],
stops=[0.1, 1.0],
radius=1,
),
)
page.views.clear()
page.views.append(view)
async def main(page: ft.Page):
pass
ft.run(main, before_main=before_main)
# Gradient is rendering on top of main content (with PR)
# (Use View.shader for previous behavior instead of View.foreground_decoration)
import flet as ft
async def before_main(page: ft.Page):
page.theme_mode = ft.ThemeMode.LIGHT
container = ft.Container(
width=500,
height=300,
bgcolor=ft.Colors.SURFACE_CONTAINER,
alignment=ft.Alignment.CENTER,
border_radius=15,
content=ft.Text(
value="Hello World!",
size=26,
weight=ft.FontWeight.W_600,
),
)
view = ft.View(
route="/",
bgcolor=ft.Colors.TRANSPARENT,
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
controls=[container],
)
view.decoration = ft.BoxDecoration(
image=ft.DecorationImage(
src="https://fedoraproject.org/w/uploads/d/de/F34_default_wallpaper_night.jpg",
fit=ft.BoxFit.COVER,
),
)
view.shader = ft.RadialGradient(
colors=[
ft.Colors.TRANSPARENT,
ft.Colors.with_opacity(0.9, ft.Colors.RED)
],
stops=[0.1, 1.0],
radius=1,
)
view.blend_mode = ft.BlendMode.SRC_OVER
page.views.clear()
page.views.append(view)
async def main(page: ft.Page):
pass
ft.run(main, before_main=before_main) |
|
I’m seeing a blocking runtime issue on my side:
I think this comes from the new
That effectively mounts the same widget tree twice, which can duplicate One more concern: |
|
Sorry, didn’t notice that. Fixed and updated the example. |
|
Thanks for the fixes. I hope its not too much asked, but for a better understanding for everyone, can you please share the visual result/output before and after this PR? Cause to be honest, I don't still seem to fully understand. For example, with your PR, both the Visual output can be a little more convincing to better understand your usecase and purpose of this PR. |
|
Yes, of course, no problem. This is how After the PR, both If we still want to render a |
|
Ah, I kind of see what you are trying to achieve. flowchart TB
subgraph Before["Before"]
direction TB
X1["foreground_decoration"]
X2["app content"]
X3["background_decoration"]
X1 --> X2 --> X3
end
subgraph After["After"]
direction TB
Y1["shader (gradient) + blend_mode"]
Y2["app content"]
Y3["foreground_decoration"]
Y4["background_decoration"]
Y1 --> Y2 --> Y3 --> Y4
end
This is kind of breaking though, as only one property (or 2 with Let me have your thoughts. |
|
Yes, I understand. In fact, the only real use cases we can lose are either some kind of color Of course, if we want to preserve full backward compatibility, we could try to rework the PR. However, in that case we would need to think about how to rename the props properly (background_decoration, foreground_decoration, and possibly introduce something third). What do you think about this? |
|
OK, why not just add |



Description
The
Viewrendering order has been updated so thedecoration containeris moved to the background and is now rendered fully behind the main content, includingforeground_decoration. This fixes issues where decorations could overlap or interfere with content rendering. In addition, a newshaderproperty (with configurableblend_mode) is introduced to preserve the previous behavior whereforeground_decorationwas visually applied on top of all other content.Fixes #6094
Test Code
# Test code for the review of this PRType of change
Checklist
Summary by Sourcery
Update view rendering to place background decorations behind main content and introduce configurable shader-based foreground effects.
New Features:
Enhancements: