xzripper / flet_navigator

🌟 Navigator (Router) for Flet. 🐍
https://github.com/xzripper/flet_navigator
MIT License
36 stars 1 forks source link

AppBar #4

Closed Plumpplump closed 12 months ago

Plumpplump commented 12 months ago

Minor issue. If I set a ft.AppBar in homepage, when I navigate to the 2nd page, in which I don’t need the AppBar to be displayed anymore , the AppBar is still there. I need to set pg.page.appbar = None in the 2nd page and it works.

xzripper commented 12 months ago

Thanks for reporting! I'll release patch that fixes this issue in the near future!

Plumpplump commented 12 months ago

But in some cases, appbar again needs to be displayed in other pages anyway. So it would be better to have an option? That might make things a bit complicated.

xzripper commented 12 months ago

Its not hard to implement, I'll add opportunity to display AppBar only is selected pages.

Plumpplump commented 12 months ago

Also, it would be helpful if you could have examples to show how to : (1) use functions in actual actions like: Define_ page render set_route_data get_route_data set_homepage (2) what if Homepage, second_page, third_page … are in separate py files.

Thanks again. I find this package is very intuitive and easy to use to beginners.

xzripper commented 12 months ago

Thanks a lot, much appreciated! In the v2.3.5 release the problem with the AppBar is fixed by hooking each AppBar to the page ID. (pip install flet_navigator --upgrade)

def main_page(pg: PageData) -> None:
    pg.page.set_appbar(my_appbar) # Appbar is going to be displayed.

def second_page(pg: PageData) -> None:
    ... # Appbar isn't going to be displayed.

def third_appbar(pg: PageData) -> None:
    pg.page.set_appbar(my_appbar) # If you don't wanna copy & paste AppBar each time, you can write AppBar template and use it everywhere. # Appbar is going to be displayed.

define_page

Documentation: https://github.com/xzripper/flet_navigator/blob/main/flet-navigator-docs.md#define_page

define_page used to import page from other .py file.

flet_navigator = FletNavigator(page,
    {
        '/': main_page,
        'second_page': define_page('second_page') # Auto imports `second_page` function from `second_page.py`.
    })

second_page.py

# from flet import 

from flet_navigator import PageData

def second_page(pg: PageData) -> None:
    ... # Second page content.

render

render function shouldn't be called by user.

set_route_data

Save server data on route. Its similar to Flet session (but data saved for this route is global, for all users).

users_visited_main_page = pg.navigator.get_route_data('/')

if users_visited_main_page == None:
    users_visited_main_page = 1

else:
    users_visited_main_page += 1

pg.navigator.set_route_data('/', users_visited_main_page)

get_route_data

Get server data on route. Returns data saved on this route.

pg.page.add(Text(f'Users visited this page: {pg.navigator.get_route_data("/")}.'))

set_homepage

You can set homepage with this function (pg.navigator.set_homepage('/')), and later instead of using pg.navigator.navigate(home_page, pg.page), you can use pg.navigator.navigate_homepage(pg.page)

I also updated documentation: https://github.com/xzripper/flet_navigator/blob/main/flet-navigator-docs.md