I am toggling application theme inside Scaffold
drawer. Whenever I switch themes drawer immediately closes. Is there any way to keep drawer form closing on parent rebuild due to theme change?
return Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
const DrawerHeader(
decoration: BoxDecoration(color: Color(0x4D4971FF)),
//https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4
child: Text(
'Header',
style: style,
),
),
ListTile(
leading: const Icon(Icons.brush),
title: const Text(
'Dark Mode',
style: style,
),
trailing: Switch(
value: context.read<ThemeModeCubit>().state == ThemeMode.dark,
onChanged: (value) {
context.read<ThemeModeCubit>().toggleBrightness();
},
),
),
]
),
);
Here is the Builder that uses Cubit. StackOverflow says
It looks like your post is mostly code; please add some more details.
class AppView extends StatelessWidget {
const AppView({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, authState) {
final _router = AppRouter(
status: authState.status,
initialMessage: null,
).router;
return MaterialApp.router(
themeMode: context.watch<ThemeModeCubit>().state,
debugShowCheckedModeBanner: false,
routerConfig: _router,
);
},
);
}
}
Here is the Cubit
class ThemeModeCubit extends HydratedCubit<ThemeMode> {
ThemeModeCubit() : super(ThemeMode.system);
void toggleBrightness() {
emit(state == ThemeMode.light ? ThemeMode.dark : ThemeMode.light);
}
@override
ThemeMode? fromJson(Map<String, dynamic> json) {
return ThemeMode.values[json['themeMode'] as int];
}
@override
Map<String, dynamic>? toJson(ThemeMode state) {
return <String, int>{'themeMode': state.index};
}
}
UPDATE
class AppView extends StatelessWidget {
const AppView({super.key});
@override
Widget build(BuildContext context) {
final _router = AppRouter(
status: context.watch<AuthenticationBloc>().state.status,
initialMessage: null,
).router;
return MaterialApp.router(
theme: AppTheme.of(context).light(),
darkTheme: AppTheme.of(context).dark(),
themeMode: context.watch<ThemeModeCubit>().state,
debugShowCheckedModeBanner: false,
routerConfig: _router,
);
}
}