how to create list item swiftui with left text primary color and right text secondary color (in row) in swift

You can create a custom row view for the list in SwiftUI and get the desired result. Here is an example code:

main.swift
struct CustomRowView: View {

    var leftText: String
    var rightText: String

    var body: some View {
        HStack {
            Text(leftText)
                .foregroundColor(Color.primary)
            Spacer()
            Text(rightText)
                .foregroundColor(Color.secondary)
        }
    }
}

struct ContentView: View {

    var body: some View {
        List {
            CustomRowView(leftText: "Left Text", rightText: "Right Text")
            CustomRowView(leftText: "Primary Color", rightText: "Secondary Color")
        }
    }
}
562 chars
26 lines

In the above code, CustomRowView is a reusable row view with two text views, one aligned to the left and one aligned to the right with a spacer in-between. The foregroundColor modifier is used to set the primary and secondary colors. In ContentView, the List is using CustomRowView to create the rows.

You can customize the row view as per your requirement.

related categories

gistlibby LogSnag