エラーを解消するとしたら次のような感じでしょうか。
細かいところを色々修正していますので、質問欄のコードと比較してみてください。
希望する動きと違っていましたらコメントくださいね。
swift
1import SwiftUI
2
3struct ContentView: View {
4 let list = ["a", "b", "c"]
5 @State var backcolor: Bool = false
6 var body: some View {
7 NavigationView {
8 VStack {
9 List {
10 Section {
11 ForEach(list, id: \.self) { text in
12 NavigationLink {
13 NextView(backcolor: $backcolor)
14 } label: {
15 Text("テキスト")
16 }
17 }
18 }
19 }
20 }
21 }
22 }
23}
24
25struct NextView: View {
26 @Binding var backcolor: Bool
27 var body: some View {
28 List {
29 Section(header: Text("背景色")
30 .listRowBackground(Rectangle()
31 .background(Color.clear)
32 .foregroundColor(backcolor ? Color.gray : Color.white)
33 .opacity(0.3))
34 .foregroundColor(backcolor ? Color.white : Color.black)) {
35 Toggle(isOn: $backcolor) { }
36 }
37 }
38 .scrollContentBackground(.hidden)
39 .background(backcolor ? Color.black : Color.white)
40 }
41}
追記です
コメントありがとうございます。
何度やってもリストロウの背景色だけが変化しないので、
ContentViewの背景色が
NextViewの背景色と同じになるように修正してみました。
swift
1import SwiftUI
2
3struct ContentView: View {
4 let list = ["a", "b", "c"]
5 @State var backcolor: Bool = false
6 var body: some View {
7 NavigationView {
8 ZStack {
9 (backcolor ? Color.black : Color.white)
10 .ignoresSafeArea()
11 VStack {
12 List {
13 Section {
14 ForEach(list, id: \.self) { text in
15 NavigationLink {
16 NextView(backcolor: $backcolor)
17 } label: {
18 Text("テキスト")
19 }
20 }
21 }
22 }
23 }
24 }
25 }
26 .scrollContentBackground(.hidden)
27 }
28}
29
30struct NextView: View {
31 @Binding var backcolor: Bool
32 var body: some View {
33 List {
34 Section(header: Text("背景色")
35 .listRowBackground(Rectangle()
36 .background(Color.clear)
37 .foregroundColor(backcolor ? Color.gray : Color.white)
38 .opacity(0.3))
39 .foregroundColor(backcolor ? Color.white : Color.black)) {
40 Toggle(isOn: $backcolor) { }
41 }
42 }
43 .scrollContentBackground(.hidden)
44 .background(backcolor ? Color.black : Color.white)
45 }
46}
次のリンクなども見てみたのですが、
NavigationViewの背景色を設定するのは
色々問題がありそうなのですね。
ios - How to change background color of the NavigationView in SwiftUI? - Stack Overflow
ios - How To Set NavigationView Background Colour in SwiftUI - Stack Overflow
swift - How change background color if using NavigationView in SwiftUI? - Stack Overflow