Search code examples
swiftuiswiftui-listswiftui-foreach

How can I change background color of each element in list depend of value inside each row


i created simple To Do app. User is able to pick a color of task, which is save as a String inside tasks array. I would like to update every task background with selected color. I tried something like bellow, but it doesn't work. I also tried to put Color(day.dailyTasks.color) but it also doesn't works. Thank you.

import SwiftUI

struct ListView: View {
    
    
    @EnvironmentObject var taskVM : TaskViewModel
    
    var body: some View {
        
        NavigationView{
            
            List{
                ForEach(taskVM.sortedDays) {day in
                    Section(header:
                                Text(day.day)
                                .font(.title2)
                                .fontWeight(.light)
                    ){
                        ForEach(day.dailyTasks.sorted(by: {$0.hour < $1.hour}), id: \.hour) {task in
                               
                            HStack{
                    
                                Image(systemName: task.complete ? "checkmark.circle.fill" : "circle")
                                    
                                Text(task.tittle)
                                    .font(.system(size: 20))
                                    .fontWeight(.light)
                                    
                                    
                                Spacer()
                                
                                Text(task.hour)
                                    .font(.system(size: 20))
                                    .fontWeight(.light)
                                
                            }
                            .swipeActions {
                                Button(role: .destructive){
                                    taskVM.deleteTask(day: day, task: task)
                                } label:{
                                    Label("Delete", systemImage: "trash.circle.fill")
                                }
                                Button{
                                    taskVM.completeTask(day: day, task: task)
                                } label:{
                                    Label("Done", systemImage: "checkmark.circle.fill")
                                }
                                .tint(.green)
                            }
                        }
                        // : Foreach (Tasks)
                        .listRowBackground(Color(task.color))  // <- Doesn't work
                        
                    
                } // ForEach (Days)
                
                
            }.navigationTitle("Your tasks")
            
        }
        
      }
    }// : ZStack
    
}

Solution

  • If you want to change individual rows, you have to place the .listRowBackground() on the actual row.

            List{
                ForEach(taskVM.sortedDays) {day in
                    Section( ... ){
                        ForEach(day.dailyTasks.sorted(by: {$0.hour < $1.hour}), id: \.hour) {task in
                               
                            HStack{
                                ...
                            }
                            // Place it on the actual row that you want changed, otherwise
                            // you apply it to all rows.
                            // Also, this code assumes that task.color is of type Color
                            .listRowBackground(task.color)
                            .swipeActions {
                                ...
                            }
                        }
                        // : Foreach (Tasks)
                } // ForEach (Days)