// it need to be run on real device, not on simulator,
// because changing scheme reloads all view and pops
// all views from the stack. This is not the case.
@Observable
class Colors {
func textColor(for scheme: ColorScheme) -> Color {
scheme == .dark ? .white : .red
}
func backgroundColor(for scheme: ColorScheme) -> Color {
scheme == .dark ? .brown : .green
}
}
struct TestStartView: View {
private var colors = Colors()
@State private var path = NavigationPath()
@State var selection = 9
@State var index = 0
var body: some View {
TabView(selection: $index) {
SchemeTestView()
.environment(colors)
.tag(0)
}
.ignoresSafeArea(.container)
.tabViewStyle(.page(indexDisplayMode: .never))
// here is the problem ❌
}
}
struct SchemeTestView: View {
@Environment(Colors.self) var colors
@Environment(\.colorScheme) var scheme
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
InsideView()
.navigationDestination(for: String.self) { value in
InsideView()
}
}
}
}
struct InsideView: View {
@Environment(Colors.self) var colors
@Environment(\.colorScheme) var scheme
var body: some View {
ZStack {
colors.backgroundColor(for: scheme)
.ignoresSafeArea()
VStack {
Text("hello scheme")
.foregroundStyle(colors.textColor(for: scheme))
NavigationLink(value: "abc") {
Text("Tap me")
}
}
}
}
}
Здесь у вас есть простой пример: < /p> [code]// it need to be run on real device, not on simulator, // because changing scheme reloads all view and pops // all views from the stack. This is not the case.
@Observable class Colors { func textColor(for scheme: ColorScheme) -> Color { scheme == .dark ? .white : .red }
struct TestStartView: View { private var colors = Colors() @State private var path = NavigationPath() @State var selection = 9 @State var index = 0 var body: some View { TabView(selection: $index) { SchemeTestView() .environment(colors) .tag(0) } .ignoresSafeArea(.container) .tabViewStyle(.page(indexDisplayMode: .never)) // here is the problem ❌ } }
struct SchemeTestView: View { @Environment(Colors.self) var colors @Environment(\.colorScheme) var scheme @State var path = NavigationPath() var body: some View { NavigationStack(path: $path) { InsideView() .navigationDestination(for: String.self) { value in InsideView() } } } }
struct InsideView: View { @Environment(Colors.self) var colors @Environment(\.colorScheme) var scheme
Здесь у вас есть простой пример:
// it need to be run on real device, not on simulator,
// because changing scheme reloads all view and pops
// all views from the stack. This is not the case.
@Observable
class Colors {
func textColor(for scheme:...
Здесь у вас есть простой пример:
// it need to be run on real device, not on simulator,
// because changing scheme reloads all view and pops
// all views from the stack. This is not the case.
@Observable
class Colors {
func textColor(for scheme:...
Я работаю с Jetpack Compose на Android (или Compose Multiplatform) и ищу способ создания полной цветовой схемы на основе одного начального цвета, аналогично тому, как работает ColorScheme.fromSeed Flutter.
Во Flutter ColorScheme.fromSeed принимает...
Я пробую собственную цветовую схему Material3 в своем приложении. Но я обнаружил, что компоненты в содержимом отложенных строк не могут автоматически перекомпоноваться при изменении цветовой схемы.