使用 GetX 更改主题
Changing theme using GetX
我只想在特定路线上将主题更改为 redTheme
。为此,我使用了 routingCallback
和
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'App',
theme: defaultTheme(context),
initialRoute: AppPages.INITIAL,
getPages: AppPages.routes,
routingCallback: (routing) => _routingCallback(context, routing),
);
}
_routingCallback(BuildContext context, Routing routing) {
if (routing.current == Routes.PURSUE_MODE) {
_changeThemeIfNeeded(redTheme(context));
} else {
_changeThemeIfNeeded(defaultTheme(context));
}
}
void _changeThemeIfNeeded(ThemeData theme) {
Get.changeTheme(theme);
}
}
不幸的是,它导致了
════════ Exception caught by widgets library
═══════════════════════════════════════════════════════ setState() or
markNeedsBuild() called during build. The relevant error-causing
widget was: Directionality
file:///Users/jakub/.pub-cache/hosted/pub.dartlang.org/get_navigation-3.12.0/lib/src/root/root_widget.dart:235:22
════════════════════════════════════════════════════════════════════════════════════════════════════
因此,我将主题更改包装在 try-catch 块中,所以我在开始时不会出现异常
void _changeThemeIfNeeded(ThemeData theme) {
try {
if (Get.theme != theme) {
Get.changeTheme(theme);
}
} catch (e) {
print('Not ready e = $e');
}
}
但我相信使用 Get
框架可以更优雅地解决这个问题?
我对GetX不是很熟悉。但是,为了避免 setState() or markNeedsBuild() called during build.
错误,您可以使用 addPostFrameCallback,在您的情况下,像这样:
routingCallback: (routing) => WidgetsBinding.instance
.addPostFrameCallback((_) => _routingCallback(context, routing))
我只想在特定路线上将主题更改为 redTheme
。为此,我使用了 routingCallback
和
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'App',
theme: defaultTheme(context),
initialRoute: AppPages.INITIAL,
getPages: AppPages.routes,
routingCallback: (routing) => _routingCallback(context, routing),
);
}
_routingCallback(BuildContext context, Routing routing) {
if (routing.current == Routes.PURSUE_MODE) {
_changeThemeIfNeeded(redTheme(context));
} else {
_changeThemeIfNeeded(defaultTheme(context));
}
}
void _changeThemeIfNeeded(ThemeData theme) {
Get.changeTheme(theme);
}
}
不幸的是,它导致了
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ setState() or markNeedsBuild() called during build. The relevant error-causing widget was: Directionality file:///Users/jakub/.pub-cache/hosted/pub.dartlang.org/get_navigation-3.12.0/lib/src/root/root_widget.dart:235:22 ════════════════════════════════════════════════════════════════════════════════════════════════════
因此,我将主题更改包装在 try-catch 块中,所以我在开始时不会出现异常
void _changeThemeIfNeeded(ThemeData theme) {
try {
if (Get.theme != theme) {
Get.changeTheme(theme);
}
} catch (e) {
print('Not ready e = $e');
}
}
但我相信使用 Get
框架可以更优雅地解决这个问题?
我对GetX不是很熟悉。但是,为了避免 setState() or markNeedsBuild() called during build.
错误,您可以使用 addPostFrameCallback,在您的情况下,像这样:
routingCallback: (routing) => WidgetsBinding.instance
.addPostFrameCallback((_) => _routingCallback(context, routing))