滚动 API
ScrollView、ScrollViewReader、滚动方向和定位锚点。
ScrollView / ScrollViewReader。
#ScrollView
签名
已复制
ScrollView(content=None, axes='vertical', shows_indicators=True, showsIndicators=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content | list[View] \| View \| None | 可滚动区域;支持 with ScrollView(): 收集子视图。 |
axes | str | vertical、horizontal 或 both。 |
shows_indicators | bool | 是否显示滚动指示条。 |
示例
已复制
import appui
def body():
with appui.ScrollView(axes="vertical") as sc:
for i in range(25):
appui.Text(f"段落 {i}").padding(horizontal=4)
return sc.padding()
appui.run(body, presentation="sheet")
参阅:ScrollViewReader、LazyVStack
#ScrollViewReader
签名
已复制
ScrollViewReader(content=None, axes='vertical', shows_indicators=True,
scroll_to=None, anchor='top', showsIndicators=None, scrollTo=None,
children=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content / children | list[View] \| None | 滚动内容。 |
axes | str | 同 ScrollView。 |
shows_indicators | bool | 是否显示指示器。 |
scroll_to / scrollTo | 任意 \| None | 初始或受控滚动目标,需与子视图 .id(...) 对应。 |
anchor | str | 滚动对齐锚点,如 top。 |
示例
已复制
import appui
def body():
return appui.ScrollViewReader(
content=[
appui.Text("顶部").id("top"),
appui.Spacer(min_length=400),
appui.Text("底部锚点").id("bottom"),
],
axes="vertical",
scroll_to="bottom",
anchor="top",
).padding()
appui.run(body, presentation="sheet")
参阅:ScrollView、Spacer