我正在尝试根据 isSelected 状态更改按钮的颜色但不起作用
struct Box: Identifiable {
var id: Int
var title: String
@State var isSelected: Bool
}
struct BoxView: View {
var box: Box
var body: some View{
Button(action: {
self.box.isSelected.toggle()
}){
Text(box.title)
.foregroundColor(.white)
}
.frame(width: 130, height: 50)
.background(self.box.isSelected ? Color.red : Color.blue)
.cornerRadius(25)
.shadow(radius: 10)
.padding(10)
}
}
Run Code Online (Sandbox Code Playgroud) 如果它是方法中的最后一项,我想重新调整小部件,但我收到错误...尝试调用:call()
itemList.last()
? CircleAvatar(
backgroundColor: mainColor,
radius: 15,
child: Icon(Icons.done),
)
: Container()
Run Code Online (Sandbox Code Playgroud) 当按下循环中的一个按钮时,其背景颜色变为红色,当按下另一个按钮时,其颜色也变为红色。但剩下的按钮仍然是红色的,不会再次变成蓝色。如何只将按下的按钮更改为红色,而将其他按钮更改为蓝色?
struct Box: Identifiable {
var id: Int
var title: String
}
struct MainView: View {
let boxes:[Box] = [
Box(id: 0, title: "Home"),
Box(id: 1, title: "Subjects"),
Box(id: 2, title: "attendence"),
Box(id: 3, title: "H.W"),
Box(id: 4, title: "Quizes"),
Box(id: 5, title: "class schedule"),
Box(id: 6, title: "Exam Schedule"),
Box(id: 7, title: "Inbox"),
Box(id: 8, title: "Evalouation"),
]
@Binding var showMenu: Bool
var body: some View{
VStack {
ScrollView(.horizontal,showsIndicators: false){
HStack{
ForEach(boxes, id: \.id) {
box in
BoxView(box: box) …Run Code Online (Sandbox Code Playgroud)