typescript 如何将线性梯度背景应用于所有React导航屏面?

toiithl6  于 2023-01-18  发布在  TypeScript
关注(0)|答案(1)|浏览(132)

我想使用“expo-linear-gradient"”中的LinearGradient组件,该组件在我的所有屏幕堆栈中充当背景。如何实现这一点?
请参见App.tsx中的堆栈配置:

export default function App() {
  const { t } = useTranslation("Titles");

  return (
    <Provider store={store}> // redux
      <NativeBaseProvider theme={nativeBaseTheme}> // native base wrapper. maybe a solution here?
        <NavigationContainer>
          <Stack.Navigator>
            // a possible wrapper as a background for all the screens?
            <Stack.Screen
              name="HomePage"
              component={HomePage}
              options={{ title: t("1") as string }} />
            <Stack.Screen
              name="Settings"
              component={Settings}
              options={{ title: t("2") as string }} />
            <Stack.Screen
              name="Sign Up"
              component={SignUp}
              options={{
                title: t("3") as string
              }} />
          </Stack.Navigator>
        </NavigationContainer>
      </NativeBaseProvider>
    </Provider>
  );
}
  • (函数t仅用于翻译,请忽略)*

我试图从"@react-navigation/native"覆盖DefaultTheme,但是那里的背景域只接受string。所以它不起作用。
如果我用LinearGradient Package 一个功能组件,比如SignUp,它可以工作,但是这样我重复了我的代码,这是一个不好的做法,并且很难扩展应用程序。
我读了a similar question here,但我猜解决方案已经过时了,它对我不起作用。
我来自React背景,不是本地人,所以这对我来说真的很困惑。我很高兴得到任何帮助,提前感谢。

zbwhf8kr

zbwhf8kr1#

好吧,解决办法很简单:

export default function App() {
  const { t } = useTranslation("Titles");

  return (
    <Provider store={store}>
      <NativeBaseProvider theme={nativeBaseTheme}>
        <NavigationContainer theme={navigationTheme}>
          <LinearGradient colors={['#4c669f', '#3b5998', '#192f6a']} style={{
            position: 'absolute',
            left: 0,
            right: 0,
            top: 0,
            height: '100%'
          }} /> // <-- Put the LinearGradient here, under NavigationContainer
          <Stack.Navigator>
            <Stack.Screen
              name="HomePage"
              component={HomePage}
              options={{
                title: t("1") as string
              }} />
            <Stack.Screen
              name="Settings"
              component={Settings}
              options={{ title: t("2") as string }} />
            <Stack.Screen
              name="Sign Up"
              component={SignUp}
              options={{
                title: t("3") as string
              }} />
          </Stack.Navigator>

        </NavigationContainer>

      </NativeBaseProvider>
    </Provider >
  );
}

相关问题