package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"image/color"
)
func main() {
MyApp := app.NewWithID("color")
c := MyApp.NewWindow("dialog 弹出框")
dialogs(c)
//dialogsColors(c)
//openDialogs(c)
//saveFileDialogs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 输入 选择框
func dialogs(w fyne.Window) {
label1 := widget.NewLabel("") // 标签控件
dialog1 := dialog.NewEntryDialog("title", "message", func(s string) { // 输入对话框 控件
label1.SetText(s)
}, w)
dialog1.SetOnClosed(func() {
fmt.Println("111")
})
dialog1.SetDismissText("no") // 对话框 取消按钮 重置
dialog1.SetPlaceholder("placeholder")// 对话输入控件的 提示框
dialog1.SetText("text") // 设置对话框的内容
btn := widget.NewButton("btn clikc", func() {
dialog1.Show()
})
w.SetContent(container.NewVBox(label1, btn))
}
2. color颜色对话框
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"image/color"
)
func main() {
MyApp := app.NewWithID("color")
c := MyApp.NewWindow("dialog 弹出框")
//dialogs(c)
dialogsColors(c)
//openDialogs(c)
//saveFileDialogs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 提示框 和 颜色选择框
func dialogsColors(w fyne.Window) {
label1 := widget.NewLabel("")
// confirm 提示框
//dialog1 := dialog.NewConfirm("color","message", func(b bool) {
// label1.SetText(strconv.FormatBool(b))
//},w)
// color 提示框
dialog1 := dialog.NewColorPicker("color", "message", func(c color.Color) {
label1.SetText(fmt.Sprintf("%f", c))
}, w)
btn := widget.NewButton("show dialog", func() {
dialog1.Advanced = true // 开始 颜色 色域选择
//dialog1.SetColor(color.Black)
dialog1.Show()
fmt.Println("c")
})
c := container.NewVBox(label1, btn)
w.SetContent(c)
}
3.打开文件对话框
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"image/color"
)
func main() {
MyApp := app.NewWithID("color")
c := MyApp.NewWindow("dialog 弹出框")
//dialogs(c)
//dialogsColors(c)
openDialogs(c)
//saveFileDialogs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 打开文件 选择框
func openDialogs(w fyne.Window) {
label1 := widget.NewLabel("")
dialog1 := dialog.NewFileOpen(func(closer fyne.URIReadCloser, err error) {
if closer == nil {
return
}
label1.SetText(closer.URI().Path())
}, w)
url1, _ := storage.ListerForURI(storage.NewFileURI("./"))
dialog1.SetLocation(url1) // 设置选择目录
btn := widget.NewButton("show open dislog", func() {
//dialog1.Resize(fyne.NewSize(600,600)) // 对话框大小
dialog1.SetFilter(storage.NewExtensionFileFilter([]string{".go"})) //只展示go文件
dialog1.Show() // 显示 对话框
})
w.SetContent(container.NewVBox(label1, btn))
}
4.保存文件对话框
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"image/color"
)
func main() {
MyApp := app.NewWithID("color")
c := MyApp.NewWindow("dialog 弹出框")
//dialogs(c)
//dialogsColors(c)
//openDialogs(c)
saveFileDialogs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 保存文件 选择框
func saveFileDialogs(w fyne.Window) {
label1 := widget.NewLabel("")
dialog1 := dialog.NewFileSave(func(writeCloser fyne.URIWriteCloser, err error) {
writeCloser.Write([]byte("大大"))
label1.SetText(writeCloser.URI().Path())
}, w)
url1, _ := storage.ListerForURI(storage.NewFileURI("./"))
dialog1.SetLocation(url1) // 选择当前目录
dialog1.SetFileName("aaa.txt") //设置保存文件名
button1 := widget.NewButton("show save dialogs", func() {
dialog1.Show()
})
w.SetContent(container.NewVBox(label1, button1))
}
// 打开目录对话框
func openFolders(w fyne.Window) {
label1 := widget.NewLabel("")
folderOpen := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
if uri == nil {
return
}
label1.SetText(uri.Path())
}, w)
luri, _ := storage.ListerForURI(storage.NewFileURI("."))
folderOpen.SetLocation(luri) // 设置打开当前目录
button1 := widget.NewButton("show folderOpen", func() {
folderOpen.Show()
})
w.SetContent(container.NewVBox(label1, button1))
}
6.自定义对话框
// 自定义对话框
func customDialogs(w fyne.Window) {
label1 := widget.NewLabel("")
content := container.NewVBox(
widget.NewLabel("i am a custom label"),
widget.NewButton("custom button",nil),
)
customDialog1 := dialog.NewCustom("custom title","dismiss",content,w)
customDialog1.SetDismissText("ok") // 设置自定义按钮
button1 := widget.NewButton("show cusomDialog", func() {
customDialog1.Show()
})
w.SetContent(container.NewVBox(label1,button1))
}
7. 目录对话框和 tree目录展示结合
// 打开对话框 和 tree控件结合使用
func treeAndFolderDialogs(w fyne.Window) {
root := ".."
rootTxt := widget.NewEntry()
rootTxt.SetText(root)
tree1 := widget.NewTree(nil, nil, nil, nil)
folderOpen := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
if uri == nil {
return
}
rootTxt.SetText(uri.Path())
refreshTree(tree1,rootTxt.Text)
}, w)
button1 := widget.NewButton("tree click", func() {
folderOpen.Show()
})
w.SetContent(container.NewBorder(container.NewVBox(button1,rootTxt), nil, nil, nil, tree1))
}
func refreshTree(tree1 *widget.Tree,root string) {
tree1.Root = root
tree1.ChildUIDs = func(uid widget.TreeNodeID) (c []widget.TreeNodeID) {
if uid == "" {
c = getFileList(root)
} else {
c = getFileList(uid)
}
return
}
tree1.IsBranch = func(uid widget.TreeNodeID) (ok bool) {
return isDir(uid)
}
tree1.CreateNode = func(branch bool) (o fyne.CanvasObject) { // tree 添加控件的
return widget.NewLabel("")
}
tree1.UpdateNode = func(uid widget.TreeNodeID, branch bool, node fyne.CanvasObject) {
lbl := node.(*widget.Label)
file, err := os.Stat(uid)
if err != nil {
lbl.SetText("")
}
lbl.SetText(file.Name())
}
tree1.Refresh() // 刷新tree
}
func isDir(paths string) bool {
f, err := os.Stat(paths)
if err != nil {
return false
}
return f.IsDir()
}
// 得出当前目录内容
func getFileList(paths string) (lst []string) {
f, err := ioutil.ReadDir(paths)
if err != nil {
return
}
for _, file := range f {
path1 := paths + string(filepath.Separator) + file.Name()
lst = append(lst, path1)
}
return
}
发表评论