Untitled conversation
- Usercode
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
def create_image_card(item_path: Path, on_click: callable) -> ft.Card:
return ft.Card(
content=ft.Container(
content=ft.Stack([
ft.Image(
src=item_path.as_posix(),
fit=ft.ImageFit.CONTAIN,
border_radius=8,
expand=True,
),
ft.Container( # タイトル部分に透明背景をつける
height=28,
bgcolor=ft.Colors.with_opacity(0.6, ft.Colors.BLACK),
border_radius=8,
left=0,
bottom=0,
right=0,
),
ft.Container(
content=ft.Text(
item_path.name,
size=14,
color=ft.Colors.WHITE,
weight=ft.FontWeight.BOLD,
text_align=ft.TextAlign.CENTER,
overflow=ft.TextOverflow.ELLIPSIS,
),
alignment=ft.alignment.bottom_center, # 下中央配置
padding=4,
# 横方向最大幅制限(親から10px余白)
margin=ft.margin.symmetric(horizontal=10),
# 角丸処理
border_radius=8,
)
]),
on_click=on_click,
),
elevation=2,
shape=ft.RoundedRectangleBorder(radius=8),
)
Python 3.12, Flet 0.28.3. stack内の画像のfitをどれに変えてもContainerが満たされず余白を生じる。 ほかにいい方法がある?
原因のほとんどは
「Image が Container を埋めていない」のではなく、Material-UI の Card 自体が
標準で持っている “外側の余白(margin)” と
“角丸を描画するためのクリッピング” の影響です。
ImageFit を何に変えても余白が消えないのはそのためです。
ポイントは次の 3 つです。
以下のサンプルを比べてみてください 👇
import flet as ft
from pathlib import Path
def create_image_card(item_path: Path, on_click: callable,
w: int = 160, h: int = 160) -> ft.Card:
return ft.Card(
# ① デフォルト余白(左右4, 上下8dp)を消す
margin=0,
# ② Clip を有効にして角丸で切り抜く
clip_behavior=ft.ClipBehavior.ANTI_ALIAS,
shape=ft.RoundedRectangleBorder(radius=8),
elevation=2,
# ③ 中央の Container を“きっちり”広げる
content=ft.Container(
width=w,
height=h,
on_click=on_click,
clip_behavior=ft.ClipBehavior.HARD_EDGE, # 念のため二重に Clip
content=ft.Stack(
expand=True, # ← Stack 自体も全面に
controls=[
# ─────── 背景画像 ───────
ft.Image(
src=item_path.as_posix(),
fit=ft.ImageFit.COVER, # FILL / COVER あたりが定番
expand=True, # ← 画像を全面に
),
# ─────── 透過黒帯 ───────
ft.Container(
height=28,
bgcolor=ft.Colors.with_opacity(0.6, ft.Colors.BLACK),
left=0, bottom=0, right=0,
),
# ─────── タイトル文字 ───────
ft.Container(
content=ft.Text(
item_path.name,
size=14,
color=ft.Colors.WHITE,
weight=ft.FontWeight.BOLD,
text_align=ft.TextAlign.CENTER,
overflow=ft.TextOverflow.ELLIPSIS,
),
alignment=ft.alignment.bottom_center,
padding=4,
margin=ft.margin.symmetric(horizontal=10),
),
],
),
),
)
# サンプル用ページ
def main(page: ft.Page):
card = create_image_card(Path("sample.jpg"), lambda e: print("clicked"))
page.add(card)
# ↓ いつものおまじない
ft.app(target=main)
Not yet included in a published release.
Card の margin
Flutter(Material) の Card は EdgeInsets.symmetric(horizontal=4, vertical=8)
という既定値を持つため、何も指定しないと必ず外側に白い
余白が出来ます。margin=0 で無効化できます。
ClipBehavior
shape=RoundedRectangleBorder で角丸を描いた場合でも、
内部の子がはみ出して見えてしまうことがあります。
clip_behavior=ft.ClipBehavior.ANTI_ALIAS(あるいは HARD_EDGE)を指定すると
角丸の外側が切り取られ、余計な白縁が消えます。
expand の場所
Image だけでなく
・Stack(expand=True)
・Image(expand=True)
と両方に付けておくと、親(Container)のサイズいっぱいに伸びる
=「必ず全面に画像が貼り付く」動きになります。
Container 自体は width, height を与えるか
さらに上位レイアウトでサイズが決まるようにしてください。
• サイズ比率だけ決めて自動伸縮させたい場合
ft.AspectRatio(ratio=1) を Container の代わりに使うと
「正方形カードを維持しつつレスポンシブに溶け込む」ことも出来ます。
• 行方向・網状に並べるときは
ft.GridView ↔︎ ft.ResponsiveRow を使い、各カードを
col={"xs":12, "sm":6, "md":4, "lg":3} のように割り当てると
ブラウザ幅に合わせて自動折り返ししてくれます。
これで Card の余白問題はほぼ解決できるはずです。
ぜひ試してみてください 🙌