I want to listen to the current theme permanently because I had some issues with the systemchrom, so I found a workaround for that where I call a BlocProvider.of inside my Widget build. In general, it works perfectly fine now, but is this a clean solution or not a good practice, if last, is there a better way of listening to a SystemUiOverlayStyle permanently?
@override
Widget build(BuildContext context) {
BlocProvider.of<SysUiCubit>(context).changeTheme(context, isDarkMode);
return BlocBuilder<SysUiCubit, SysUiState>(
builder: (context, state) {
return AnnotatedRegion<SystemUiOverlayStyle>(
I added a small logic inside the build so the BlocProvider only gets called when the theme changes:
bool buildOnce =false;
@override
Widget build(BuildContext context) {
bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
if(!buildOnce){
setPrev(Theme.of(context).brightness == Brightness.dark);
buildOnce=true;
}
if(darkmodePrev != isDarkMode){
BlocProvider.of<SysUiCubit>(context).changeTheme(context, isDarkMode);
buildOnce=false;
}
Same thing I asked myself on another point at my app:
@override
Widget build(BuildContext context) {
final todo= context.watch<TodoListCubit>().state.todo;
return BlocListener<TodoListCubit, TodoListState>(
listener: (context, state) {
Here I have a todo list where I call context.watch inside my build function - same question - I didn't find a cleaner solution for updating the todo list every time the widget rebuilds, so is this okay or a bad practice?