flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.62k stars 455 forks source link

Display informative message when date is in a wrong format #4017

Closed Klu1d closed 3 weeks ago

Klu1d commented 2 months ago

Duplicate Check

Describe the bug

I get a gray screen when I press the button

Code sample

Code ```python import copy import flet as ft from datetime import datetime, timedelta style_button = { 'height': 40, 'icon': ft.icons.CALENDAR_MONTH, 'bgcolor': 'secondarycontainer, 0.6', 'style': ft.ButtonStyle(shape=ft.RoundedRectangleBorder(radius=10)), } class Event: pass class TimeRange: def __init__(self, start_date, end_date): #self.start_date = datetime.strptime(events[0]['date_start'].replace(' ', '_')start_date, '%Y-%m-%d_%H:%M:%S') self.start_date = datetime.strptime(start_date, '%Y-%m-%d_%H:%M:%S') self.end_date = datetime.strptime(end_date, '%Y-%m-%d_%H:%M:%S') self.current_start_date = copy.copy(start_date) self.current_end_date = copy.copy(end_date) self.start_date_picker = ft.DatePicker( current_date=self.start_date, first_date=copy.copy(start_date), last_date=self.end_date, on_change=self.__on_change_start_date, ) self.end_date_picker = ft.DatePicker( current_date=copy.copy(end_date), first_date=self.start_date, last_date=self.end_date, on_change=self.__on_change_end_date, ) def is_within_range(self, events: list[Event]) -> list: for event in events: event_date = datetime.strptime(event.date_start, '%Y-%m-%d_%H:%M:%S') if self.start_date <= event_date <= self.end_date: event.visible = True event.data = 'visible' else: event.visible = False event.data = None return events def get_start_date(self) -> str: return self.start_date.strftime('%d %B %Y') def get_end_date(self) -> str: return self.end_date.strftime('%d %B %Y') def __on_change_start_date(self, e: ft.ControlEvent): self.start_date = e.control.value self.start_date_picker.current_date = self.start_date self.start_date_picker.first_date = self.start_date self.end_date_picker.first_date = self.start_date self.update() def __on_change_end_date(self, e: ft.ControlEvent): self.end_date = e.control.value self.end_date_picker.current_date = self.end_date self.end_date_picker.last_date = self.end_date self.start_date_picker.last_date = self.end_date self.update() def main(page: ft.Page): def click(e): page.open(time_range.start_date_picker) page.update() time_range = TimeRange('2024-03-04_12:23:23'.replace(' ', '_'), '2024-05-04_12:23:23'.replace(' ', '_'),) el_b = ft.ElevatedButton(time_range.get_start_date(), on_click=click) page.add(el_b) ft.app(main) ```

To reproduce

Click button

Expected behavior

Open TimeRange attribute self.start_date_picker

Screenshots / Videos

https://github.com/user-attachments/assets/f8795bf6-4b6e-49ab-8e1c-863d0dce1f9d

Operating System

Linux

Operating system details

Debian 12

Flet version

0.24.1

Regression

I'm not sure / I don't know

Suggestions

No response

Logs

Logs ```console [Paste your logs here] ```

Additional details

No response

ndonkoHenri commented 2 months ago

The date you are using is in a wrong format. Accepted formats: https://api.flutter.dev/flutter/dart-core/DateTime/parse.html

Will make some improvements to show a more informative error message.

Klu1d commented 2 months ago

Accepted formats: https://api.flutter.dev/flutter/dart-core/DateTime/parse.html


class TimeRange:
def __init__(self, start_date, end_date):
#self.start_date = datetime.strptime(events[0]['date_start'].replace(' ', '_')start_date, '%Y-%m-%d_%H:%M:%S')
self.start_date = datetime.strptime(start_date, '%Y-%m-%d_%H:%M:%S')
self.end_date = datetime.strptime(end_date, '%Y-%m-%d_%H:%M:%S')
    print(self.start_date)

![image](https://github.com/user-attachments/assets/0beb16f7-7d8d-4c36-84e7-896dd820667d)

My format is correct. Here is the output of the variable self.start_date after converting it to the datetime type.
I added print and output one of the approved datetime formats.

Point out the mistake to me, otherwise I don't understand.
Klu1d commented 2 months ago

However, if you put the following instruction in the body of the click function, it will be displayed correctly:

def main(page: ft.Page):
    def click(e):
        page.open(ft.DatePicker(
            current_date=time_range.start_date,
            first_date=copy.copy(time_range.start_date),
            last_date=time_range.end_date,

        ))
        page.update()

but If you use what is specified initially, it won't work.