How can I dismiss keyboard on TextField losing focus?

Am trying to figure out how to dismiss the iOS keyboard if my TextFields do not have focus. Obviously when clicking a button I can call a dismiss keyboard function.

But what I wanted to learn was how to dismiss the keyboard of if the user hits return OR clicks off of the TextFields.

Before I could figure out the "user hit return part" I got stuck at the user being able to click away from the TextFields.

While I can add a onTapGesture to the text fields, I wonder if I can do something like detecting a tapGesture to the entire screen, so that if the user taps on any blank space I could call the dismiss keyboard function.

import SwiftUI

struct ContentView: View {
	
	@State var textField1 = ""
	@State var textField2 = ""
	@State var hasFocus = "No text field has focus"
	@FocusState var leftTyping	: Bool
	@FocusState var rightTyping : Bool
	
	var body: some View {
		VStack {
			Text(hasFocus)
				.font(.largeTitle)
			HStack {
				TextField("left" , text: $textField1)
					.focused($leftTyping)
					.onChange(of: leftTyping) {
						if leftTyping == false, rightTyping == false {
							hideKeyboard()
							hasFocus = "No text field has focus"
						}
						else if leftTyping {
							hasFocus = "focus on left field"
						}
					}
				TextField("right", text: $textField2)
					.focused($rightTyping)
					.onChange(of: rightTyping) {
						if leftTyping == false, rightTyping == false {
							hideKeyboard()
							hasFocus = "No text field has focus"
						}
						else if rightTyping {
							hasFocus = "focus on right field"
						}
					}
			}
			Button ("steal focus"){
				hideKeyboard()
				hasFocus = "No text field has focus"			
			}
			.buttonStyle(.borderedProminent)
			.tint(.brown)
			.font(.largeTitle)
			.padding(10)
			.foregroundStyle(.white)
		}
		.padding()
	}
	func hideKeyboard() {
		UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
	}
}

#Preview {
	ContentView()
}

Is there a way to do this?

How can I dismiss keyboard on TextField losing focus?
 
 
Q