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

feat: Sliver controls (SliverAppBar, SliverListView, SliverGridView, SliverSafeArea, SliverScrollView) #5112

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Mar 22, 2025

Fix #3427
Fix #3428
Fix #1843

Test Code

Details

import time
import flet as ft


def main(page: ft.Page):
    page.theme_mode = ft.ThemeMode.LIGHT
    page.padding = 0

    def handle_action(e: ft.ControlEvent):
        action = e.control.label.lower()
        if action == "pinned":
            sliver_appbar.pinned = e.control.value
        elif action == "floating":
            sliver_appbar.floating = e.control.value
            sliver_appbar.snap = sliver_appbar.snap and sliver_appbar.floating
            snap.value = sliver_appbar.snap
        elif action == "snap":
            sliver_appbar.snap = e.control.value
            sliver_appbar.floating = sliver_appbar.floating or sliver_appbar.snap
            floating.value = sliver_appbar.floating
        elif action == "stretch":
            sliver_appbar.stretch = e.control.value
        page.update()

    def handle_bar_stretch(e: ft.ControlEvent):
        progress_bar.visible = True
        progress_bar.update()
        time.sleep(2)
        progress_bar.visible = False
        progress_bar.update()

    page.bottom_appbar = ft.BottomAppBar(
        content=ft.Row(
            alignment=ft.MainAxisAlignment.CENTER,
            wrap=True,
            controls=[
                ft.Switch(label="Pinned", value=True, on_change=handle_action),
                ft.Switch(label="Stretch", value=True, on_change=handle_action),
                floating := ft.Switch(
                    label="Floating", value=True, on_change=handle_action
                ),
                snap := ft.Switch(label="Snap", value=True, on_change=handle_action),
            ],
        )
    )

    page.overlay.append(progress_bar := ft.ProgressBar(visible=False, height=15))
    page.add(
        ft.SliverScrollView(
            expand=True,
            slivers=[
                sliver_appbar := ft.SliverAppBar(
                    pinned=True,
                    floating=True,
                    stretch=True,
                    snap=True,
                    expanded_height=150,
                    on_stretch=handle_bar_stretch,
                    flexible_space=ft.FlexibleSpaceBar(
                        title=ft.Text("Sliver Playground"),
                        center_title=True,
                        background=ft.Image(
                            src="https://picsum.photos/400/500", fit=ft.ImageFit.COVER
                        ),
                    ),
                ),
                ft.SliverListView(
                    spacing=10,
                    divider_thickness=1,
                    controls=[
                        ft.ListTile(
                            leading=ft.CircleAvatar(ft.Text(f"{i}")),
                            title=ft.Text(f"Item {i}"),
                            bgcolor=ft.Colors.BLUE_50
                            if i % 2 == 0
                            else ft.Colors.WHITE,
                            on_click=lambda _: None,
                        )
                        for i in range(11)
                    ],
                ),
                ft.SliverGridView(
                    spacing=10,
                    child_aspect_ratio=4,
                    run_spacing=10,
                    runs_count=3,
                    max_extent=200,
                    controls=[
                        ft.Container(
                            content=ft.Text(f"Item {i}"),
                            alignment=ft.alignment.center,
                            width=20,
                            height=20,
                            bgcolor=ft.Colors.RED_300
                            if i % 2 == 0
                            else ft.Colors.BLUE_300,
                        )
                        for i in range(20)
                    ],
                ),
            ],
        )
    )


ft.app(main)

Summary by Sourcery

Introduce new Sliver controls, including SliverAppBar, SliverListView, SliverGridView, SliverSafeArea, and SliverScrollView, to enable custom scroll effects.

New Features:

  • Add SliverAppBar control for a Material Design app bar that integrates with a SliverScrollView.
  • Add SliverListView control to place multiple box children in a linear array along the main axis.
  • Add SliverGridView control to place multiple box children in a linear array along the main axis.
  • Add SliverSafeArea control to inset another sliver by sufficient padding to avoid intrusions by the operating system.
  • Add SliverScrollView control to create custom scroll effects using slivers.
  • Add FlexibleSpaceBar control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SliverGrid Control SliverList Control SliverAppBar control
1 participant