组件工坊:Reflex 组件库 100 例
·
课程概览
布局
·
Box
·
Flex
·
Stack
·
Grid
·
Container
·
Section
·
Center
·
Spacer
·
Separator
·
Card
·
Inset
·
Aspect Ratio
·
Fragment
排版
·
Text
·
Heading
·
Link
·
Code
·
Markdown
·
Blockquote
·
Quote
·
Strong
·
Em
·
Kbd
表单
·
Button
·
Input
·
Text Area
·
Checkbox
·
Switch
·
Radio Group
·
Select
·
Slider
·
Form
·
Upload
数据展示
·
Badge
·
Avatar
·
Icon
·
Callout
·
List
·
Data List
·
Code Block
·
Progress
·
Spinner
·
Scroll Area
·
Moment
展开与切换
·
Tabs
·
Accordion
·
Segmented Control
浮层
·
Tooltip
·
Popover
·
Hover Card
·
Dialog
·
Alert Dialog
·
Drawer
·
Dropdown Menu
·
Context Menu
·
Toast
动态渲染
·
Cond
·
Match
·
Foreach
·
Auto Scroll
媒体
·
Image
·
Audio
·
Video
表格与数据网格
·
Table
·
Data Table
·
Data Editor
图表
·
Area Chart
·
Bar Chart
·
Line Chart
·
Composed Chart
·
Pie Chart
·
Radar Chart
·
Radial Bar Chart
·
Scatter Chart
·
Funnel Chart
·
Treemap
·
Sankey Chart
·
Error Bar
图表部件
·
Axis
·
Cartesian Grid
·
Chart Tooltip
·
Legend
·
Label
·
Reference
·
Brush
其他图表库
·
Plotly
·
Pyplot
HTML 元素
·
HTML
·
HTML Text
·
HTML Layout
·
HTML Forms
·
HTML Media
·
HTML Tables
·
SVG
其他工具
·
Theme
·
Skeleton
·
Clipboard
·
Html Embed
·
Script
·
Memo
工坊订单
请把设置页面的各个条目从上到下等距堆叠,每个条目里的图标和文字横向并排。
Stack 是把子元素朝一个方向堆叠、并在它们之间留出固定间距的盒子。rx.vstack 纵向堆叠,rx.hstack 横向堆叠,rx.stack 则由你指定方向。三者都基于 rx.flex,可以直接使用 Flex 的属性。区别在于默认值:间距(spacing="3")和对齐(align="start")都已预先设好。
rx.vstack(
rx.card("Card 1"),
rx.card("Card 2"),
rx.card("Card 3"),
)改一改 rx.stack 的方向、间距和对齐。只有中间的盒子更高,所以横向排列时能看出 align 的差别。
试着改属性
direction
spacing
align
justify
rx.stack(
rx.box("Example", background_color="orange", border_radius="3px", padding="4px", width="20%"),
rx.box("Example", background_color="lightblue", border_radius="3px", padding="4px", width="30%", height="3rem"),
rx.box("Example", background_color="lightgreen", border_radius="3px", padding="4px", width="40%"),
direction="column",
spacing="3",
align="start",
justify="start",
width="100%",
min_height="12rem",
padding="8px",
border="1px dashed var(--gray-7)",
)| 属性 | 取值 | 默认值 | 用途 |
|---|---|---|---|
direction | "row" "column" "row-reverse" "column-reverse" | vstack "column",hstack "row",stack 无(横向) | 堆叠方向 |
spacing | "0" ~ "9" | "3" | 子元素间距 |
align | "start" "center" "end" "baseline" "stretch" | "start" | 交叉轴对齐。stretch 时子元素撑满交叉轴 |
justify | "start" "center" "end" "between" | "start" | 堆叠方向(主轴)上的对齐 |
wrap | "nowrap" "wrap" "wrap-reverse" | "nowrap" | 放不下时是否换行 |
as_child | True / False | False | 渲染为子元素并合并属性 |
所有属性与 Flex 相同,都可以用 rx.breakpoints(...) 按屏幕宽度给出不同的值。
rx.stack 没有默认方向,不设置就横向堆叠。官方文档用 CSS 属性 flex_direction 指定方向,而已安装的 0.10 还带有 Flex 的 direction 属性,两种写法都可以。下面左边是 flex_direction="row",右边是 direction="column"。
rx.flex(
rx.stack(
rx.box("Example", bg="orange", border_radius="3px", width="20%"),
rx.box("Example", bg="lightblue", border_radius="3px", width="30%"),
rx.box("Example", bg="lightgreen", border_radius="3px", width="50%"),
flex_direction="row",
width="100%",
),
rx.stack(
rx.box("Example", bg="orange", border_radius="3px", width="20%"),
rx.box("Example", bg="lightblue", border_radius="3px", width="30%"),
rx.box("Example", bg="lightgreen", border_radius="3px", width="50%"),
direction="column",
width="100%",
),
width="100%",
)横向堆叠。给每个子元素设置不同的宽度,它们就按这个比例占位。
rx.hstack(
rx.box("Example", bg="red", border_radius="3px", width="10%"),
rx.box("Example", bg="orange", border_radius="3px", width="10%"),
rx.box("Example", bg="yellow", border_radius="3px", width="10%"),
rx.box("Example", bg="lightblue", border_radius="3px", width="10%"),
rx.box("Example", bg="lightgreen", border_radius="3px", width="60%"),
width="100%",
)纵向堆叠。页面从上到下排布的地方,大多用它。
rx.vstack(
rx.box("Example", bg="red", border_radius="3px", width="20%"),
rx.box("Example", bg="orange", border_radius="3px", width="40%"),
rx.box("Example", bg="yellow", border_radius="3px", width="60%"),
rx.box("Example", bg="lightblue", border_radius="3px", width="80%"),
rx.box("Example", bg="lightgreen", border_radius="3px", width="100%"),
width="100%",
)Stack 的 align 默认是 "start",所以 vstack 里的按钮只占自己文字的宽度。想让子元素横向撑满,就设置 align="stretch",或者给子元素 width="100%"。左边是默认,右边是 stretch。
rx.flex(
rx.vstack(
rx.button("Save"),
rx.button("Cancel", variant="soft"),
width="140px",
),
rx.vstack(
rx.button("Save"),
rx.button("Cancel", variant="soft"),
align="stretch",
width="140px",
),
spacing="6",
)用 hstack 把两个装着标题和正文的盒子并排放置。也可以不用 spacing,而是用 CSS gap 直接写 "2em" 这样的任意长度。
Set aside a small part of every pay day.
Balance today's wishes and tomorrow's needs.
rx.hstack(
rx.box(
rx.heading("Saving Money", as_="h2", size="4"),
rx.text("Set aside a small part of every pay day.", margin_top="0.5em"),
padding="1em",
border="1px solid var(--gray-6)",
),
rx.box(
rx.heading("Spending Money", as_="h2", size="4"),
rx.text("Balance today's wishes and tomorrow's needs.", margin_top="0.5em"),
padding="1em",
border="1px solid var(--gray-6)",
),
gap="2em",
)外层用 vstack 堆叠条目,每个条目用 hstack 把图标·文字·开关并排。中间的 rx.spacer() 把开关推到最右端。
Notifications
Dark mode
Weekly email
rx.vstack(
rx.hstack(rx.icon("bell"), rx.text("Notifications"), rx.spacer(), rx.switch(default_checked=True), align="center", width="100%"),
rx.hstack(rx.icon("moon"), rx.text("Dark mode"), rx.spacer(), rx.switch(), align="center", width="100%"),
rx.hstack(rx.icon("mail"), rx.text("Weekly email"), rx.spacer(), rx.switch(), align="center", width="100%"),
spacing="4",
width="100%",
max_width="320px",
)工坊笔记
vstack 纵向,hstack 横向,stack 用 direction 指定方向。
三者都基于 Flex,direction·align·justify·wrap·spacing 照用不误。
默认值不同:spacing "3",align "start"。
要让子元素撑满交叉轴,用 align="stretch",或给子元素 width="100%"。
内层 hstack + rx.spacer() 把东西推到一端,是很常见的写法。
订阅新课时和新课程通知
本课程新增课时或推出后续课程时,我们会发邮件通知你。
问答
关于本课有任何疑问都可以在这里留言。所有语言版本页面的提问会汇集在一起。