Untitled conversation
- User
import flet as ft from flet import Icons from flet.core.colors import Colors class RemovableTextInput(ft.Card): def __init__(self, on_delete=None, on_duplicate=None, on_reorder=None, index=0, **kwargs): super().__init__(expand=True) self.on_delete = on_delete self.on_duplicate = on_duplicate self.on_reorder = on_reorder self.index = index # ドラッグハンドル self.drag_handle = ft.Draggable( group="fields", content=ft.Container( content=ft.Icon(Icons.DRAG_HANDLE, size=24), alignment=ft.alignment.center, width=40, height=40, ), content_when_dragging=ft.Container( width=40, height=40, bgcolor=Colors.BLUE_200, border_radius=5, ), on_drag_start=self._on_drag_start, on_drag_complete=self._on_drag_complete, ) self.title_field = ft.TextField(hint_text="タイトル", expand=True, **kwargs) self.content_field = ft.TextField(hint_text="内容", multiline=True, expand=True, **kwargs) duplicate_button = ft.IconButton( icon=Icons.COPY_OUTLINED, tooltip="コピー", on_click=self._on_duplicate_click, ) delete_button = ft.IconButton( icon=Icons.CLOSE, icon_size=20, tooltip="削除", on_click=self._on_delete_click, ) # ドラッグターゲット(カード全体) self.drag_target = ft.DragTarget( group="fields", content=ft.Row( controls=[ self.drag_handle, ft.Column( controls=[self.title_field, self.content_field], expand=True ), ft.Column( controls=[duplicate_button, delete_button], ) ], spacing=5, ), on_will_accept=self._on_will_accept, on_accept=self._on_accept, on_leave=self._on_leave, ) self.content = self.drag_target def _on_drag_start(self, e): if self.on_reorder: self.on_reorder("start", self) def _on_drag_complete(self, e): if self.on_reorder: self.on_reorder("complete", self) def _on_will_accept(self, e): if self.on_reorder: self.on_reorder("hover", self) e.control.update() def _on_accept(self, e): if self.on_reorder: self.on_reorder("drop", self) def _on_leave(self, e): if self.on_reorder: self.on_reorder("leave", self) def _on_delete_click(self, e): if self.on_delete: self.on_delete(self) def _on_duplicate_click(self, e): if self.on_duplicate: self.on_duplicate(self) def get_values(self): return { "title": self.title_field.value, "content": self.content_field.value } def main(page: ft.Page): page.title = "削除可能なTextField(ドラッグ&ドロップ対応)" page.padding = 20 field_container = ft.Column(spacing=10) # ドラッグ中の情報を保持 drag_state = { "dragging_field": None, "original_index": None, "current_hover_index": None } def update_indices(): """各フィールドのインデックスを更新""" for i, field in enumerate(field_container.controls): field.index = i def reorder_fields(action, target_field): """フィールドの並び替え処理""" if action == "start": # ドラッグ開始 drag_state["dragging_field"] = target_field drag_state["original_index"] = field_container.controls.index(target_field) drag_state["current_hover_index"] = drag_state["original_index"] elif action == "hover" and drag_state["dragging_field"]: # ホバー中(リアルタイムで入れ替え) dragging_field = drag_state["dragging_field"] if dragging_field != target_field: dragging_index = field_container.controls.index(dragging_field) target_index = field_container.controls.index(target_field) # フィールドを入れ替え field_container.controls.pop(dragging_index) field_container.controls.insert(target_index, dragging_field) drag_state["current_hover_index"] = target_index update_indices() page.update() elif action == "complete": # ドラッグ完了(状態をリセット) drag_state["dragging_field"] = None drag_state["original_index"] = None drag_state["current_hover_index"] = None update_indices() page.update() # 削除確認ダイアログ def confirm_delete(field): def close_dialog(e): dialog.open = False page.update() def yes_click(e): field_container.controls.remove(field) dialog.open = False update_indices() page.update() dialog = ft.AlertDialog( modal=True, title=ft.Text("削除の確認"), content=ft.Text("このフィールドを削除してもよろしいですか?"), actions=[ ft.TextButton("いいえ", on_click=close_dialog), ft.TextButton("はい", on_click=yes_click), ], actions_alignment=ft.MainAxisAlignment.END, ) page.overlay.append(dialog) dialog.open = True page.update() def duplicate_field(field): values = field.get_values() new_field = RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=field.index + 1 ) new_field.title_field.value = values["title"] new_field.content_field.value = values["content"] # 元のフィールドの次に挿入 index = field_container.controls.index(field) field_container.controls.insert(index + 1, new_field) update_indices() page.update() def add_field(e): new_field = RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=len(field_container.controls) ) field_container.controls.append(new_field) update_indices() page.update() # 初期フィールドを3つ追加 for i in range(3): field_container.controls.append( RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=i ) ) page.add( ft.Text( "ハンドルをドラッグしてフィールドを並び替え", size=20, weight=ft.FontWeight.BOLD ), field_container, ft.ElevatedButton("フィールドを追加", on_click=add_field), ) ft.app(target=main)ドラッグ中に要素全体を半透明にしてカーソルに追従させたい。現在はハンドルのアイコンだけが追従する。 どこを変更するべき?コードは部分ではなく全体を提示して。
Assistant
ドラッグ中に要素全体を半透明にしてカーソルに追従させるには、
Draggableのcontentとcontent_when_draggingを調整する必要があります。以下が修正版の完全なコードです:import flet as ft from flet import Icons from flet.core.colors import Colors class RemovableTextInput(ft.Card): def __init__(self, on_delete=None, on_duplicate=None, on_reorder=None, index=0, **kwargs): super().__init__(expand=True) self.on_delete = on_delete self.on_duplicate = on_duplicate self.on_reorder = on_reorder self.index = index self.title_field = ft.TextField(hint_text="タイトル", expand=True, **kwargs) self.content_field = ft.TextField(hint_text="内容", multiline=True, expand=True, **kwargs) duplicate_button = ft.IconButton( icon=Icons.COPY_OUTLINED, tooltip="コピー", on_click=self._on_duplicate_click, ) delete_button = ft.IconButton( icon=Icons.CLOSE, icon_size=20, tooltip="削除", on_click=self._on_delete_click, ) # カード全体の内容を作成 card_content = ft.Row( controls=[ ft.Container( content=ft.Icon(Icons.DRAG_HANDLE, size=24), alignment=ft.alignment.center, width=40, height=40, ), ft.Column( controls=[self.title_field, self.content_field], expand=True ), ft.Column( controls=[duplicate_button, delete_button], ) ], spacing=5, ) # ドラッグ中に表示する半透明のカード dragging_content = ft.Container( content=ft.Row( controls=[ ft.Container( content=ft.Icon(Icons.DRAG_HANDLE, size=24), alignment=ft.alignment.center, width=40, height=40, ), ft.Column( controls=[ ft.TextField( value=self.title_field.value if hasattr(self, 'title_field') else "", hint_text="タイトル", expand=True, read_only=True ), ft.TextField( value=self.content_field.value if hasattr(self, 'content_field') else "", hint_text="内容", multiline=True, expand=True, read_only=True ) ], expand=True ), ft.Column( controls=[ ft.IconButton(icon=Icons.COPY_OUTLINED, disabled=True), ft.IconButton(icon=Icons.CLOSE, icon_size=20, disabled=True), ], ) ], spacing=5, ), bgcolor=Colors.with_opacity(0.8, Colors.SURFACE_VARIANT), padding=10, border_radius=10, width=800, # 適切な幅を設定 ) # Draggableでカード全体をラップ self.draggable = ft.Draggable( group="fields", content=card_content, content_when_dragging=dragging_content, on_drag_start=self._on_drag_start, on_drag_complete=self._on_drag_complete, ) # DragTargetでDraggableをラップ self.drag_target = ft.DragTarget( group="fields", content=self.draggable, on_will_accept=self._on_will_accept, on_accept=self._on_accept, on_leave=self._on_leave, ) self.content = self.drag_target def _on_drag_start(self, e): # ドラッグ開始時に現在の値を取得してドラッグ中のコンテンツを更新 self._update_dragging_content() if self.on_reorder: self.on_reorder("start", self) def _update_dragging_content(self): # ドラッグ中のコンテンツに現在の値を反映 dragging_content = ft.Container( content=ft.Row( controls=[ ft.Container( content=ft.Icon(Icons.DRAG_HANDLE, size=24), alignment=ft.alignment.center, width=40, height=40, ), ft.Column( controls=[ ft.TextField( value=self.title_field.value or "", hint_text="タイトル", expand=True, read_only=True ), ft.TextField( value=self.content_field.value or "", hint_text="内容", multiline=True, expand=True, read_only=True ) ], expand=True ), ft.Column( controls=[ ft.IconButton(icon=Icons.COPY_OUTLINED, disabled=True), ft.IconButton(icon=Icons.CLOSE, icon_size=20, disabled=True), ], ) ], spacing=5, ), bgcolor=Colors.with_opacity(0.8, Colors.SURFACE_VARIANT), padding=10, border_radius=10, width=800, opacity=0.8, # 半透明に設定 ) self.draggable.content_when_dragging = dragging_content def _on_drag_complete(self, e): if self.on_reorder: self.on_reorder("complete", self) def _on_will_accept(self, e): if self.on_reorder: self.on_reorder("hover", self) e.control.update() def _on_accept(self, e): if self.on_reorder: self.on_reorder("drop", self) def _on_leave(self, e): if self.on_reorder: self.on_reorder("leave", self) def _on_delete_click(self, e): if self.on_delete: self.on_delete(self) def _on_duplicate_click(self, e): if self.on_duplicate: self.on_duplicate(self) def get_values(self): return { "title": self.title_field.value, "content": self.content_field.value } def main(page: ft.Page): page.title = "削除可能なTextField(ドラッグ&ドロップ対応)" page.padding = 20 field_container = ft.Column(spacing=10) # ドラッグ中の情報を保持 drag_state = { "dragging_field": None, "original_index": None, "current_hover_index": None } def update_indices(): """各フィールドのインデックスを更新""" for i, field in enumerate(field_container.controls): field.index = i def reorder_fields(action, target_field): """フィールドの並び替え処理""" if action == "start": # ドラッグ開始 drag_state["dragging_field"] = target_field drag_state["original_index"] = field_container.controls.index(target_field) drag_state["current_hover_index"] = drag_state["original_index"] elif action == "hover" and drag_state["dragging_field"]: # ホバー中(リアルタイムで入れ替え) dragging_field = drag_state["dragging_field"] if dragging_field != target_field: dragging_index = field_container.controls.index(dragging_field) target_index = field_container.controls.index(target_field) # フィールドを入れ替え field_container.controls.pop(dragging_index) field_container.controls.insert(target_index, dragging_field) drag_state["current_hover_index"] = target_index update_indices() page.update() elif action == "complete": # ドラッグ完了(状態をリセット) drag_state["dragging_field"] = None drag_state["original_index"] = None drag_state["current_hover_index"] = None update_indices() page.update() # 削除確認ダイアログ def confirm_delete(field): def close_dialog(e): dialog.open = False page.update() def yes_click(e): field_container.controls.remove(field) dialog.open = False update_indices() page.update() dialog = ft.AlertDialog( modal=True, title=ft.Text("削除の確認"), content=ft.Text("このフィールドを削除してもよろしいですか?"), actions=[ ft.TextButton("いいえ", on_click=close_dialog), ft.TextButton("はい", on_click=yes_click), ], actions_alignment=ft.MainAxisAlignment.END, ) page.overlay.append(dialog) dialog.open = True page.update() def duplicate_field(field): values = field.get_values() new_field = RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=field.index + 1 ) new_field.title_field.value = values["title"] new_field.content_field.value = values["content"] # 元のフィールドの次に挿入 index = field_container.controls.index(field) field_container.controls.insert(index + 1, new_field) update_indices() page.update() def add_field(e): new_field = RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=len(field_container.controls) ) field_container.controls.append(new_field) update_indices() page.update() # 初期フィールドを3つ追加 for i in range(3): field_container.controls.append( RemovableTextInput( on_delete=confirm_delete, on_duplicate=duplicate_field, on_reorder=reorder_fields, index=i ) ) page.add( ft.Text( "ハンドルをドラッグしてフィールドを並び替え", size=20, weight=ft.FontWeight.BOLD ), field_container, ft.ElevatedButton("フィールドを追加", on_click=add_field), ) ft.app(target=main)主な変更点:
-
Draggableがカード全体をラップ: ドラッグハンドルだけでなく、カード全体の内容を
Draggableのcontentに設定しました。 -
content_when_dragging: ドラッグ中に表示される半透明のカードを作成。元のカードと同じレイアウトですが、
opacityとbgcolorで半透明効果を実現しています。 -
_update_dragging_content()メソッド: ドラッグ開始時に現在のテキストフィールドの値を取得して、ドラッグ中のコンテンツに反映させます。
-
読み取り専用フィールド: ドラッグ中のテキストフィールドは
read_only=Trueに設定し、ボタンはdisabled=Trueにしています。
これにより、ドラッグ時にカード全体が半透明になってカーソルに追従するようになります。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.