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

textfield for log view #4249

Open
1 task done
yincheng8 opened this issue Oct 28, 2024 · 6 comments
Open
1 task done

textfield for log view #4249

yincheng8 opened this issue Oct 28, 2024 · 6 comments
Assignees
Labels
status: awaiting response Further information is requested

Comments

@yincheng8
Copy link

yincheng8 commented Oct 28, 2024

Duplicate Check

Describe the requested feature

I want to use textfield to create a log window for recording logs, but after recording a lot of logs, I cannot control the scrollbar of textfield. I want to know how to control it. I think there should be methods such as scoll_to-end

import time

import flet as ft
from datetime import datetime


def get_now_time():
    current_time = datetime.now()
    formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
    return formatted_time


def main(page: ft.Page):
    page.window.center()
    page.window.width = 480
    page.window.height = 320
    page.window.resizable = False

    def btn_click(e):
        print(e)
        for i in range(20):
            log_field.value += f"{get_now_time()}......{i + 1}\n"
            time.sleep(0.3)
            log_field.focus()
            page.update()

    log_field = ft.TextField(label='write log example', min_lines=8, max_lines=8, read_only=False,
                             value='-------------------Log-------------------\n')
    page.add(log_field)
    button = ft.ElevatedButton(text='start', color='#499c54', on_click=btn_click)
    row = ft.Row(
        controls=[
            ft.Text(),
            button],
        alignment=ft.MainAxisAlignment.END
    )
    page.add(row)
    page.update()


if __name__ == '__main__':
    ft.app(target=main)

Suggest a solution

maybe add a func scoll_to_end

Screenshots

No response

Additional details

No response

@yincheng8 yincheng8 reopened this Oct 28, 2024
@OwenMcDonnell
Copy link
Contributor

What do you mean you "cannot control the scrollbar". I ran your code and was not able to detect any problem with scrolling.
If you want programmatic control of scrolling you might try putting the log items as ft.Text elements into a column and then use the scroll_to method there. Following is an example modification of your code to do that.

import time

import flet as ft
from datetime import datetime


def get_now_time():
    current_time = datetime.now()
    formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
    return formatted_time


def main(page: ft.Page):
    page.window.center()
    page.window.width = 480
    page.window.height = 360
    page.window.resizable = False

    def btn_click(e):
        print(e)
        for i in range(200):
            column_field.content.controls.append(ft.Text(f"{get_now_time()}......{i + 1}"))
            time.sleep(0.1)
            page.update()
    
    def jump_click(e):
        print(e)
        for i in range(200):
            column_field.content.scroll_to(delta=50)

    column_label = ft.Row(
        controls=[ft.Text("write log example")]

    )
    
    column_field = ft.Container(
            border=ft.border.all(5, ft.colors.GREY),
            border_radius=10,
            content=ft.Column(
                [ft.Text('--------------------------Log-------------------------')],
                scroll=ft.ScrollMode.ALWAYS,
                expand=True,
                spacing=10,
                height=200,
                width=float("inf"),
            )
        )

    page.add(column_label, column_field)
    start_button = ft.ElevatedButton(text='start', color='#499c54', on_click=btn_click)
    to_end_button = ft.ElevatedButton(text='jump scroll', color='#499c54', on_click=jump_click)
    row = ft.Row(
        controls=[
            ft.Text(),
            to_end_button,
            start_button],
        alignment=ft.MainAxisAlignment.END
    )
    page.add(row)
    page.update()


if __name__ == '__main__':
    ft.app(target=main)

@OwenMcDonnell OwenMcDonnell added the status: awaiting response Further information is requested label Oct 29, 2024
@OwenMcDonnell OwenMcDonnell self-assigned this Oct 29, 2024
@yincheng-a
Copy link

Thank you for your reply,I use textfield to record logs, and during the process of recording logs, I want the scrollbar of textfield to always be at the bottom, that is, after clicking the button and waiting for all logs to be added, as the logs increase, my scrollbar should also move together instead of being in its original position。
The current effect:now
The effect I want:
i_want

@ndonkoHenri
Copy link
Collaborator

ndonkoHenri commented Oct 30, 2024

Set Column.auto_scroll to True.

@yincheng8
Copy link
Author

aha, How can I modify my code based on it?I think textField should also need a similar attribute,such as auto_scroll or scroll_to_end

@ndonkoHenri
Copy link
Collaborator

ndonkoHenri commented Oct 31, 2024

Seems like this issue doesn't have a proper fix in flutter yet. I found some SO threads, but nothing concrete honestly.

Let us know if you find a solution which could be implemented.

(You could alternatively make use of Columns combined with Text as suggested by Owen above.)

@yincheng-a
Copy link

Look forward to your good news

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting response Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants