Display Item Details – Fyne Golang GUI Tutorial 60

Display Item Details – Fyne Golang GUI Tutorial 60

package main
// import fyne
import (
    "encoding/json"
    "io/ioutil"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
)
func main() {
    // student struct, you can use any name
    type Student struct {
        Name  string // name N is capital
        Phone string
    }
    // Now creat a slice/ array to store data
    var myStudentData []Student
    // welcome again
    //lets read data from file and display it in list
    data_from_file, _ := ioutil.ReadFile("myFile_name.txt")
    // file name with extension .txt or .json
    // unmarshall/parse data received from file and save/push in slice
    // 2 argument 1. data source, 2. data slice to store data
    json.Unmarshal(data_from_file, &myStudentData)
    // now we can use this data in our list
    // lets create our list
    // new app
    a := app.New()
    // new title and window
    w := a.NewWindow("CRUD APP")
    // resize window
    w.Resize(fyne.NewSize(400, 400))
    // New label to dispaly name and phone number details
    l_name := widget.NewLabel("...")
    l_name.TextStyle = fyne.TextStyle{Bold: true}
    // for phone number
    l_phone := widget.NewLabel("...")
    // entry widget for name
    e_name := widget.NewEntry()
    e_name.SetPlaceHolder("Enter name here...")
    // entry widget for phone
    e_phone := widget.NewEntry()
    e_phone.SetPlaceHolder("Enter phone here...")
    // submit button
    submit_btn := widget.NewButton("Submit", func() {
        // logic part- store our data in json format
        // let create a struct for our data
        // Get data from entry widget and push to slice
        myData1 := &Student{
            Name:  e_name.Text, // data from name entry widget
            Phone: e_phone.Text,
        }
        // append / push data to our slice
        myStudentData = append(myStudentData, *myData1)
        // * star is very important
        // convert / parse data to json format
        // 3 arguments
        // 1st is our slice data
        // ignore 2nd
        // 3rd is identation, we are using space to indent our data
        final_data, _ := json.MarshalIndent(myStudentData, "", " ")
        // writing data to file
        // it take 3 things
        //file name .txt or .json or anything you want to use
        // data source, final_data in our case
        // writing/reading/execute permission
        ioutil.WriteFile("myFile_name.txt", final_data, 0644)
        /// we are done 🙂
        // lets test
        // empty input field after data insertion
        e_name.Text = ""
        e_phone.Text = ""
        // refresh name & phone entry object
        e_name.Refresh()
        e_phone.Refresh()
    })
    // list widget
    list := widget.NewList(
        // first argument is item count
        // len() function to get myStudentData slice len
        func() int { return len(myStudentData) },
        // 2nd argument is for widget choice. I want to use label
        func() fyne.CanvasObject { return widget.NewLabel("") },
        // 3rd argument is to update widget with our new data
        func(lii widget.ListItemID, co fyne.CanvasObject) {
            co.(*widget.Label).SetText(myStudentData[lii].Name)
        },
    )
    // update on clicked/ selected
    list.OnSelected = func(id widget.ListItemID) {
        l_name.Text = myStudentData[id].Name
        l_name.Refresh()
        // for phone number
        l_phone.Text = myStudentData[id].Phone
        l_phone.Refresh()
    }
    // show and run
    w.SetContent(
        // lets create Hsplite container
        container.NewHSplit(
            // first argument is list of data
            list,
            // 2nd is
            // vbox container
            // show both label
            container.NewVBox(l_name, l_phone, e_name, e_phone, submit_btn),
        ),
    )
    w.ShowAndRun()
}

 

Download Code

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: