主题
UrlLauncher 继承自:Service, BaseControl
提供 URL 启动能力。
在 Web 应用中打开新标签页
浏览器会把不是在点击期间打开的新标签页当作弹窗并拦截它,因此在 Web 上, 带 UrlTarget.BLANK 的 launch_url() 可能无法正常工作。
可以设置控件的 url 属性,或者使用 OpenUrl 动作 —— 两者都由客户端 在原始手势内部执行。参见 客户端动作。
通过客户端动作打开 URL
python
import flet as ft
def main(page: ft.Page):
# `action` is performed by the client while it is still handling the click,
# so opening a new tab is not treated as an unsolicited popup. Compare with
# `UrlLauncher().launch_url()`, which has to reach Python first and is
# therefore blocked by Safari on iOS.
page.add(
ft.SafeArea(
content=ft.Column(
controls=[
ft.Text("Both buttons open the same page:"),
ft.Button(
"Open in this tab",
action=ft.OpenUrl("https://flet.dev", target=ft.UrlTarget.SELF),
),
ft.Button(
"Open in a new tab",
action=ft.OpenUrl(
"https://flet.dev",
target=ft.UrlTarget.BLANK,
),
),
ft.Text(
"An action can be combined with on_click - the action "
"runs on the client, then your handler runs in Python."
),
ft.Button(
"Open and log",
action=ft.OpenUrl("https://flet.dev/docs"),
on_click=lambda e: page.show_dialog(
ft.SnackBar(ft.Text("Docs opened"))
),
),
],
),
)
)
if __name__ == "__main__":
ft.run(main)示例
python
import flet as ft
async def main(page: ft.Page):
url_launcher = ft.UrlLauncher()
url = ft.TextField(label="URL to open", value="https://flet.dev", expand=True)
status = ft.Text()
async def can_launch():
can = await url_launcher.can_launch_url(url.value)
status.value = f"Can launch: {can}"
async def launch_default():
await url_launcher.launch_url(url.value)
async def launch_in_app_webview():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.IN_APP_WEB_VIEW,
web_view_configuration=ft.WebViewConfiguration(
enable_javascript=True, enable_dom_storage=True
),
)
async def launch_in_app_browser_view():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.IN_APP_BROWSER_VIEW,
browser_configuration=ft.BrowserConfiguration(show_title=True),
)
async def launch_external():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.EXTERNAL_APPLICATION,
web_only_window_name="_blank",
)
async def launch_popup():
await url_launcher.open_window(
url.value, title="Flet popup", width=480, height=640
)
async def close_webview(_):
supported = await url_launcher.supports_close_for_launch_mode(
ft.LaunchMode.IN_APP_WEB_VIEW
)
if supported:
await url_launcher.close_in_app_web_view()
else:
status.value = "Close in-app web view not supported on this platform"
page.add(
ft.SafeArea(
content=ft.Column(
controls=[
ft.Column(
[
url,
ft.Row(
[
ft.Button("Launch URL", on_click=launch_default),
ft.Button(
"Launch in-app webview",
on_click=launch_in_app_webview,
),
ft.Button(
"Launch in-app browser view",
on_click=launch_in_app_browser_view,
),
ft.Button(
"Launch external/new tab",
on_click=launch_external,
),
ft.Button(
"Open popup window (web)", on_click=launch_popup
),
ft.Button("Can launch?", on_click=can_launch),
ft.Button(
"Close in-app webview", on_click=close_webview
),
],
wrap=True,
),
status,
],
)
],
),
)
)
if __name__ == "__main__":
ft.run(main)属性
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
data | Any | skip_ | 任意类型的自定义数据。 |
key | Optional['KeyValue'] | None | 用于在更新之间保持控件标识稳定的键。 |
只读属性
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
parent | Optional['BaseControl'] | — | 此控件的直接祖先(父级)。 默认为 None,只有在此控件被挂载(加入页面树)后才有值。 Page 控件(页面树的根)是个例外 —— 它的 parent 始终为 None。 |
page | 'Page' | — | 此控件所属的页面。 |
方法
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
launch_url | — | — | 在 Web 浏览器或应用内视图中打开给定的 url。 参数: url:要打开的 URL。 mode:打开该 URL 的首选启动模式。 web_view_configuration:应用内 WebView 的可选配置。 browser_configuration:应用内浏览器视图的可选配置。 web_only_window_name:仅 Web 启动时使用的窗口名称。 |
can_launch_url | bool | — | 检查指定的 URL 是否能由设备上已安装的某个应用处理。 参数: url:要检查的 URL。 返回: 如果可以验证存在可用的处理器则返回 True。 如果没有可用的处理器,或应用没有权限进行检查,则返回 False。例如: - 在较新版本的 Android 和 iOS 上,除非应用已配置为允许向系统查询启动支持,否则此方法始终返回 False。 - 在 Web 上,除了少数始终被认为受支持的特定协议(例如 http(s))外,此方法始终返回 False,因为网页永远不允许查询已安装的应用。 |
close_in_app_web_view | — | — | 如果应用内 WebView 当前处于打开状态,则关闭它。 |
open_window | — | — | 在 Web 环境中打开一个弹出式浏览器窗口。 参数: url:要在弹出窗口中打开的 URL。 title:弹出窗口的标题。 width:期望的弹出窗口宽度(逻辑像素)。 height:期望的弹出窗口高度(逻辑像素)。 |
supports_launch_mode | bool | — | 检查是否支持指定的启动模式。 参数: mode:要验证的启动模式。 返回: 如果平台支持该启动模式则返回 True,否则返回 False。 |
supports_close_for_launch_mode | bool | — | 检查某个启动模式是否支持 close_in_app_web_view。 参数: mode:要验证其关闭支持情况的启动模式。 返回: 如果支持关闭应用内 WebView 则返回 True,否则返回 False。 |
init | — | — | — |
is_isolated | — | — | 返回此控件是否被标记为隔离。 隔离控件会被排除在父级驱动的更新遍历之外,并需要自行管理更新边界。 |
build | — | — | 在控件初始化期间调用一次,用于定义其子控件。在此方法中 page 属性可用。 |
before_update | — | — | 每当此控件被更新时都会调用此方法。 注意: 务必不要在此处调用或请求 update()。 |
before_event | — | — | 在事件处理器执行之前拦截事件。 返回 False 可取消分发。返回 True 或 None 则继续正常的事件处理。 |
did_mount | — | — | 在控件挂载到页面树之后调用。 重写此方法以启动需要已挂载页面的资源,例如订阅、定时器或服务监听器。 |
will_unmount | — | — | 在控件从页面树移除之前调用。 重写此方法以释放 did_mount() 中创建的资源,例如订阅、定时器或外部句柄。 |
get_data_channel | — | — | 解析为此 widget 在 Dart 侧分配的 [DataChannel]。模式: on_data_channel_open: Optional[ft.EventHandler[DataChannelOpenEvent]] = None def init(self): self.on_data_channel_open = self._on_open def _on_open(self, e): self._channel = self.get_data_channel(e.channel_id) 幂等 —— 底层的 Connection 会按 id 缓存 DataChannel,因此重复调用会返回同一实例。没有错误路径:该 id 始终来自框架触发的事件,所以当此方法运行时,通道在两端都已存在。 |
update | None | — | 为此控件请求一次 UI 更新。 在更改控件状态或属性后调用。控件必须已挂载到页面且未被标记为冻结。 |