我尝试了许多不同的方法将我的桌子扩展到剩余的可用应用程序空间。
重现我的问题的简单方法是:
package main
import (
"fmt"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type Reminders struct {
ID int64
EventName string
EventDay time.Time
}
func getReminderSlice() [][]any {
var slice [][]any
reminders := []Reminders{{ID: 0, EventName: "First Event Name", EventDay: time.Now()},
{ID: 1, EventName: "Second Name", EventDay: time.Now()}}
slice = append(slice, []any{
"ID",
"Event Name",
"Event Day",
// "Start At",
// "End At",
// "Event Duration",
// "Event Place",
// "Priority",
// "Description",
"Delete?",
})
for _, r := range reminders {
var currentRow []any
currentRow = append(currentRow, strconv.FormatInt(r.ID, 10))
currentRow = append(currentRow, r.EventName)
currentRow = append(currentRow, r.EventDay.Format("2006-01-02"))
// currentRow = append(currentRow, r.EventStartHour.Format("15:04"))
// currentRow = append(currentRow, r.EventEndHour.Format("15:04"))
// currentRow = append(currentRow, strconv.FormatFloat(r.EventDuration, 'f', -1, 64))
// currentRow = append(currentRow, r.EventPlace)
// currentRow = append(currentRow, strconv.Itoa(int(r.Priority)))
// currentRow = append(currentRow, r.Description)
// currentRow = append(currentRow, widget.NewButton("Delete", func() {}))
slice = append(slice, currentRow)
}
return slice
}
func main() {
data := getReminderSlice()
myApp := app.New()
myWindow := myApp.NewWindow("Table Widget")
myWindow.Resize(fyne.NewSize(850, 530))
// Create a table
list := widget.NewTable(
func() (int, int) {
return len(data), len(data[0])
},
func() fyne.CanvasObject {
container := container.NewVBox(widget.NewLabel(""))
return container
},
func(i widget.TableCellID, o fyne.CanvasObject) {
if i.Col == (len(data[0])-1) && i.Row != 0 {
// last cell, put in a button
w := widget.NewButtonWithIcon("Delete", theme.DeleteIcon(),
func() {
dialog.ShowConfirm("Delete?", "This will delete Event",
func(delete bool) {
if delete {
fmt.Println("Deleted")
}
// refresh the reminders table
// refreshRemindersTable(db, uimanager)
}, myWindow)
})
w.Importance = widget.HighImportance
o.(*fyne.Container).Objects = []fyne.CanvasObject{
w,
}
} else {
o.(*fyne.Container).Objects = []fyne.CanvasObject{
widget.NewLabel(data[i.Row][i.Col].(string)),
}
}
})
// trying to streach up table
remindersContainer := container.NewBorder(
nil,
nil,
nil,
nil,
container.NewAdaptiveGrid(1, list))
colWidths := []float32{50, 150, 150, 150, 150, 150, 150, 150, 150}
for i := range colWidths {
list.SetColumnWidth(i, colWidths[i])
}
// tabs
tabs := container.NewAppTabs(
container.NewTabItemWithIcon("Reminders", theme.HomeIcon(), remindersContainer),
// here will be the second tab
// container.NewTabItemWithIcon("ToDo!", theme.ListIcon(), remindersContainer),
)
tabs.SetTabLocation(container.TabLocationLeading)
// create toolbar
toolbar := widget.NewToolbar(
widget.NewToolbarSpacer(),
widget.NewToolbarAction(theme.DocumentCreateIcon(), func() {
}),
widget.NewToolbarAction(theme.ViewRefreshIcon(), func() {}),
widget.NewToolbarAction(theme.SettingsIcon(), func() {}),
)
// Creating finaly looking container
finalContent := container.NewVBox(toolbar, tabs)
myWindow.SetContent(finalContent)
myWindow.ShowAndRun()
}
将最终容器的各个部分组装到 NewVBox(工具栏、选项卡、内容表)后,表会变小。水平位置时无法很好地伸展。如果仅使用带表的选项卡,则一切看起来都很好。
我尝试了很多不同的方法来解决这个问题,但就是搞不清楚到底是什么问题。我希望最终的内容能够占据下面的所有可用空间。
希望我能清楚地表达我的想法。