I want to use NavigationLink for open the chat detail view when I click the rows items. Here example of code I mentioned. Any idea will be appreciated.
RecentRowView:
import SwiftUI
struct RecentRowView: View {
var recent: Profile
var animation: Namespace.ID
// Environment Object...
@EnvironmentObject var profileData: ProfileDetailModel
var body: some View {
HStack(spacing: 15){
// Making it as clickable Button....
Button(action: {
withAnimation{
profileData.selectedProfile = recent
profileData.showProfile.toggle()
}
}, label: {
ZStack{
// Without matched geometry effect simply showing image...
Image(recent.profile)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 60, height: 60)
.clipShape(Circle())
if !profileData.showProfile{
Image(recent.profile)
.resizable()
.aspectRatio(contentMode: .fill)
// Matched Geometry Effect...
// Giving unique ID that is from UUID from profile Model....
.matchedGeometryEffect(id: recent.id, in: animation)
.frame(width: 60, height: 60)
.clipShape(Circle())
}
}
})
// it decreased the highlight color....
.buttonStyle(PlainButtonStyle())
VStack{
NavigationLink(destination: ChatDetailView(), isActive: $profileData.show) {
HStack{
VStack(alignment: .leading, spacing: 8, content: {
Text(recent.userName)
.fontWeight(.bold)
Text(recent.lastMsg)
.font(.caption)
.foregroundColor(.gray)
})
Spacer(minLength: 10)
Text(recent.time)
.font(.caption2)
.foregroundColor(.gray)
}
Divider()
}
}
}
.padding(.horizontal)
}
}
struct RecentRowView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
ContentView:
struct ContentView: View {
// ANimation Namespace...
@Namespace var animation
// StateObject...
@StateObject var profileData = ProfileDetailModel()
var body: some View {
Home(animation: animation)
// setting Environment Object...
.environmentObject(profileData)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationView{
ContentView()
}
}
}
ChatDetailView:
import SwiftUI
struct ChatDetailView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}
struct ChatDetailView_Previews: PreviewProvider {
static var previews: some View {
ChatDetailView()
}
}
Here is a very simplistic example:
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(destination: DetailView() .navigationBarTitle("Test Detail")){
Text("Goto Detail").foregroundColor(.blue)
}
.navigationBarTitle("Welcome")
}
}
}
}