File listing

As the Fyne list widgets do not support icon and text combinations, we will need to construct one from basic components. Within the file group, we update each item to call a new function, makeRow(), that will be defined later on. We pass the filename to this function so that it can load the image and display a suitable caption:

fileList := widget.NewGroup("directory",
makeRow("shiny-hall.jpg"),
makeRow("shiny-hall.jpg"),
makeRow("shiny-hall.jpg"))

The new makeRow() function will return a horizontal box widget containing the image preview and caption text. The preview image is loaded using canvas.NewImageFromFile() and a suitable size is set using SetMinSize(). To be consistent in terms of sizing, theme.IconInlineSize() is used for height and a 50% larger width—assuming most pictures are landscape. Finally, this is returned in a horizontal box, along with a new label widget, using widget.NewHBox():

func makeRow(text string, file string) fyne.CanvasObject {
preview := canvas.NewImageFromFile(file)
iconHeight := theme.IconInlineSize()
preview.SetMinSize(fyne.NewSize(int(float32(iconHeight)*1.5), iconHeight))

return widget.NewHBox(preview, widget.NewLabel(text))
}

With these changes in place, you should see the same interface with icon previews before each filename. Before we are done with the layout, let's polish the image view and see how we can maintain the image aspect ratio:

Placeholder files and image thumbnails added to the interface
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.235.79