Swift UI:如果文本字段为空,则禁用按钮

Swift UI: Disable a button if a text field is empty

如果 Swift UI 的文本字段为空,我如何禁用按钮。我的代码:

struct AddTask: View {
  @Binding var isOpen: Bool
  @State var text = ""
  let tint: Color
  let done: (String) -> ()

  var body: some View {
    VStack() {
      HStack {
        Text("What tasks are you planning to do?")
          .font(.custom("Avenir", size: 14))
          .foregroundColor(.gray)
        Spacer()
      }

        CustomTextField(text: $text, isFirstResponder: true)
        .frame(height: 30)

      HStack {
        Spacer()
        Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
          .foregroundColor(self.tint)
      }
      Spacer()
    }
    .padding(.all, 30)
  }
}

如果 CustomTextField 为空,我想禁用按钮。

像下面那样使用 .disabled 修饰符

    Button(action: { self.done(self.text); self.isOpen.toggle() }) { Text("Done") }
      .foregroundColor(self.tint)
      .disabled(self.text.isEmpty)