Created
June 9, 2021 09:54
-
-
Save iosdevie/66e5797931b909b53c540873e1960769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Colors : Identifiable{ | |
var id = UUID() | |
var name : String | |
} | |
struct SearchingLists: View { | |
@State private var searchQuery: String = "" | |
@State private var colors: [Colors] = [Colors(name: "blue"), | |
Colors(name: "Red"), | |
Colors(name: "Green"), | |
Colors(name: "Yellow"), | |
Colors(name: "Pink"), | |
Colors(name: "Purple"), | |
Colors(name: "White"), | |
Colors(name: "Orange"), | |
Colors(name: "Black"),] | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach (searchResults, id:\.id){ color in | |
Text(color.name) | |
} | |
} | |
.searchable("Search color", text: $searchQuery, | |
placement: .automatic) | |
.navigationTitle("Colors List") | |
} | |
} | |
var searchResults: [Colors] { | |
if searchQuery.isEmpty{ | |
return colors | |
} | |
else{ | |
return colors.filter {$0.name.lowercased().contains(searchQuery.lowercased())} | |
} | |
} | |
} | |
struct SearchingLists_Previews: PreviewProvider { | |
static var previews: some View { | |
SearchingLists() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment