注册 登录

清河洛

fyne中的弹窗

qingheluo2024-08-15清河洛123
在"fyne/dialog"包中对弹窗相关的功能进行了实现在fyne的弹窗中,有一个所有弹窗的基本接口,规定了一个弹窗的基本实现方法弹窗是基于fyne.Window窗口的,所以每个弹窗在创建时要指定其所处的窗口当弹窗显示时会使用遮罩层覆盖所在Window窗口,无法对弹窗下层进行交互操作 type Dialog interface { Show() Hide() SetDismissText(label string) // 默认关闭按钮的文本 SetOnClosed(closed func()) // 弹窗关闭时的运行函数 ...

在"fyne/dialog"包中对弹窗相关的功能进行了实现

在fyne的弹窗中,有一个所有弹窗的基本接口,规定了一个弹窗的基本实现方法

弹窗是基于fyne.Window窗口的,所以每个弹窗在创建时要指定其所处的窗口

当弹窗显示时会使用遮罩层覆盖所在Window窗口,无法对弹窗下层进行交互操作

type Dialog interface {
    Show()
    Hide()
    SetDismissText(label string)  // 默认关闭按钮的文本
    SetOnClosed(closed func())
        // 弹窗关闭时的运行函数
        // 会在弹窗自身的回调函数运行之前运行
    Refresh()
    Resize(size fyne.Size)
    MinSize() fyne.Size
}

基于该基本接口实现了多种弹窗

自定义弹窗(Custom)

可以自定义按钮,显示内容

NewCustom(title, dismiss string, content fyne.CanvasObject, parent fyne.Window) *dialog.CustomDialog
ShowCustom()  // 创建并直接显示,所需参数相同,内部调用了Show()
    包含一个dismiss默认关闭按钮的弹窗

NewCustomWithoutButtons(title string, content fyne.CanvasObject, parent fyne.Window) *dialog.CustomDialog
ShowCustomWithoutButtons()
    没有关闭按钮的弹窗

NewCustomConfirm(title, confirm, dismiss string, content fyne.CanvasObject,
    callback func(bool), parent fyne.Window) *ConfirmDialog
ShowCustomConfirm()
    有两个按钮且有回调函数的弹窗

dialog.CustomDialog的方法
SetButtons(buttons []fyne.CanvasObject)

信息提醒(Information)

只有一个按钮,用于一些信息提示的弹窗

NewInformation(title, message string, parent fyne.Window) Dialog
ShowInformation()

NewError(err error, parent fyne.Window) Dialog
ShowError()

询问弹窗(Confirm)

弹窗有两个按钮,左边文本为"No",右边文本为"Yes"

NewConfirm(title, message string, callback func(bool), parent fyne.Window) *dialog.ConfirmDialog
    第三个参数为点击按钮的回调函数
    当点击了No按钮是传入false,点击Yes按钮时传入true
ShowConfirm()

SetDismissText(label string)  设置左侧"No"按钮的文本
SetConfirmText(label string)  设置右侧"Yes"按钮的文本

文件/目录选择弹窗(File)

在fyne中,文件或目录选装弹窗并为调用系统API,而是以弹窗的形式使用自定义界面进行渲染

NewFileOpen(callback func(fyne.URIReadCloser, error), parent fyne.Window) *FileDialog
ShowFileOpen()
    打开文件

NewFolderOpen(callback func(fyne.ListableURI, error), parent fyne.Window) *FileDialog
ShowFolderOpen()
    打开目录

NewFileSave(callback func(fyne.URIWriteCloser, error), parent fyne.Window) *FileDialog
ShowFileSave()
    保存文件

FileDialog的常用方法

SetDismissText(label string)
SetConfirmText(label string)
SetLocation(u fyne.ListableURI)
SetFilter(filter storage.FileFilter)
SetFileName(fileName string)
SetView(v ViewLayout)  // 修改文件视图
    0表示默认
    1表示列表
    2表示网格

storage.FileFilter

storage.NewExtensionFileFilter(extensions []string) FileFilter
    如:.jpg, .mp3, .txt, .sh
storage.NewMimeTypeFileFilter(mimeTypes []string) FileFilter
    如:image/*, audio/mp3, text/plain, application/*

表单弹窗(Form)

显示内容中包含一些表单元素的弹窗

NewForm(title, confirm, dismiss string, items []*widget.FormItem, callback func(bool), parent fyne.Window) *FormDialog
ShowForm()
    当点击confirm或dismiss两个按钮时,仅仅运行回调函数并传入一个bool值,并不会触发Submit事件

Submit()  // 提交表单,不同于点击


网址导航