Created
October 23, 2024 16:08
-
-
Save takoikatakotako/d6341beed26e10e95b1fd0a41611da8a to your computer and use it in GitHub Desktop.
SwiftUIのListの中にボタンを複数設置する
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 ContentView: View { | |
| let pokemons: [String] = ["Snorlax", "Slowpoke", "Pikachu", "Eevee"] | |
| @State var text: String = "" | |
| var filterdPokemons: [String] { | |
| if text.isEmpty { | |
| return pokemons | |
| } else { | |
| return pokemons.filter {$0.uppercased().contains(text.uppercased())} | |
| } | |
| } | |
| var body: some View { | |
| ScrollView { | |
| LazyVStack { | |
| TextField("Type your search", text: $text) | |
| .padding(8) | |
| .textFieldStyle(RoundedBorderTextFieldStyle()) | |
| ForEach(filterdPokemons, id: \.self) { pokemon in | |
| VStack(alignment: .leading) { | |
| Text(pokemon) | |
| .padding(.leading, 12) | |
| Divider() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment