在SwiftUI中收藏后,如何保持按钮颜色?

amrnrhlw  于 2023-06-28  发布在  Swift
关注(0)|答案(1)|浏览(99)

我想我的收藏夹按钮切换颜色的基础上,如果用户收藏或取消收藏的字符。我能够将文本从“最喜欢”更改为“最喜欢”,并最初更改颜色。如果我退出应用程序,按钮颜色将恢复为原始颜色。我的calculated属性中有颜色变化的逻辑,但不知道如何在用户打开和关闭应用程序时保持它。
这是我的代码。

import SwiftUI

struct CharacterDetailsView: View {
    
   
 var character: Character
    
    @EnvironmentObject var favorites: Favorites
   
    
    @State private var isFavorited = false
  
    
    
    var favoriteText: String {
        if isFavorited  {
            return "Favorited"
        } else {
            return "Favorite"
        }
    }
    
    var favoriteButton: some View {
        Button {
            isFavorited.toggle()
           
            
            if favorites.contains(character) {
                //If favorites contains a character, we are trying to un-favorite and remove it
                favorites.remove(character)
            } else {
                favorites.add(character)
            }
           
        } label: {
            ZStack {
                if isFavorited == false {
                    Capsule()
                        .strokeBorder(Color.green, lineWidth: 4)
                        .frame(width: 250, height: 50)
                        .background(
                            Capsule()
                                .fill(.green)
                                .cornerRadius(20)
                        )
                
                } else {
                    Capsule()
                        .strokeBorder(Color.blue, lineWidth: 4)
                        .frame(width: 250, height: 50)
                        .background(
                        Capsule()
                            .fill(.blue)
                            .cornerRadius(20)
                        )
                        
                }
                
                HStack {
                    Text(favoriteText)
                        .foregroundColor(.white)
                    Image(systemName: favorites.contains(character) ? "heart.fill" : "heart")
                        .foregroundColor(favorites.contains(character) ? .white : .white)
                        
                }
            }
        }
        .padding(.vertical)
    }
 
    
    var body: some View {
        ScrollView {
            VStack {
                //MARK: - Image
                AsyncImage(url: URL(string: character.image)) {
                    phase in
                    if let image = phase.image {
                        image
                            .resizable()
                            .scaledToFit()
                    } else if phase.error != nil {
                        Text("Couldn't upload photo")
                    } else {
                        ProgressView()
                    }
                }
            

                VStack(alignment: .leading) {
                    Text(character.name)
                        .font(.largeTitle)
                        .bold()
                        .padding(.leading)
                       
                    

            favoriteButton
    
            }
        }
       
    }
    
    
  
}
avwztpqn

avwztpqn1#

您需要从存储了临时状态的环境对象中恢复状态,如

favoriteButton
   .onAppear {
      isFavorite = favorites.contains(character)    // << here !!
   }

相关问题